Control "use strict" directive use in builds.

Former-commit-id: 66a9bdee5cc61710d7d42d685feba616ec855322
This commit is contained in:
John-David Dalton
2012-07-12 16:25:30 -04:00
parent f0d7c97b7b
commit cc620205d6

View File

@@ -20,6 +20,9 @@
/** Flag used to specify a mobile build */
var isMobile = !isLegacy && process.argv.indexOf('mobile') > -1;
/** Flag used to specify if the source should be in strict mode */
var isStrict = !(isLegacy || isMobile);
/** Shortcut used to convert array-like objects to arrays */
var slice = [].slice;
@@ -33,6 +36,15 @@
['isBindFast', 'isKeysFast', 'nativeBind', 'nativeIsArray', 'nativeKeys'].forEach(function(varName) {
source = replaceVar(source, varName, 'false');
});
// remove `useStrict` branch from `iteratorTemplate`
source = source.replace(/(?: *\/\/.*\n)*\s*' *<% *if *\(useStrict\).+\n/, '');
// remove `useStrict` from iterator options
source = source.replace(/ *'useStrict': *false,\n/g, '');
// remove `useStrict` data object property assignment in `createIterator`
source = source.replace(/\s*.+?\.useStrict *=.+/, '');
}
else if (isMobile) {
source = replaceVar(source, 'isKeysFast', 'false');
@@ -505,6 +517,17 @@
.replace(/(?: *\/\/.*\n)*\s*'( *)<% *if *\(isKeysFast[\s\S]+?'\1<% *} *else *\{ *%>.+\n([\s\S]+?) *'\1<% *} *%>.+/, '$2');
}
/**
* Removes the "use strict" directive from `source`.
*
* @private
* @param {String} source The source to process.
* @returns {String} Returns the modified source.
*/
function removeUseStrict(source) {
return source.replace(/(["'])use strict\1;( *\n)?/, '');
}
/**
* Removes a given variable from `source`.
*
@@ -938,10 +961,19 @@
// cleanup code
source = source.replace(/^ *;\n/gm, '');
if (!isStrict) {
source = removeUseStrict(source);
}
// begin the minification process
if (filterType || isBackbone || isLegacy || isMobile) {
fs.writeFileSync(path.join(cwd, 'lodash.custom.js'), source);
minify(source, 'lodash.custom.min', function(result) {
// re-remove "use strict" added by the minifier
if (!isStrict) {
result = removeUseStrict(result);
}
fs.writeFileSync(path.join(cwd, 'lodash.custom.min.js'), result);
});
}