aboutsummaryrefslogtreecommitdiff
path: root/node_modules/handlebars/lib/precompiler.js
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2018-09-20 02:56:13 +0200
committerFlorian Dold <florian.dold@gmail.com>2018-09-20 02:56:13 +0200
commitbbff7403fbf46f9ad92240ac213df8d30ef31b64 (patch)
treec58400ec5124da1c7d56b01aea83309f80a56c3b /node_modules/handlebars/lib/precompiler.js
parent003fb34971cf63466184351b4db5f7c67df4f444 (diff)
update packages
Diffstat (limited to 'node_modules/handlebars/lib/precompiler.js')
-rw-r--r--node_modules/handlebars/lib/precompiler.js43
1 files changed, 34 insertions, 9 deletions
diff --git a/node_modules/handlebars/lib/precompiler.js b/node_modules/handlebars/lib/precompiler.js
index 6ba3800cb..ab3eb201d 100644
--- a/node_modules/handlebars/lib/precompiler.js
+++ b/node_modules/handlebars/lib/precompiler.js
@@ -4,7 +4,7 @@ import fs from 'fs';
import * as Handlebars from './handlebars';
import {basename} from 'path';
import {SourceMapConsumer, SourceNode} from 'source-map';
-import uglify from 'uglify-js';
+
module.exports.loadTemplates = function(opts, callback) {
loadStrings(opts, function(err, strings) {
@@ -60,7 +60,7 @@ function loadStrings(opts, callback) {
function loadFiles(opts, callback) {
// Build file extension pattern
- let extension = (opts.extension || 'handlebars').replace(/[\\^$*+?.():=!|{}\-\[\]]/g, function(arg) { return '\\' + arg; });
+ let extension = (opts.extension || 'handlebars').replace(/[\\^$*+?.():=!|{}\-[\]]/g, function(arg) { return '\\' + arg; });
extension = new RegExp('\\.' + extension + '$');
let ret = [],
@@ -235,7 +235,6 @@ module.exports.cli = function(opts) {
}
}
-
if (opts.map) {
output.add('\n//# sourceMappingURL=' + opts.map + '\n');
}
@@ -244,12 +243,7 @@ module.exports.cli = function(opts) {
output.map = output.map + '';
if (opts.min) {
- output = uglify.minify(output.code, {
- fromString: true,
-
- outSourceMap: opts.map,
- inSourceMap: JSON.parse(output.map)
- });
+ output = minify(output, opts.map);
}
if (opts.map) {
@@ -271,3 +265,34 @@ function arrayCast(value) {
}
return value;
}
+
+/**
+ * Run uglify to minify the compiled template, if uglify exists in the dependencies.
+ *
+ * We are using `require` instead of `import` here, because es6-modules do not allow
+ * dynamic imports and uglify-js is an optional dependency. Since we are inside NodeJS here, this
+ * should not be a problem.
+ *
+ * @param {string} output the compiled template
+ * @param {string} sourceMapFile the file to write the source map to.
+ */
+function minify(output, sourceMapFile) {
+ try {
+ // Try to resolve uglify-js in order to see if it does exist
+ require.resolve('uglify-js');
+ } catch (e) {
+ if (e.code !== 'MODULE_NOT_FOUND') {
+ // Something else seems to be wrong
+ throw e;
+ }
+ // it does not exist!
+ console.error('Code minimization is disabled due to missing uglify-js dependency');
+ return output;
+ }
+ return require('uglify-js').minify(output.code, {
+ sourceMap: {
+ content: output.map,
+ url: sourceMapFile
+ }
+ });
+}