From f478fa8029114b5c629f0a8842c2730e66b1d51c Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Thu, 8 Nov 2012 11:57:56 -0800 Subject: [PATCH] =?UTF-8?q?Add=20`lodash=20moduleId=3D=E2=80=A6`=20build?= =?UTF-8?q?=20option.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Former-commit-id: a68a2085028a3832181aaf02081311d84e93fab5 --- build.js | 20 +++++++++++++------- build/post-compile.js | 2 +- test/test-build.js | 38 ++++++++++++++++++++++---------------- 3 files changed, 36 insertions(+), 24 deletions(-) diff --git a/build.js b/build.js index a8db56480..af116d555 100755 --- a/build.js +++ b/build.js @@ -254,17 +254,14 @@ * * @private * @param {String} [pattern='/*.jst'] The file path pattern. - * @param {Object} [options=_.templateSettings] The options object. + * @param {Object} options The options object. * @returns {String} Returns the compiled source. */ function buildTemplate(pattern, options) { pattern || (pattern = path.join(cwd, '*.jst')); - options || (options = _.templateSettings); var directory = path.dirname(pattern); - var moduleName = 'lodash'; - var source = [ ';(function(window) {', " var freeExports = typeof exports == 'object' && exports &&", @@ -295,7 +292,7 @@ source.push( " 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);', ' });', " } else if (freeExports) {", @@ -362,6 +359,7 @@ ' (e.g. `lodash template=./*.jst`)', ' lodash settings=... Template settings used when precompiling templates', ' (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.', ' 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 var invalidArgs = _.reject(options.slice(options[0] == 'node' ? 2 : 0), function(value, index, options) { 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 [ @@ -906,6 +904,12 @@ : 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 var outputPath = options.reduce(function(result, value, index) { if (/-o|--output/.test(value)) { @@ -929,7 +933,9 @@ return match ? Function('return {' + match[1].replace(/^{|}$/g, '') + '}')() : result; - }, _.clone(_.templateSettings)); + }, _.extend(_.clone(_.templateSettings), { + 'moduleId': moduleId + })); // flag used to specify a template build var isTemplate = !!templatePattern; diff --git a/build/post-compile.js b/build/post-compile.js index 89a8f2029..c453b2851 100644 --- a/build/post-compile.js +++ b/build/post-compile.js @@ -29,7 +29,7 @@ // 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'); - // 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}'); // unescape properties (i.e. foo["bar"] => foo.bar) diff --git a/test/test-build.js b/test/test-build.js index fe1f59e15..76ca6aae8 100644 --- a/test/test-build.js +++ b/test/test-build.js @@ -488,25 +488,31 @@ }); }); - asyncTest('`lodash template=*.jst` exports=amd', function() { - var start = _.after(2, _.once(QUnit.start)); + ['', 'moduleId=underscore'].forEach(function(command) { + 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) { - var basename = path.basename(filePath, '.js'), - context = createContext(), - pass = false; + build(['-s', 'template=' + templatePath + '/*.jst', 'exports=amd'].concat(command || []), function(source, filePath) { + var moduleId, + basename = path.basename(filePath, '.js'), + context = createContext(), + pass = false; - (context.define = function(requires, factory) { - factory(_); - var templates = _.templates; - pass = 'a' in templates && 'b' in templates; - }) - .amd = {}; + (context.define = function(requires, factory) { + factory(_); + var templates = _.templates; + moduleId = requires + ''; + pass = 'a' in templates && 'b' in templates; + }) + .amd = {}; - vm.runInContext(source, context); - ok(pass, basename); - delete _.templates; - start(); + vm.runInContext(source, context); + + equal(moduleId, command ? 'underscore' : 'lodash'); + ok(pass, basename); + delete _.templates; + start(); + }); }); });