diff --git a/build.js b/build.js index f0b8caa974..ce222f73b1 100755 --- a/build.js +++ b/build.js @@ -275,9 +275,8 @@ " var freeExports = typeof exports == 'object' && exports &&", " (typeof global == 'object' && global && global == global.global && (window = global), exports);", '', - ' var templates = {};', - '', - ' var _ = window._;', + ' var templates = {},', + ' _ = window._;', '' ]; @@ -295,16 +294,14 @@ precompiled = getFunctionSource(_.template(text, null, options)), prop = filename.replace(/\..*$/, ''); - source.push(" templates['" + prop + "'] = " + precompiled + ';'); + source.push(" templates['" + prop.replace(/'/g, "\\'") + "'] = " + precompiled + ';', ''); } }); source.push( - '', " if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {", " define(['" + moduleName + "'], function(lodash) {", - ' _ = lodash;', - ' _.templates = _.extend(_.templates || {}, templates);', + ' lodash.templates = lodash.extend(lodash.templates || {}, templates);', ' });', " } else if (freeExports) {", " if (typeof module == 'object' && module && module.exports == freeExports) {", @@ -1382,7 +1379,7 @@ if (isAMD && isGlobal) { source = source.replace(/(?: *\/\/.*\n)* *(?:else )?if *\(freeExports\) *{\s*}\n/, ''); } else { - source = source.replace(/(?: *\/\/.*\n)* *(?:else )?if *\(freeExports\) *{\s*}(?:\s*else *{([\s\S]+?) *})?\n/, '$1'); + source = source.replace(/(?: *\/\/.*\n)* *(?:else )?if *\(freeExports\) *{\s*}(?:\s*else *{([\s\S]+?) *})?\n/, '$1\n'); } if ((source.match(/\bfreeExports\b/g) || []).length < 2) { @@ -1511,8 +1508,9 @@ 'outputPath': outputPath, 'onComplete': function(source) { // correct overly aggressive Closure Compiler minification - source = source.replace(/prototype\s*=\s*{\s*valueOf\s*:\s*1\s*}/, 'prototype={valueOf:1,y:1}'); - + if (!isTemplate) { + source = source.replace(/prototype\s*=\s*{\s*valueOf\s*:\s*1\s*}/, 'prototype={valueOf:1,y:1}'); + } // inject "use strict" directive if (isStrict) { source = source.replace(/^(\/\*![\s\S]+?\*\/\n;\(function[^)]+\){)([^'"])/, '$1"use strict";$2'); diff --git a/build/minify.js b/build/minify.js index 3fede079b5..e819b38c6a 100755 --- a/build/minify.js +++ b/build/minify.js @@ -119,7 +119,7 @@ // use simple optimizations when minifying template files if (this.isTemplate) { options = options.map(function(value) { - return value.replace(/^(compilation_level)=.+$/, '$1=SIMPLE_OPTIMIZATIONS'); + return value.replace(/^(--compilation_level)=.+$/, '$1=SIMPLE_OPTIMIZATIONS'); }); } diff --git a/build/pre-compile.js b/build/pre-compile.js index 76987dd1a0..b63f6cec10 100644 --- a/build/pre-compile.js +++ b/build/pre-compile.js @@ -215,7 +215,6 @@ 'take', 'tap', 'template', - 'templates', 'templateSettings', 'throttle', 'times', diff --git a/test/test-build.js b/test/test-build.js index e76c95dff7..bd704449cc 100644 --- a/test/test-build.js +++ b/test/test-build.js @@ -483,6 +483,29 @@ equal(templates.a(data.a).replace(/[\r\n]+/g, ''), '', basename); equal(templates.b(data.b), 'Hello stooge.', basename); + delete _.templates; + start(); + }); + }); + + asyncTest('`lodash template=*.jst` exports=amd', 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; + + (context.define = function(requires, factory) { + factory(_); + var templates = _.templates; + pass = 'a' in templates && 'b' in templates; + }) + .amd = {}; + + vm.runInContext(source, context); + ok(pass, basename); + delete _.templates; start(); }); });