mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-12 11:57:49 +00:00
Add lodash moduleId=… build option.
Former-commit-id: a68a2085028a3832181aaf02081311d84e93fab5
This commit is contained in:
20
build.js
20
build.js
@@ -254,17 +254,14 @@
|
|||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
* @param {String} [pattern='<cwd>/*.jst'] The file path pattern.
|
* @param {String} [pattern='<cwd>/*.jst'] The file path pattern.
|
||||||
* @param {Object} [options=_.templateSettings] The options object.
|
* @param {Object} options The options object.
|
||||||
* @returns {String} Returns the compiled source.
|
* @returns {String} Returns the compiled source.
|
||||||
*/
|
*/
|
||||||
function buildTemplate(pattern, options) {
|
function buildTemplate(pattern, options) {
|
||||||
pattern || (pattern = path.join(cwd, '*.jst'));
|
pattern || (pattern = path.join(cwd, '*.jst'));
|
||||||
options || (options = _.templateSettings);
|
|
||||||
|
|
||||||
var directory = path.dirname(pattern);
|
var directory = path.dirname(pattern);
|
||||||
|
|
||||||
var moduleName = 'lodash';
|
|
||||||
|
|
||||||
var source = [
|
var source = [
|
||||||
';(function(window) {',
|
';(function(window) {',
|
||||||
" var freeExports = typeof exports == 'object' && exports &&",
|
" var freeExports = typeof exports == 'object' && exports &&",
|
||||||
@@ -295,7 +292,7 @@
|
|||||||
|
|
||||||
source.push(
|
source.push(
|
||||||
" if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {",
|
" if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {",
|
||||||
" define(['" + moduleName + "'], function(lodash) {",
|
" define(['" + options.moduleId + "'], function(lodash) {",
|
||||||
' lodash.templates = lodash.extend(lodash.templates || {}, templates);',
|
' lodash.templates = lodash.extend(lodash.templates || {}, templates);',
|
||||||
' });',
|
' });',
|
||||||
" } else if (freeExports) {",
|
" } else if (freeExports) {",
|
||||||
@@ -362,6 +359,7 @@
|
|||||||
' (e.g. `lodash template=./*.jst`)',
|
' (e.g. `lodash template=./*.jst`)',
|
||||||
' lodash settings=... Template settings used when precompiling templates',
|
' lodash settings=... Template settings used when precompiling templates',
|
||||||
' (e.g. `lodash settings="{interpolate:/\\\\{\\\\{([\\\\s\\\\S]+?)\\\\}\\\\}/g}"`)',
|
' (e.g. `lodash settings="{interpolate:/\\\\{\\\\{([\\\\s\\\\S]+?)\\\\}\\\\}/g}"`)',
|
||||||
|
' lodash moduleId=... The AMD module ID of Lo-Dash, which defaults to “lodash”, used by precompiled templates',
|
||||||
'',
|
'',
|
||||||
' All arguments, except `legacy` with `csp` or `mobile`, may be combined.',
|
' All arguments, except `legacy` with `csp` or `mobile`, may be combined.',
|
||||||
' Unless specified by `-o` or `--output`, all files created are saved to the current working directory.',
|
' Unless specified by `-o` or `--output`, all files created are saved to the current working directory.',
|
||||||
@@ -809,7 +807,7 @@
|
|||||||
// used to report invalid command-line arguments
|
// used to report invalid command-line arguments
|
||||||
var invalidArgs = _.reject(options.slice(options[0] == 'node' ? 2 : 0), function(value, index, options) {
|
var invalidArgs = _.reject(options.slice(options[0] == 'node' ? 2 : 0), function(value, index, options) {
|
||||||
if (/^(?:-o|--output)$/.test(options[index - 1]) ||
|
if (/^(?:-o|--output)$/.test(options[index - 1]) ||
|
||||||
/^(?:category|exclude|exports|iife|include|minus|plus|settings|template)=.*$/.test(value)) {
|
/^(?:category|exclude|exports|iife|include|moduleId|minus|plus|settings|template)=.*$/i.test(value)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return [
|
return [
|
||||||
@@ -906,6 +904,12 @@
|
|||||||
: exportsAll.slice()
|
: exportsAll.slice()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// used to specify the AMD module ID of Lo-Dash used by precompiled templates
|
||||||
|
var moduleId = options.reduce(function(result, value) {
|
||||||
|
var match = value.match(/moduleId=(.*)/);
|
||||||
|
return match ? match[1] : result;
|
||||||
|
}, 'lodash');
|
||||||
|
|
||||||
// used to specify the output path for builds
|
// used to specify the output path for builds
|
||||||
var outputPath = options.reduce(function(result, value, index) {
|
var outputPath = options.reduce(function(result, value, index) {
|
||||||
if (/-o|--output/.test(value)) {
|
if (/-o|--output/.test(value)) {
|
||||||
@@ -929,7 +933,9 @@
|
|||||||
return match
|
return match
|
||||||
? Function('return {' + match[1].replace(/^{|}$/g, '') + '}')()
|
? Function('return {' + match[1].replace(/^{|}$/g, '') + '}')()
|
||||||
: result;
|
: result;
|
||||||
}, _.clone(_.templateSettings));
|
}, _.extend(_.clone(_.templateSettings), {
|
||||||
|
'moduleId': moduleId
|
||||||
|
}));
|
||||||
|
|
||||||
// flag used to specify a template build
|
// flag used to specify a template build
|
||||||
var isTemplate = !!templatePattern;
|
var isTemplate = !!templatePattern;
|
||||||
|
|||||||
@@ -29,7 +29,7 @@
|
|||||||
// move vars exposed by the Closure Compiler into the IIFE
|
// move vars exposed by the Closure Compiler into the IIFE
|
||||||
source = source.replace(/^((?:(['"])use strict\2;)?(?:var (?:[a-z]+=(?:!0|!1|null)[,;])+)?)([\s\S]*?function[^)]+\){)/, '$3$1');
|
source = source.replace(/^((?:(['"])use strict\2;)?(?:var (?:[a-z]+=(?:!0|!1|null)[,;])+)?)([\s\S]*?function[^)]+\){)/, '$3$1');
|
||||||
|
|
||||||
// correct overly aggressive Closure Compiler minification
|
// correct overly aggressive Closure Compiler advanced optimizations
|
||||||
source = source.replace(/prototype\s*=\s*{\s*valueOf\s*:\s*1\s*}/, 'prototype={valueOf:1,y:1}');
|
source = source.replace(/prototype\s*=\s*{\s*valueOf\s*:\s*1\s*}/, 'prototype={valueOf:1,y:1}');
|
||||||
|
|
||||||
// unescape properties (i.e. foo["bar"] => foo.bar)
|
// unescape properties (i.e. foo["bar"] => foo.bar)
|
||||||
|
|||||||
@@ -488,25 +488,31 @@
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
asyncTest('`lodash template=*.jst` exports=amd', function() {
|
['', 'moduleId=underscore'].forEach(function(command) {
|
||||||
var start = _.after(2, _.once(QUnit.start));
|
asyncTest('`lodash template=*.jst` exports=amd' + (command ? ' ' + command : ''), function() {
|
||||||
|
var start = _.after(2, _.once(QUnit.start));
|
||||||
|
|
||||||
build(['-s', 'template=' + templatePath + '/*.jst', 'exports=amd'], function(source, filePath) {
|
build(['-s', 'template=' + templatePath + '/*.jst', 'exports=amd'].concat(command || []), function(source, filePath) {
|
||||||
var basename = path.basename(filePath, '.js'),
|
var moduleId,
|
||||||
context = createContext(),
|
basename = path.basename(filePath, '.js'),
|
||||||
pass = false;
|
context = createContext(),
|
||||||
|
pass = false;
|
||||||
|
|
||||||
(context.define = function(requires, factory) {
|
(context.define = function(requires, factory) {
|
||||||
factory(_);
|
factory(_);
|
||||||
var templates = _.templates;
|
var templates = _.templates;
|
||||||
pass = 'a' in templates && 'b' in templates;
|
moduleId = requires + '';
|
||||||
})
|
pass = 'a' in templates && 'b' in templates;
|
||||||
.amd = {};
|
})
|
||||||
|
.amd = {};
|
||||||
|
|
||||||
vm.runInContext(source, context);
|
vm.runInContext(source, context);
|
||||||
ok(pass, basename);
|
|
||||||
delete _.templates;
|
equal(moduleId, command ? 'underscore' : 'lodash');
|
||||||
start();
|
ok(pass, basename);
|
||||||
|
delete _.templates;
|
||||||
|
start();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user