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

@@ -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 {