Escape template properties for minified precompiled templates and add a lodash template="…" exports="…" build test.

Former-commit-id: 866252235232ab52cf21842554c37573de4cf402
This commit is contained in:
John-David Dalton
2012-10-11 23:51:15 -07:00
parent 58dc0b1aef
commit c8f871ff2a
4 changed files with 32 additions and 12 deletions

View File

@@ -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');

View File

@@ -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');
});
}

View File

@@ -215,7 +215,6 @@
'take',
'tap',
'template',
'templates',
'templateSettings',
'throttle',
'times',

View File

@@ -483,6 +483,29 @@
equal(templates.a(data.a).replace(/[\r\n]+/g, ''), '<ul><li>moe</li><li>larry</li><li>curly</li></ul>', 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();
});
});