Last active
December 14, 2015 16:39
-
-
Save h2non/5117099 to your computer and use it in GitHub Desktop.
A simple RequireJS addon to automatically load minified script sources
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A simple RequireJS addon to automatically load minified script sources | |
* | |
* @method applyMinification | |
* @param {Object} context RequireJS current context Object. Required | |
* @param {Array/String} deps Dependencies to process. Required | |
* @return {Array/String} Minified dependencies | |
* @private | |
* | |
* @cfg {Boolean} minification True to enable it | |
* @cfg {Array} minificationExceptions List of file names to discard. Optional | |
* @cfg {String/Array} minificationContains Dependency String must contains. The String values allow RegEx especial characters. Be aware with that. Optional | |
* @cfg {Boolean} minificationDebug Enables console debug mode. Optional | |
* | |
* ## Note | |
* Only complete path dependencies which includes '.js' will be handled | |
* | |
* ## Library integration | |
* | |
* 1. Copy the snippet inside the RequireJS library source (closure) | |
* 2. Add the call to the private Function in both dependencies entry points in the RequireJS library: define() and context.require() Functions. | |
* You must pass two arguments to the Function (the current context Object and an Array of dependencies). | |
* Be sure the context variables was already defined when you call the Function. | |
* 3. You must override the deps variable with the returned Array of the addon Function call, like this: | |
* deps = applyMinification(context, deps); | |
* | |
* ## Example | |
* | |
* require.config({ | |
* minification: true, | |
* minificationExceptions: [ | |
* 'main.js' | |
* ], | |
* minificationContains: [ | |
* '^resources', | |
* 'js' | |
* ], | |
* minificationDebug: true, | |
* paths: { | |
* async: 'libs/require-async', | |
* order: 'libs/require-order', | |
* text: 'libs/require-text' | |
* } | |
* }); | |
* | |
* // the loaded script file will be /src/myscript.min.js | |
* define(['/src/myscript.js'], function (myModule) { | |
* // module code... | |
* }); | |
* require(['/src/myscript.js'], function(myModule) { | |
* // module code... | |
* }); | |
* | |
*/ | |
function applyMinification(context, deps) { | |
var i, x, l, le, dep, fileName, mustDiscard, minRexExp, jsRexExp, | |
minJsRexExp, hasExceptions, exceptions, contains, debug, isString, | |
config = typeof context !== 'object' ? contexts['_'].config : context.config; | |
if (config.minification) { | |
isString = ostring.call(deps) === '[object String]'; | |
deps = isArray(deps) ? deps : [ deps ]; | |
if (!deps.length) { | |
return (isString) ? deps[0] : deps; | |
} | |
l = deps.length; | |
minRexExp = /\.min$/gi; | |
jsRexExp = /\.js$/gi; | |
minJsRexExp = /\.min\.js$/gi; | |
exceptions = config.minificationExceptions, | |
hasExceptions = isArray(exceptions) && exceptions.length, | |
contains = config.minificationContains || true; | |
debug = config.minificationDebug || false; | |
// process dependencies | |
for (i=0; i<l; i+=1) { | |
dep = deps[i]; | |
mustDiscard = false; | |
if (typeof dep === 'string' && dep.length > 0 && | |
// checking if should be processed as minimized | |
(!minRexExp.test(dep) && jsRexExp.test(dep) && !minJsRexExp.test(dep))) { | |
// check files to discard | |
if (hasExceptions) { | |
le = exceptions.length; | |
fileName = dep.split('/').slice(-1); | |
for (x=0; x<le; x+=1) { | |
// checking whether to discard | |
if (new RegExp(exceptions[x], 'gi').test(fileName)) { | |
mustDiscard = true; | |
break; | |
} | |
} | |
} | |
// if the file must be discarted | |
if (mustDiscard) { | |
continue; | |
} else { | |
// check conditions | |
if (isArray(contains) && contains.length) { | |
le = contains.length; | |
mustDiscard = true; | |
for (x=0; x<le; x+=1) { | |
if (new RegExp(contains[x], 'gi').test(dep)) { | |
mustDiscard = false; | |
break; | |
} | |
} | |
if (mustDiscard) { | |
continue; | |
} | |
} else if (typeof contains === 'string' && !(new RegExp(contains,'gi').test(dep))) { | |
continue; | |
} | |
} | |
// override dependency Array | |
deps[i] = dep.replace(jsRexExp, '.min.js'); // add '.min' | |
if (debug) { | |
console.log('RequireJS: minified resource loaded: ', dep, '=', deps[i]); | |
} | |
} | |
} | |
} | |
return isString ? deps[0] : deps; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment