fix corner cases with export default (#4673)

This commit is contained in:
Alex Lam S.L
2021-02-21 05:01:56 +00:00
committed by GitHub
parent bfe3a8b516
commit b726e364c1
5 changed files with 96 additions and 40 deletions

View File

@@ -5537,6 +5537,27 @@ merge(Compressor.prototype, {
}
}
function to_func_expr(defun, drop_name) {
var ctor;
switch (defun.CTOR) {
case AST_AsyncDefun:
ctor = AST_AsyncFunction;
break;
case AST_AsyncGeneratorDefun:
ctor = AST_AsyncGeneratorFunction;
break;
case AST_Defun:
ctor = AST_Function;
break;
case AST_GeneratorDefun:
ctor = AST_GeneratorFunction;
break;
}
var fn = make_node(ctor, defun, defun);
fn.name = drop_name ? null : make_node(AST_SymbolLambda, defun.name, defun.name);
return fn;
}
AST_Scope.DEFMETHOD("drop_unused", function(compressor) {
if (!compressor.option("unused")) return;
var self = this;
@@ -5632,7 +5653,7 @@ merge(Compressor.prototype, {
in_use.push(def);
}
initializations.add(def.id, node);
return true; // don't go in nested scopes
if (!(tw.parent() instanceof AST_ExportDefault)) return true;
}
if (node instanceof AST_Definitions) {
node.definitions.forEach(function(defn) {
@@ -5828,6 +5849,7 @@ merge(Compressor.prototype, {
if (!(def.id in in_use_ids)) {
log(node.name, "Dropping unused function {name}");
def.eliminated++;
if (parent instanceof AST_ExportDefault) return to_func_expr(node, true);
return in_list ? List.skip : make_node(AST_EmptyStatement, node);
}
}
@@ -9973,25 +9995,7 @@ merge(Compressor.prototype, {
def.single_use = false;
fixed._squeezed = true;
fixed.single_use = true;
if (fixed instanceof AST_LambdaDefinition) {
var ctor;
switch (fixed.CTOR) {
case AST_AsyncDefun:
ctor = AST_AsyncFunction;
break;
case AST_AsyncGeneratorDefun:
ctor = AST_AsyncGeneratorFunction;
break;
case AST_Defun:
ctor = AST_Function;
break;
case AST_GeneratorDefun:
ctor = AST_GeneratorFunction;
break;
}
fixed = make_node(ctor, fixed, fixed);
fixed.name = make_node(AST_SymbolLambda, fixed.name, fixed.name);
}
if (fixed instanceof AST_LambdaDefinition) fixed = to_func_expr(fixed);
if (fixed instanceof AST_Lambda) {
var scope = self.scope.resolve();
fixed.enclosed.forEach(function(def) {