support export statements properly (#2126)

- mangle & compress correctly with `toplevel`
- retain non-toplevel import/export
- parse & output `export { variable as name }`
- remove extraneous spaces from `beautify`


fixes #2038
fixes #2124
This commit is contained in:
Alex Lam S.L
2017-06-21 00:51:36 +08:00
committed by GitHub
parent 849ba79dee
commit 62d1fbf645
7 changed files with 199 additions and 57 deletions

View File

@@ -672,7 +672,9 @@ merge(Compressor.prototype, {
node instanceof AST_DefClass ||
node instanceof AST_Defun ||
node instanceof AST_Let ||
node instanceof AST_Const
node instanceof AST_Const ||
node instanceof AST_Export ||
node instanceof AST_Import
);
}
@@ -2099,15 +2101,16 @@ merge(Compressor.prototype, {
return true; // don't go in nested scopes
}
if (node instanceof AST_Definitions && scope === self) {
var in_export = tw.parent() instanceof AST_Export;
node.definitions.forEach(function(def){
if (def.name instanceof AST_SymbolVar) {
var_defs_by_id.add(def.name.definition().id, def);
}
if (!drop_vars) {
if (in_export || !drop_vars) {
def.name.walk(new TreeWalker(function(node) {
if (node instanceof AST_SymbolDeclaration) {
var def = node.definition();
if (def.global && !(def.id in in_use_ids)) {
if ((in_export || def.global) && !(def.id in in_use_ids)) {
in_use_ids[def.id] = true;
in_use.push(def);
}
@@ -4037,6 +4040,10 @@ merge(Compressor.prototype, {
return self;
});
OPT(AST_SymbolExport, function(self, compressor){
return self;
});
OPT(AST_SymbolRef, function(self, compressor){
var def = self.resolve_defines(compressor);
if (def) {