mklabs / node-build-script

MIT License
322 stars 55 forks source link

Switching between non-minified and minified versions of jQuery #7

Open KingScooty opened 12 years ago

KingScooty commented 12 years ago

So i've started to look into this issue. The most straight forward answer is to switch between non-minified and minified versions of the Google CDN hosted file, while falling back to the permanently minified locally hosted one:

Index.html:

<!-- Grab Google CDN's jQuery, with a protocol relative URL; fall back to local if offline -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
<!-- end CDN -->
<script>window.jQuery || document.write('<script src="static/js/libs/jquery-1.7.1.min.js"><\/script>')</script>
<!-- end jQuery -->

usemin.js:

log.writeln('switch from a regular jquery to minified');
content = content.replace(/<!--\s*Grab Google[\d\w\s\W\n]*<!--\s*end CDN\s*-->/gm, function(match, prefix) {
  return '<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>';
});

But this is far from ideal.

  1. The jQuery version number for both files should be controlled from the markup *.html /footer.php.
  2. The HTML comments add control and insight on the development version of index.html, so they should be kept (in some form or another), and stripped out during build.
mklabs commented 12 years ago

I'm pretty sure this can be done without the surrounding html comments. On each script replacement, a basic check can be done to see if it's hosted on a cdn (i'm also thinking of cdnjs here) and if it's jquery or a fallback replacement.

This surely slightly increase the complexity of the usemin task, but if the value added is worth it..

Relatedly, I'm wondering if we should provide an option to simply remove cdn + fallback script in favor of one single <script defer src="bundle.js"></script> with all libraries and application code. Ideally, the bundle.js would be specific for each file depending on what script imports they use in template files (either it is .html or other extensions).

I originally thought that you were talking about the other way around, eg. switching from minified to non minified version of jQuery. The original h5bp repo do use minified version so we should keep it that way. However in development, I often use non minified version of third-party libs as it can come handy to inspect the source. It might be good to have a special task to switch between minified version to non-minified version and vice versa, but that should be kept outside of the usemin task I guess.

These are my thoughts :)

KingScooty commented 12 years ago

Yeah, in development/staging i try to use non-minified versions of all scripts for easier debugging etc. So it would be quite nice to be able to switch between jQueries too.

I only mentioned comments as i like to heavily document all my code, so i guess we can always strip out comments separately.

Relatedly, I'm wondering if we should provide an option to simply remove cdn + fallback script in favor of one single with all libraries and application code. Ideally, the bundle.js would be specific for each file depending on what script imports they use in template files (either it is .html or other extensions).

This could be a nice optional feature. I'm not too sure how to go about it though.