implement --module (#5462)

This commit is contained in:
Alex Lam S.L
2022-05-23 22:45:47 +01:00
committed by GitHub
parent 740f93f5a9
commit c82fc1ef71
8 changed files with 45 additions and 12 deletions

View File

@@ -80,6 +80,7 @@ function Compressor(options, false_by_default) {
keep_infinity : false,
loops : !false_by_default,
merge_vars : !false_by_default,
module : false,
negate_iife : !false_by_default,
objects : !false_by_default,
optional_chains : !false_by_default,
@@ -97,7 +98,7 @@ function Compressor(options, false_by_default) {
switches : !false_by_default,
templates : !false_by_default,
top_retain : null,
toplevel : !!(options && options["top_retain"]),
toplevel : !!(options && (options["module"] || options["top_retain"])),
typeofs : !false_by_default,
unsafe : false,
unsafe_comps : false,
@@ -130,6 +131,7 @@ function Compressor(options, false_by_default) {
var escaped = def.escaped;
return escaped && escaped.depth != 1;
};
if (this.options["module"]) this.directives["use strict"] = true;
var pure_funcs = this.options["pure_funcs"];
if (typeof pure_funcs == "function") {
this.pure_funcs = pure_funcs;
@@ -7360,6 +7362,7 @@ Compressor.prototype.compress = function(node) {
}
});
tt.push(compressor.parent());
tt.directives = Object.create(compressor.directives);
self.transform(tt);
if (self instanceof AST_Lambda
&& self.body.length == 1

View File

@@ -81,13 +81,14 @@ function minify(files, options) {
keep_fargs: false,
keep_fnames: false,
mangle: {},
module: false,
nameCache: null,
output: {},
parse: {},
rename: undefined,
sourceMap: false,
timings: false,
toplevel: false,
toplevel: !!(options && options["module"]),
v8: false,
validate: false,
warnings: false,
@@ -101,6 +102,7 @@ function minify(files, options) {
if (options.ie) set_shorthand("ie", options, [ "compress", "mangle", "output", "rename" ]);
if (options.keep_fargs) set_shorthand("keep_fargs", options, [ "compress", "mangle", "rename" ]);
if (options.keep_fnames) set_shorthand("keep_fnames", options, [ "compress", "mangle", "rename" ]);
if (options.module) set_shorthand("module", options, [ "compress", "parse" ]);
if (options.toplevel) set_shorthand("toplevel", options, [ "compress", "mangle", "rename" ]);
if (options.v8) set_shorthand("v8", options, [ "mangle", "output", "rename" ]);
if (options.webkit) set_shorthand("webkit", options, [ "compress", "mangle", "output", "rename" ]);

View File

@@ -237,8 +237,7 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
newline_before : false,
regex_allowed : false,
comments_before : [],
directives : {},
directive_stack : [],
directives : Object.create(null),
read_template : with_eof_error("Unterminated template literal", function(strings) {
var s = "";
for (;;) {
@@ -635,23 +634,19 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
};
next_token.add_directive = function(directive) {
S.directive_stack[S.directive_stack.length - 1].push(directive);
S.directives[directive] = (S.directives[directive] || 0) + 1;
S.directives[directive] = true;
}
next_token.push_directives_stack = function() {
S.directive_stack.push([]);
S.directives = Object.create(S.directives);
}
next_token.pop_directives_stack = function() {
var directives = S.directive_stack.pop();
for (var i = directives.length; --i >= 0;) {
S.directives[directives[i]]--;
}
S.directives = Object.getPrototypeOf(S.directives);
}
next_token.has_directive = function(directive) {
return S.directives[directive] > 0;
return !!S.directives[directive];
}
return next_token;
@@ -698,6 +693,7 @@ function parse($TEXT, options) {
expression : false,
filename : null,
html5_comments : true,
module : false,
shebang : true,
strict : false,
toplevel : null,
@@ -2545,6 +2541,7 @@ function parse($TEXT, options) {
return function() {
var start = S.token;
var body = [];
if (options.module) S.input.add_directive("use strict");
S.input.push_directives_stack();
while (!is("eof"))
body.push(statement());