Skip to content

Commit

Permalink
Escape template properties for minified precompiled templates and add…
Browse files Browse the repository at this point in the history
… a `lodash template="…" exports="…"` build test.
  • Loading branch information
jdalton committed Oct 12, 2012
1 parent 332855f commit 33441e1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
18 changes: 8 additions & 10 deletions build.js
Expand Up @@ -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._;',
''
];

Expand All @@ -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) {",
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion build/minify.js
Expand Up @@ -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');
});
}

Expand Down
1 change: 0 additions & 1 deletion build/pre-compile.js
Expand Up @@ -215,7 +215,6 @@
'take',
'tap',
'template',
'templates',
'templateSettings',
'throttle',
'times',
Expand Down
23 changes: 23 additions & 0 deletions test/test-build.js
Expand Up @@ -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();
});
});
Expand Down

0 comments on commit 33441e1

Please sign in to comment.