diff --git a/lib/minify.js b/lib/minify.js index ab29d1e74..90dfa730d 100644 --- a/lib/minify.js +++ b/lib/minify.js @@ -165,6 +165,7 @@ async function minify(files, options, _fs_module) { ie8: false, keep_classnames: false, keep_fnames: false, + keep_imports: false, module: false, nth_identifier: base54, properties: false, diff --git a/lib/scope.js b/lib/scope.js index 951b35caf..6a49f2500 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -168,7 +168,8 @@ class SymbolDef { || this.orig[0] instanceof AST_SymbolDefun) && keep_name(options.keep_fnames, this.orig[0].name) || this.orig[0] instanceof AST_SymbolMethod || (this.orig[0] instanceof AST_SymbolClass - || this.orig[0] instanceof AST_SymbolDefClass) && keep_name(options.keep_classnames, this.orig[0].name); + || this.orig[0] instanceof AST_SymbolDefClass) && keep_name(options.keep_classnames, this.orig[0].name) + || (this.orig[0] instanceof AST_SymbolImport && options.keep_imports); } mangle(options) { const cache = options.cache && options.cache.props; diff --git a/test/compress/keep_imports.js b/test/compress/keep_imports.js new file mode 100644 index 000000000..6bc8923f4 --- /dev/null +++ b/test/compress/keep_imports.js @@ -0,0 +1,79 @@ +keep_imports_default: { + options = { + defaults: false + } + mangle = { + toplevel: true, + keep_imports: true, + } + input: { + import test from 'test' + } + expect: { + import test from 'test' + } +} + +keep_imports_default_wildcard: { + options = { + defaults: false + } + mangle = { + toplevel: true, + keep_imports: true, + } + input: { + import * as alias from 'test' + } + expect: { + import * as alias from 'test' + } +} + +keep_imports_named: { + options = { + defaults: false + } + mangle = { + toplevel: true, + keep_imports: true, + } + input: { + import {test} from 'test' + } + expect: { + import {test} from 'test' + } +} + +keep_imports_named_alias: { + options = { + defaults: false + } + mangle = { + toplevel: true, + keep_imports: true, + } + input: { + import {test as alias} from 'test' + } + expect: { + import {test as alias} from 'test' + } +} + +keep_imports_named_string: { + options = { + defaults: false + } + mangle = { + toplevel: true, + keep_imports: true, + } + input: { + import {"test"} from 'test' + } + expect: { + import {"test"} from 'test' + } +} \ No newline at end of file diff --git a/tools/terser.d.ts b/tools/terser.d.ts index 1b353e8f1..09e1f9b29 100644 --- a/tools/terser.d.ts +++ b/tools/terser.d.ts @@ -80,6 +80,7 @@ export interface MangleOptions { eval?: boolean; keep_classnames?: boolean | RegExp; keep_fnames?: boolean | RegExp; + keep_imports?: boolean; module?: boolean; nth_identifier?: SimpleIdentifierMangler | WeightedIdentifierMangler; properties?: boolean | ManglePropertiesOptions;