mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-06 17:47:49 +00:00
Escape template properties for minified precompiled templates and add a lodash template="…" exports="…" build test.
Former-commit-id: 866252235232ab52cf21842554c37573de4cf402
This commit is contained in:
18
build.js
18
build.js
@@ -275,9 +275,8 @@
|
|||||||
" var freeExports = typeof exports == 'object' && exports &&",
|
" var freeExports = typeof exports == 'object' && exports &&",
|
||||||
" (typeof global == 'object' && global && global == global.global && (window = global), exports);",
|
" (typeof global == 'object' && global && global == global.global && (window = global), exports);",
|
||||||
'',
|
'',
|
||||||
' var templates = {};',
|
' var templates = {},',
|
||||||
'',
|
' _ = window._;',
|
||||||
' var _ = window._;',
|
|
||||||
''
|
''
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -295,16 +294,14 @@
|
|||||||
precompiled = getFunctionSource(_.template(text, null, options)),
|
precompiled = getFunctionSource(_.template(text, null, options)),
|
||||||
prop = filename.replace(/\..*$/, '');
|
prop = filename.replace(/\..*$/, '');
|
||||||
|
|
||||||
source.push(" templates['" + prop + "'] = " + precompiled + ';');
|
source.push(" templates['" + prop.replace(/'/g, "\\'") + "'] = " + precompiled + ';', '');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
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(['" + moduleName + "'], function(lodash) {",
|
||||||
' _ = lodash;',
|
' lodash.templates = lodash.extend(lodash.templates || {}, templates);',
|
||||||
' _.templates = _.extend(_.templates || {}, templates);',
|
|
||||||
' });',
|
' });',
|
||||||
" } else if (freeExports) {",
|
" } else if (freeExports) {",
|
||||||
" if (typeof module == 'object' && module && module.exports == freeExports) {",
|
" if (typeof module == 'object' && module && module.exports == freeExports) {",
|
||||||
@@ -1382,7 +1379,7 @@
|
|||||||
if (isAMD && isGlobal) {
|
if (isAMD && isGlobal) {
|
||||||
source = source.replace(/(?: *\/\/.*\n)* *(?:else )?if *\(freeExports\) *{\s*}\n/, '');
|
source = source.replace(/(?: *\/\/.*\n)* *(?:else )?if *\(freeExports\) *{\s*}\n/, '');
|
||||||
} else {
|
} 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) {
|
if ((source.match(/\bfreeExports\b/g) || []).length < 2) {
|
||||||
@@ -1511,8 +1508,9 @@
|
|||||||
'outputPath': outputPath,
|
'outputPath': outputPath,
|
||||||
'onComplete': function(source) {
|
'onComplete': function(source) {
|
||||||
// correct overly aggressive Closure Compiler minification
|
// 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
|
// inject "use strict" directive
|
||||||
if (isStrict) {
|
if (isStrict) {
|
||||||
source = source.replace(/^(\/\*![\s\S]+?\*\/\n;\(function[^)]+\){)([^'"])/, '$1"use strict";$2');
|
source = source.replace(/^(\/\*![\s\S]+?\*\/\n;\(function[^)]+\){)([^'"])/, '$1"use strict";$2');
|
||||||
|
|||||||
@@ -119,7 +119,7 @@
|
|||||||
// use simple optimizations when minifying template files
|
// use simple optimizations when minifying template files
|
||||||
if (this.isTemplate) {
|
if (this.isTemplate) {
|
||||||
options = options.map(function(value) {
|
options = options.map(function(value) {
|
||||||
return value.replace(/^(compilation_level)=.+$/, '$1=SIMPLE_OPTIMIZATIONS');
|
return value.replace(/^(--compilation_level)=.+$/, '$1=SIMPLE_OPTIMIZATIONS');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -215,7 +215,6 @@
|
|||||||
'take',
|
'take',
|
||||||
'tap',
|
'tap',
|
||||||
'template',
|
'template',
|
||||||
'templates',
|
|
||||||
'templateSettings',
|
'templateSettings',
|
||||||
'throttle',
|
'throttle',
|
||||||
'times',
|
'times',
|
||||||
|
|||||||
@@ -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.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);
|
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();
|
start();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user