fix export related issues (#2005)

- `mangle` non-exported names
- `unused` on `export` of `function`
- `hoist_funs` on `export`
- `export default`
  - prohibit definition statements
  - parse `AST_Defun` properly
  - drop only unused class and function names


fixes #2001
fixes #2004
This commit is contained in:
Alex Lam S.L
2017-05-26 13:35:40 +08:00
committed by GitHub
parent 02811ce35e
commit 39d4d7e20a
4 changed files with 270 additions and 72 deletions

View File

@@ -2157,9 +2157,11 @@ merge(Compressor.prototype, {
// pass 3: we should drop declarations not in_use
var tt = new TreeTransformer(
function before(node, descend, in_list) {
if (node instanceof AST_Function
&& node.name
&& !compressor.option("keep_fnames")) {
var parent = tt.parent();
if (!compressor.option("keep_fnames")
&& ((node instanceof AST_Function || node instanceof AST_ClassExpression) && node.name
|| (node instanceof AST_Defun || node instanceof AST_DefClass)
&& parent instanceof AST_Export && parent.is_default)) {
var def = node.name.definition();
// any declarations with same name will overshadow
// name of this anonymous function and can therefore
@@ -2194,7 +2196,7 @@ merge(Compressor.prototype, {
}
}
}
if ((node instanceof AST_Defun || node instanceof AST_DefClass) && node !== self) {
if ((node instanceof AST_Defun || node instanceof AST_DefClass) && !(parent instanceof AST_Export) && node !== self) {
var keep = (node.name.definition().id in in_use_ids) || !drop_funcs && node.name.definition().global;
if (!keep) {
compressor[node.name.unreferenced() ? "warn" : "info"]("Dropping unused function {name} [{file}:{line},{col}]", template(node.name));
@@ -2202,7 +2204,7 @@ merge(Compressor.prototype, {
}
return node;
}
if (node instanceof AST_Definitions && !(tt.parent() instanceof AST_ForIn && tt.parent().init === node)) {
if (node instanceof AST_Definitions && !(parent instanceof AST_ForIn && parent.init === node)) {
// place uninitialized names at the start
var body = [], head = [], tail = [];
// for unused names whose initialization has
@@ -2298,7 +2300,7 @@ merge(Compressor.prototype, {
if (!(def.id in in_use_ids)
&& (drop_vars || !def.global)
&& self.variables.get(def.name) === def) {
return maintain_this_binding(tt.parent(), node, node.right.transform(tt));
return maintain_this_binding(parent, node, node.right.transform(tt));
}
}
// certain combination of unused name + side effect leads to:
@@ -2384,7 +2386,7 @@ merge(Compressor.prototype, {
dirs.push(node);
return make_node(AST_EmptyStatement, node);
}
if (node instanceof AST_Defun && hoist_funs) {
if (hoist_funs && node instanceof AST_Defun && !(tt.parent() instanceof AST_Export)) {
hoisted.push(node);
return make_node(AST_EmptyStatement, node);
}