When I create a jQuery plugin, is it best practice to include Modernizr when I need it?
I was just wondering since I always strive for very simple things: I currently write a jQuery mobile carousel plugin where you can switch from image to image with a swipe. For the mobile devices, it would make sense to use CSS3 transitions/transformations since that would improve performance.
For testing if the functionality is available, I could use Modernizr but I'm not so keen on since including another script for just a simple carousel plugin seems a bit too much, especially in the mobile world since bandwith is very precious. Would you guys just copy the Modernizr part into the plugin or would you make Modernizr a prerequisi开发者_运维知识库te?
Cheers
If all you need is to test whether a browser has a certain capability or not, you might be better off using the YepNope loader only.
I would package the script in with it and point out that it depends on it.
I would do this for two reasons:
- If an update to the script is released that fixes a bug or closes as security hole it is in their hands to be able to quickly and easily drop in the latest version
- If they are using the script elsewhere then it will cut down on duplicate code being downloaded to the client.
Size wise there is obviously the opposite argument that if they are not using Modernizr elsewhere they will have to download the entire script just for that one method.
I guess its a judgement call depending on the likelihood of them needing the functionality. If they are using a plug-in that is using ultra-modern features of the browser I guess its likely they are going to be using modern features elsewhere as well and be needing to do extra checks.
Using A Seam
One approach to solving this that you might consider is the use of a seam at your third party boundary code. This means that you can package up the calls to the third party library in your own wrapper function. You could then easily swap between Modernizr or your own version of the required functions so that you could release a dependencies and no-dependencies version.
In this case I would personally say just require the user to include the library as a pre-requisite because its a very common library.
精彩评论