Add exports=npm and first round of modularize node.js support.

Former-commit-id: fd0ae2328312d5e28dd5d866dc56f613aff54817
This commit is contained in:
John-David Dalton
2013-08-18 23:46:16 -07:00
parent eecb6986ee
commit 1901134601

127
build.js
View File

@@ -531,7 +531,8 @@
'amd', 'amd',
'commonjs', 'commonjs',
'global', 'global',
'node' 'node',
'npm'
]; ];
/** List of variables with complex assignments */ /** List of variables with complex assignments */
@@ -834,6 +835,7 @@
isMobile = state.isMobile, isMobile = state.isMobile,
isModern = state.isModern, isModern = state.isModern,
isNode = state.isNode, isNode = state.isNode,
isNpm = state.isNpm,
isStdOut = state.isStdOut, isStdOut = state.isStdOut,
isStrict = state.isStrict, isStrict = state.isStrict,
isUnderscore = state.isUnderscore, isUnderscore = state.isUnderscore,
@@ -894,17 +896,30 @@
.concat(varDepMap[identifier] || arrayRef) .concat(varDepMap[identifier] || arrayRef)
.sort(); .sort();
var depArgs = deps.join(', '), var depPaths = getDepPaths(deps, modulePath);
depPaths = '[' + (deps.length ? "'" + getDepPaths(deps, modulePath).join("', '") + "'" : '') + '], ';
if (isAMD) { if (isAMD) {
depPaths = '[' + (deps.length ? "'" + depPaths.join("', '") + "'" : '') + '], ';
iife.push( iife.push(
'define(' + depPaths + 'function(' + depArgs + ') {', 'define(' + depPaths + 'function(' + deps.join(', ') + ') {',
'%output%', '%output%',
' return ' + identifier + ';', ' return ' + identifier + ';',
'});' '});'
); );
} }
else if (isNode) {
if (isNpm) {
depPaths = deps.map(function(dep) { return 'lodash.' + dep; });
}
iife.push(
_.reduce(depPaths, function(result, path, index) {
return result + (result ? ',\n ' : ' var ') + deps[index] + " = require('" + path + "')";
}, '') + ';',
'%output%',
'module.expoorts = ' + identifier + ';'
);
}
state.buildFuncs = state.includeFuncs = state.includeProps = state.includeVars = empty; state.buildFuncs = state.includeFuncs = state.includeProps = state.includeVars = empty;
state.iife = iife.join('\n'); state.iife = iife.join('\n');
state.outputPath = path.join(outputPath, modulePath + identifier + '.js'); state.outputPath = path.join(outputPath, modulePath + identifier + '.js');
@@ -919,7 +934,14 @@
else { else {
state.buildFuncs = state.includeFuncs = include; state.buildFuncs = state.includeFuncs = include;
} }
build(state); build(state, function(data) {
var source = data.source;
if (isNode) {
source = source.replace(/^ /gm, '');
}
data.source = source;
defaultBuildCallback(data);
});
}); });
// create lodash module // create lodash module
@@ -938,9 +960,8 @@
depPaths = categoryDepPaths.concat(getDepPaths(deps, modulePath)), depPaths = categoryDepPaths.concat(getDepPaths(deps, modulePath)),
iife = []; iife = [];
depPaths = '[' + (depPaths.length ? "'" + depPaths.join("', '") + "'" : '') + '], ';
if (isAMD) { if (isAMD) {
depPaths = '[' + (depPaths.length ? "'" + depPaths.join("', '") + "'" : '') + '], ';
iife.push( iife.push(
'define(' + depPaths + 'function(' + depArgs + ') {', 'define(' + depPaths + 'function(' + depArgs + ') {',
'%output%', '%output%',
@@ -948,6 +969,24 @@
'});' '});'
); );
} }
else if (isNode) {
if (isNpm) {
deps = _.union(deps, _.transform(categories, function(result, category) {
push.apply(result, _.intersection(categoryMap[category], identifiers));
}))
.sort();
depPaths = deps.map(function(dep) { return 'lodash.' + dep; });
}
iife.push(
_.reduce(depPaths, function(result, path, index) {
return result + (result ? ',\n ' : ' var ') + deps[index] + " = require('" + path + "')";
}, '') + ';',
'%output%',
'module.expoorts = ' + identifier + ';'
);
}
state.iife = iife.join('\n'); state.iife = iife.join('\n');
state.buildFuncs = state.includeFuncs = [identifier]; state.buildFuncs = state.includeFuncs = [identifier];
state.includeProps = state.includeVars = empty; state.includeProps = state.includeVars = empty;
@@ -957,10 +996,11 @@
var source = data.source; var source = data.source;
// add category namespaces to each lodash function assignment // add category namespaces to each lodash function assignment
source = source.replace(/(lodash(?:\.prototype)?\.\w+\s*=\s*)(\w+)/g, function(match, prelude, identifier) { if (!isNode) {
return prelude + getCategory(identifier, funcDepMap).toLowerCase() + '.' + identifier; source = source.replace(/(lodash(?:\.prototype)?\.\w+\s*=\s*)(\w+)/g, function(match, prelude, identifier) {
}); return prelude + getCategory(identifier, funcDepMap).toLowerCase() + '.' + identifier;
});
}
if (_.contains(identifiers, 'mixin')) { if (_.contains(identifiers, 'mixin')) {
source = source.replace(/^ *lodashWrapper\.prototype\s*=[^;]+;\n/m, function(match) { source = source.replace(/^ *lodashWrapper\.prototype\s*=[^;]+;\n/m, function(match) {
return match + [ return match + [
@@ -991,6 +1031,10 @@
return prelude + match; return prelude + match;
}); });
if (isNode) {
source = source.replace(/^ /gm, '');
}
data.source = source; data.source = source;
defaultBuildCallback(data); defaultBuildCallback(data);
}); });
@@ -1000,26 +1044,28 @@
state.buildFuncs = state.includeFuncs = state.includeProps = state.includeVars = empty; state.buildFuncs = state.includeFuncs = state.includeProps = state.includeVars = empty;
// create category modules // create category modules
categories.forEach(function(category) { if (!isNpm) {
var deps = _.intersection(categoryMap[category], identifiers).sort(), categories.forEach(function(category) {
depArgs = deps.join(', '), var deps = _.intersection(categoryMap[category], identifiers).sort(),
depPaths = "['" + getDepPaths(deps).join("', '") + "'], ", depArgs = deps.join(', '),
iife = []; depPaths = "['" + getDepPaths(deps).join("', '") + "'], ",
iife = [];
if (isAMD) { if (isAMD) {
iife.push( iife.push(
'define(' + depPaths + 'function(' + depArgs + ') {', 'define(' + depPaths + 'function(' + depArgs + ') {',
'%output%', '%output%',
' return {', ' return {',
deps.map(function(dep) { return " '" + dep + "': " + dep; }).join(',\n'), deps.map(function(dep) { return " '" + dep + "': " + dep; }).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);
}); });
}
} }
/** /**
@@ -2613,12 +2659,17 @@
var isLegacy = !(isModern || isUnderscore) && _.contains(options, 'legacy'); var isLegacy = !(isModern || isUnderscore) && _.contains(options, 'legacy');
// used to specify the ways to export the `lodash` function // used to specify the ways to export the `lodash` function
var exportsOptions = options.reduce(function(result, value) { var exportsOptions = (function() {
return /^exports=.*$/.test(value) ? optionToArray(value).sort() : result; var result = options.reduce(function(result, value) {
}, isUnderscore return /^exports=.*$/.test(value) ? optionToArray(value).sort() : result;
? ['commonjs', 'global', 'node'] }, isUnderscore
: allExports.slice() ? ['commonjs', 'global', 'node']
); : allExports.slice()
);
return isModularize
? _.first(result, 1)
: _.pull(result, 'npm');
}());
// used to specify the AMD module ID of Lo-Dash used by precompiled templates // used to specify the AMD module ID of Lo-Dash used by precompiled templates
var moduleId = options.reduce(function(result, value) { var moduleId = options.reduce(function(result, value) {
@@ -2648,7 +2699,8 @@
var isAMD = _.contains(exportsOptions, 'amd'), var isAMD = _.contains(exportsOptions, 'amd'),
isCommonJS = _.contains(exportsOptions, 'commonjs'), isCommonJS = _.contains(exportsOptions, 'commonjs'),
isGlobal = _.contains(exportsOptions, 'global'), isGlobal = _.contains(exportsOptions, 'global'),
isNode = _.contains(exportsOptions, 'node'); isNpm = _.contains(exportsOptions, 'npm'),
isNode = isNpm || _.contains(exportsOptions, 'node');
// flag to specify a template build // flag to specify a template build
var isTemplate = !!templatePattern; var isTemplate = !!templatePattern;
@@ -4177,6 +4229,7 @@
'isMobile': isMobile, 'isMobile': isMobile,
'isModern': isModern, 'isModern': isModern,
'isNode': isNode, 'isNode': isNode,
'isNpm': isNpm,
'isStdOut': isStdOut, 'isStdOut': isStdOut,
'isStrict': isStrict, 'isStrict': isStrict,
'isUnderscore': isUnderscore, 'isUnderscore': isUnderscore,