Skip to content

Commit d48e8bd

Browse files
committed
upgrade to uglify-js2
1 parent 256b572 commit d48e8bd

File tree

4 files changed

+56
-27
lines changed

4 files changed

+56
-27
lines changed

lib/webpack.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ function webpack(context, moduleName, options, callback) {
366366

367367
// minimize if wished
368368
try {
369-
if(options.minimize) buffer = uglify(buffer, path.join(options.outputDirectory, filename));
369+
if(options.minimize) buffer = uglify(buffer, path.join(options.outputDirectory, filename), options.minimize);
370370
} catch(e) {
371371
return callback(e);
372372
}
@@ -553,15 +553,29 @@ getTemplate.chunk = function(chunk, options, templateOptions) {
553553
}
554554

555555
// minimize it the uglify
556-
function uglify(input, filename) {
557-
var uglify = require("uglify-js");
556+
function uglify(input, filename, options) {
557+
if(typeof options != "object") options = {
558+
warnings: false
559+
};
560+
var uglify2 = require("uglify-js2");
558561
try {
559-
source = uglify.parser.parse(input);
560-
source = uglify.uglify.ast_mangle(source);
561-
source = uglify.uglify.ast_squeeze(source);
562-
source = uglify.uglify.gen_code(source);
562+
var ast = uglify2.parse(input, {
563+
filename: filename
564+
});
565+
ast.figure_out_scope()
566+
if(options.compress !== false) {
567+
var compressor = uglify2.Compressor(options);
568+
ast = ast.transform(compressor);
569+
ast.figure_out_scope();
570+
ast.compute_char_frequency(options.mangle || {});
571+
ast.mangle_names(options.mangle || {});
572+
}
573+
var source = ast.print_to_string({
574+
comments: options.comments || /^\**!|@preserve|@license/,
575+
beautify: options.beautify
576+
});
563577
} catch(e) {
564-
throw new Error(filename + " @ Line " + e.line + ", Col " + e.col + ", " + e.message);
578+
throw new Error(filename + " @ Line " + e.line + ", Col " + e.col + ", " + e);
565579
return input;
566580
}
567581
return source;

lib/writeChunk.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ module.exports = function(depTree, chunk, options) {
3434
buffer.push(module.realId);
3535
buffer.push(": function(module, exports, require) {\n\n");
3636
if(options.includeFilenames) {
37-
buffer.push("/*** ");
37+
buffer.push("/**! ");
3838
buffer.push(shortenFilename(module.request || module.dirname));
39-
buffer.push(" ***/\n\n");
39+
buffer.push(" !**/\n\n");
4040
}
4141
buffer.push(writeSource(module, options,
4242
function(id) { return depTree.modules[id].realId },

lib/writeSource.js

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ module.exports = function(module, options, toRealId, toRealChuckId) {
6161
if(requireItem.id !== undefined && toRealId(requireItem.id) !== undefined) {
6262
var prefix = "";
6363
if(requireItem.name && options.includeFilenames)
64-
prefix += "/* " + shortenFilename(requireItem.name) + " */";
64+
prefix += "/*! " + shortenFilename(requireItem.name) + " */";
6565
if(requireItem.expressionRange) {
6666
replaces.push({
6767
from: requireItem.expressionRange[0],
@@ -106,15 +106,15 @@ module.exports = function(module, options, toRealId, toRealChuckId) {
106106
replaces.push({
107107
from: requireItem.amdNameRange[0],
108108
to: requireItem.amdNameRange[1],
109-
value: "/* "+ requireItem.label.replace(/\*\//g, "* nice try /") + " */0"
109+
value: options.includeFilenames ? "/*! " + requireItem.label.replace(/\*\//g, "* nice try /") + " */0" : "0"
110110
});
111111
}
112112
}
113113
function genContextReplaces(contextItem) {
114114
var postfix = "";
115115
var prefix = "";
116-
if(contextItem.name)
117-
prefix += "/* " + contextItem.name + " */";
116+
if(contextItem.name && options.includeFilenames)
117+
prefix += "/*! " + contextItem.name + " */";
118118
if(contextItem.require) {
119119
replaces.push({
120120
from: contextItem.calleeRange[0],
@@ -201,7 +201,7 @@ module.exports = function(module, options, toRealId, toRealChuckId) {
201201
replaces.push({
202202
from: asyncItem.nameRange[0],
203203
to: asyncItem.nameRange[1],
204-
value: "/* "+ asyncItem.name.replace(/\*\//g, "* nice try /") + " */0"
204+
value: options.includeFilenames ? "/*! " + asyncItem.name.replace(/\*\//g, "* nice try /") + " */0" : "0"
205205
});
206206
}
207207
if(asyncItem.propertyRange) {
@@ -248,8 +248,8 @@ module.exports = function(module, options, toRealId, toRealChuckId) {
248248
values.forEach(function(requireItem, idx) {
249249
if(requireItem.id !== undefined && toRealId(requireItem.id) !== undefined) {
250250
var prefix = "";
251-
if(requireItem.name)
252-
prefix += "/* " + requireItem.name + " */";
251+
if(requireItem.name && options.includeFilenames)
252+
prefix += "/*! " + requireItem.name + " */";
253253
values[idx] = "require(" + prefix + toRealId(requireItem.id) + ")" + (requireItem.append || "");
254254
}
255255
});
@@ -290,7 +290,7 @@ module.exports = function(module, options, toRealId, toRealChuckId) {
290290
// minimize if in debug mode
291291
// else calculate the minimized size for stats
292292
if(options.minimize) {
293-
var minimized = uglify(result, module.filename);
293+
var minimized = uglify(result, module.request, options.minimize, options.debug);
294294
module.size = minimized.length;
295295
if(options.debug) {
296296
result = minimized;
@@ -309,7 +309,7 @@ module.exports = function(module, options, toRealId, toRealChuckId) {
309309
"// module.id = ", module.id, "\n",
310310
"// module.realId = ", module.realId, "\n",
311311
"// module.chunks = ", module.chunks.join(", "), "\n",
312-
"//@ sourceURL=webpack-module:///", encodeURI(shortenFilename(module.request || module.dirname)).replace(/%5C|%2F/g, "/").replace(/^\//, "")
312+
"//@ sourceURL=webpack-module:///", encodeURI(shortenFilename(module.request || module.dirname)).replace(/%5C|%2F/g, "/").replace(/\?/, "%3F").replace(/^\//, "")
313313
].join("")),
314314
");"].join("");
315315
}
@@ -320,15 +320,30 @@ module.exports = function(module, options, toRealId, toRealChuckId) {
320320
return finalResult.join("");
321321
}
322322

323-
function uglify(input, filename) {
324-
var uglify = require("uglify-js");
323+
// minimize it the uglify
324+
function uglify(input, filename, options, beautify) {
325+
if(typeof options != "object") options = {
326+
warnings: false
327+
};
328+
var uglify2 = require("uglify-js2");
325329
try {
326-
source = uglify.parser.parse(input);
327-
source = uglify.uglify.ast_mangle(source);
328-
source = uglify.uglify.ast_squeeze(source);
329-
source = uglify.uglify.gen_code(source);
330+
var ast = uglify2.parse(input, {
331+
filename: filename
332+
});
333+
ast.figure_out_scope()
334+
if(options.compress !== false) {
335+
var compressor = uglify2.Compressor(options);
336+
ast = ast.transform(compressor);
337+
ast.figure_out_scope();
338+
ast.compute_char_frequency(options.mangle || {});
339+
ast.mangle_names(options.mangle || {});
340+
}
341+
var source = ast.print_to_string({
342+
comments: options.comments || /^\**!|@preserve|@license/,
343+
beautify: beautify
344+
});
330345
} catch(e) {
331-
throw new Error(filename + " @ Line " + e.line + ", Col " + e.col + ", " + e.message);
346+
throw new Error(filename + " @ Line " + e.line + ", Col " + e.col + ", " + e);
332347
return input;
333348
}
334349
return source;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"dependencies": {
77
"esprima": "0.9.x",
88
"optimist": "0.2.x",
9-
"uglify-js": "1.2.x",
9+
"uglify-js2": "2.1.x",
1010
"sprintf": "0.1.x",
1111
"enhanced-require": "0.4.x",
1212
"enhanced-resolve": "0.4.x",

0 commit comments

Comments
 (0)