introduce hoist_exports (#4651)

This commit is contained in:
Alex Lam S.L
2021-02-15 01:01:18 +00:00
committed by GitHub
parent c21f096ab8
commit 203ca2586a
3 changed files with 74 additions and 1 deletions

View File

@@ -672,6 +672,9 @@ to be `false` and all symbol names will be omitted.
- `global_defs` (default: `{}`) -- see [conditional compilation](#conditional-compilation)
- `hoist_exports` (default: `true`) -- hoist `export` statements to facilitate
various `compress` and `mangle` optimizations.
- `hoist_funs` (default: `false`) -- hoist function declarations
- `hoist_props` (default: `true`) -- hoist properties from constant object and

View File

@@ -65,6 +65,7 @@ function Compressor(options, false_by_default) {
expression : false,
functions : !false_by_default,
global_defs : false,
hoist_exports : !false_by_default,
hoist_funs : false,
hoist_props : !false_by_default,
hoist_vars : false,
@@ -183,6 +184,7 @@ merge(Compressor.prototype, {
},
compress: function(node) {
node = node.resolve_defines(this);
node.hoist_exports(this);
if (this.option("expression")) {
node.process_expression(true);
}
@@ -260,6 +262,35 @@ merge(Compressor.prototype, {
return this.TYPE == node.TYPE && this.print_to_string() == node.print_to_string();
});
AST_Toplevel.DEFMETHOD("hoist_exports", function(compressor) {
if (!compressor.option("hoist_exports")) return;
var body = this.body, props = [];
for (var i = 0; i < body.length; i++) {
var stat = body[i];
if (stat instanceof AST_ExportDeclaration) {
body[i] = stat = stat.body;
if (stat instanceof AST_Definitions) {
stat.definitions.forEach(function(defn) {
defn.name.match_symbol(export_symbol, true);
});
} else {
export_symbol(stat.name);
}
} else if (stat instanceof AST_ExportReferences) {
body.splice(i, 1);
[].push.apply(props, stat.properties);
}
}
if (props.length) body.push(make_node(AST_ExportReferences, this, { properties: props }));
function export_symbol(sym) {
if (!(sym instanceof AST_SymbolDeclaration)) return;
var node = make_node(AST_SymbolExport, sym, sym);
node.alias = node.name;
props.push(node);
}
});
AST_Scope.DEFMETHOD("process_expression", function(insert, transform) {
var self = this;
var tt = new TreeTransformer(function(node) {
@@ -9871,6 +9902,10 @@ merge(Compressor.prototype, {
}
});
OPT(AST_SymbolExport, function(self) {
return self;
});
function recursive_ref(compressor, def) {
var level = 0, node = compressor.self();
do {

View File

@@ -142,3 +142,38 @@ mangle_rename: {
}
}
}
hoist_exports: {
options = {
evaluate: true,
hoist_exports: true,
reduce_vars: true,
toplevel: true,
unused: true,
}
mangle = {
toplevel: true,
}
input: {
const a = 42;
export let bbb, { foo: ccc } = a;
export function fff(d, { [bbb]: e }) {
d(e, fff);
}
export default a;
export default async function g(x, ...{ [ccc]: y }) {
(await x)(g, y);
}
}
expect: {
let f, { foo: o } = 42;
function c(t, { [f]: a }) {
t(a, c);
}
export default 42;
export default async function t(a, ...{ [o]: f }) {
(await a)(t, f);
};
export { f as bbb, o as ccc, c as fff };
}
}