Skip to content

Commit

Permalink
feat: minify with swc instead of terser
Browse files Browse the repository at this point in the history
  • Loading branch information
CyanSalt committed Feb 8, 2023
1 parent 607feda commit 72c3791
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ export default {
}
```

Terser must be installed because plugin-legacy uses Terser for minification.

```sh
npm add -D terser
```

## Options

### `targets`
Expand Down
28 changes: 18 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createHash } from 'crypto'
import path from 'path'
import type {
EnvConfig,
JsMinifyOptions,
Options as SwcOptions,
Plugin as SwcPlugin,
Statement,
Expand Down Expand Up @@ -276,8 +277,6 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
imports: legacyPolyfills,
bundle,
facadeToChunkMap: facadeToLegacyPolyfillMap,
// force using terser for legacy polyfill minification, since esbuild
// isn't legacy-safe
buildOptions: resolvedConfig.build,
format: 'iife',
rollupOutputOptions: opts,
Expand Down Expand Up @@ -415,11 +414,6 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
// legacy-unsafe code - e.g. rewriting object properties into shorthands
opts.__vite_skip_esbuild__ = true

// @ts-expect-error force terser for legacy chunks. This only takes effect if
// minification isn't disabled, because that leaves out the terser plugin
// entirely.
opts.__vite_force_terser__ = true

// @ts-expect-error In the `generateBundle` hook,
// we'll delete the assets from the legacy bundle to avoid emitting duplicate assets.
// But that's still a waste of computing resource.
Expand Down Expand Up @@ -448,6 +442,20 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
},
},
},
minify: {
compress: {
// Different defaults between terser and swc
dead_code: true,
keep_fargs: true,
passes: 1,
},
mangle: true,
safari10: true,
...resolvedConfig.build.terserOptions as JsMinifyOptions,
sourceMap: Boolean(opts.sourcemap),
module: opts.format.startsWith('es'),
toplevel: opts.format === 'cjs',
},
},
}
const transformResult = await swc.transform(raw, {
Expand All @@ -462,6 +470,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
const result = await swc.print(plugin(ast), {
...swcOptions,
inputSourceMap: transformResult.map,
minify: Boolean(resolvedConfig.build.minify),
})

return result
Expand Down Expand Up @@ -675,8 +684,7 @@ async function buildPolyfillChunk({
rollupOutputOptions: NormalizedOutputOptions,
excludeSystemJS?: boolean,
}) {
let { minify, assetsDir } = buildOptions
minify = minify ? 'terser' : false
let { assetsDir } = buildOptions
const res = await build({
mode,
// so that everything is resolved from here
Expand All @@ -686,7 +694,7 @@ async function buildPolyfillChunk({
plugins: [polyfillsPlugin(imports, excludeSystemJS)],
build: {
write: false,
minify,
minify: false,
assetsDir,
rollupOptions: {
input: {
Expand Down

0 comments on commit 72c3791

Please sign in to comment.