Round two of modularize exports=node support.

Former-commit-id: 8a764774804f194f3fa344219cc8464190c4405f
This commit is contained in:
John-David Dalton
2013-08-20 09:23:02 -07:00
parent a562126f2f
commit 84759de36f
2 changed files with 49 additions and 37 deletions

View File

@@ -899,9 +899,8 @@
var depPaths = getDepPaths(deps, modulePath); var depPaths = getDepPaths(deps, modulePath);
if (isAMD) { if (isAMD) {
depPaths = '[' + (deps.length ? "'" + depPaths.join("', '") + "'" : '') + '], ';
iife.push( iife.push(
'define(' + depPaths + 'function(' + deps.join(', ') + ') {', 'define([' + (depPaths.length ? "'" + depPaths.join("', '") + "'" : '') + '], function(' + deps.join(', ') + ') {',
'%output%', '%output%',
' return ' + identifier + ';', ' return ' + identifier + ';',
'});' '});'
@@ -916,7 +915,7 @@
return result + (result ? ',\n ' : ' var ') + deps[index] + " = require('" + path + "')"; return result + (result ? ',\n ' : ' var ') + deps[index] + " = require('" + path + "')";
}, '') + ';', }, '') + ';',
'%output%', '%output%',
'module.expoorts = ' + identifier + ';' 'module.exports = ' + identifier + ';'
); );
} }
@@ -961,9 +960,8 @@
iife = []; iife = [];
if (isAMD) { if (isAMD) {
depPaths = '[' + (depPaths.length ? "'" + depPaths.join("', '") + "'" : '') + '], ';
iife.push( iife.push(
'define(' + depPaths + 'function(' + depArgs + ') {', 'define([' + (depPaths.length ? "'" + depPaths.join("', '") + "'" : '') + '], function(' + depArgs + ') {',
'%output%', '%output%',
' return lodash;', ' return lodash;',
'});' '});'
@@ -977,13 +975,16 @@
.sort(); .sort();
depPaths = deps.map(function(dep) { return 'lodash.' + dep; }); depPaths = deps.map(function(dep) { return 'lodash.' + dep; });
} else {
deps = categoryDeps.concat(deps);
} }
iife.push( iife.push(
_.reduce(depPaths, function(result, path, index) { _.reduce(depPaths, function(result, path, index) {
return result + (result ? ',\n ' : ' var ') + deps[index] + " = require('" + path + "')"; return result + (result ? ',\n ' : ' var ') + deps[index] + " = require('" + path + "')";
}, '') + ';', }, '') + ';',
'%output%', '%output%',
'module.expoorts = ' + identifier + ';' 'module.exports = ' + identifier + ';'
); );
} }
@@ -996,7 +997,7 @@
var source = data.source; var source = data.source;
// add category namespaces to each lodash function assignment // add category namespaces to each lodash function assignment
if (!isNode) { if (!isNpm) {
source = source.replace(/(lodash(?:\.prototype)?\.\w+\s*=\s*)(\w+)/g, function(match, prelude, identifier) { source = source.replace(/(lodash(?:\.prototype)?\.\w+\s*=\s*)(\w+)/g, function(match, prelude, identifier) {
return prelude + getCategory(identifier, funcDepMap).toLowerCase() + '.' + identifier; return prelude + getCategory(identifier, funcDepMap).toLowerCase() + '.' + identifier;
}); });
@@ -1031,7 +1032,6 @@
return prelude + match; return prelude + match;
}); });
if (isNode) { if (isNode) {
source = source.replace(/^ /gm, ''); source = source.replace(/^ /gm, '');
} }
@@ -1047,13 +1047,12 @@
if (!isNpm) { if (!isNpm) {
categories.forEach(function(category) { categories.forEach(function(category) {
var deps = _.intersection(categoryMap[category], identifiers).sort(), var deps = _.intersection(categoryMap[category], identifiers).sort(),
depArgs = deps.join(', '), depPaths = getDepPaths(deps),
depPaths = "['" + getDepPaths(deps).join("', '") + "'], ",
iife = []; iife = [];
if (isAMD) { if (isAMD) {
iife.push( iife.push(
'define(' + depPaths + 'function(' + depArgs + ') {', "define(['" + depPaths.join("', '") + "'], function(" + deps.join(', ') + ') {',
'%output%', '%output%',
' return {', ' return {',
deps.map(function(dep) { return " '" + dep + "': " + dep; }).join(',\n'), deps.map(function(dep) { return " '" + dep + "': " + dep; }).join(',\n'),
@@ -1061,6 +1060,14 @@
'});' '});'
); );
} }
else if (isNode) {
iife.push(
'%output%',
'module.exports = {',
depPaths.map(function(path, index) { return " '" + deps[index] + "': require('" + path + "')"; }).join(',\n'),
'};'
);
}
state.iife = iife.join('\n'); state.iife = iife.join('\n');
state.outputPath = path.join(outputPath, category.toLowerCase() + '.js'); state.outputPath = path.join(outputPath, category.toLowerCase() + '.js');
build(state); build(state);
@@ -1579,7 +1586,7 @@
} }
// remove the variable assignment from the source // remove the variable assignment from the source
source = source.replace(match, ''); source = source.replace(match, '');
return RegExp('[^\\w"\'.]' + varName + '\\b').test(source); return RegExp('[^\\w"\'.]' + varName + '\\b(?!\\s*=)').test(source);
} }
/** /**
@@ -4190,7 +4197,6 @@
source = source.replace(/(?: *\/\/.*\n)*( *)if *\(typeof +define[\s\S]+?else /, '$1'); source = source.replace(/(?: *\/\/.*\n)*( *)if *\(typeof +define[\s\S]+?else /, '$1');
} }
if (!isNode || isModularize) { if (!isNode || isModularize) {
source = removeVar(source, 'freeGlobal');
source = source.replace(/(?: *\/\/.*\n)*( *)if *\(freeModule[\s\S]+?else *{([\s\S]+?\n)\1}\n+/, '$1$2'); source = source.replace(/(?: *\/\/.*\n)*( *)if *\(freeModule[\s\S]+?else *{([\s\S]+?\n)\1}\n+/, '$1$2');
} }
if (!isCommonJS || isModularize) { if (!isCommonJS || isModularize) {
@@ -4247,24 +4253,6 @@
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/
// customize Lo-Dash's IIFE
if (isIIFE) {
source = (function() {
var token = '%output%',
header = source.match(/^\/\**[\s\S]+?\*\/\n/),
index = iife.indexOf(token);
return header + (index < 0
? iife
: iife.slice(0, index).replace(/\n+$/, '') +
source.replace(/^[\s\S]+?\(function[^{]+{\n|\s*}\(this\)\)[;\s]*$/g, '\n') +
iife.slice(index + token.length).replace(/^\n+/, '')
);
}());
}
/*------------------------------------------------------------------------*/
// modify/remove references to removed functions/variables // modify/remove references to removed functions/variables
if (!isTemplate) { if (!isTemplate) {
if (isExcluded(isNoDep ? 'lodash' : 'lodashWrapper')) { if (isExcluded(isNoDep ? 'lodash' : 'lodashWrapper')) {
@@ -4423,6 +4411,9 @@
} }
} }
} }
if (!useMap.window) {
source = removeVar(source, 'freeGlobal');
}
}()); }());
if (isNoDep) { if (isNoDep) {
@@ -4450,11 +4441,29 @@
source = removeVar(source, 'freeExports'); source = removeVar(source, 'freeExports');
} }
debugSource = cleanupSource(source); /*------------------------------------------------------------------------*/
source = debugSource;
// customize Lo-Dash's IIFE
if (isIIFE) {
source = (function() {
var token = '%output%',
header = source.match(/^\/\**[\s\S]+?\*\/\n/),
index = iife.indexOf(token);
return header + (index < 0
? iife
: iife.slice(0, index) +
source.replace(/^[\s\S]+?\(function[^{]+{\n+|\s*}\.call\(this\)\)[;\s]*$/g, '\n') +
iife.slice(index + token.length)
);
}());
}
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/
debugSource = cleanupSource(source);
source = debugSource;
// resolve `outputPath` and create directories if needed // resolve `outputPath` and create directories if needed
if (!outputPath) { if (!outputPath) {
outputPath = options.reduce(function(result, value, index) { outputPath = options.reduce(function(result, value, index) {

View File

@@ -6,7 +6,7 @@
* Copyright 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors * Copyright 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
* Available under MIT license <http://lodash.com/license> * Available under MIT license <http://lodash.com/license>
*/ */
;(function(window) { ;(function() {
/** Used as a safe reference for `undefined` in pre ES5 environments */ /** Used as a safe reference for `undefined` in pre ES5 environments */
var undefined; var undefined;
@@ -128,6 +128,9 @@
'\u2029': 'u2029' '\u2029': 'u2029'
}; };
/** Used as a reference to the global object */
var window = this;
/** Detect free variable `exports` */ /** Detect free variable `exports` */
var freeExports = objectTypes[typeof exports] && exports; var freeExports = objectTypes[typeof exports] && exports;
@@ -6600,4 +6603,4 @@
// in a browser or Rhino // in a browser or Rhino
window._ = _; window._ = _;
} }
}(this)); }.call(this));