Skip to content

Commit

Permalink
fix(lib-mode): do not minify lib mode es output
Browse files Browse the repository at this point in the history
Otherwise it breaks library tree-shaking
vuejs/core#2860 (comment)
  • Loading branch information
yyx990803 committed Sep 24, 2021
1 parent ba0c42c commit 06d86e4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
4 changes: 1 addition & 3 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,7 @@ export function resolveBuildPlugins(config: ResolvedConfig): {
post: [
buildImportAnalysisPlugin(config),
buildEsbuildPlugin(config),
...(options.minify === 'terser'
? [terserPlugin(options.terserOptions)]
: []),
...(options.minify === 'terser' ? [terserPlugin(config)] : []),
...(options.manifest ? [manifestPlugin(config)] : []),
...(options.ssrManifest ? [ssrManifestPlugin(config)] : []),
buildReporterPlugin(config),
Expand Down
8 changes: 7 additions & 1 deletion packages/vite/src/node/plugins/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,13 @@ export const buildEsbuildPlugin = (config: ResolvedConfig): Plugin => {
}

const target = config.build.target
const minify = config.build.minify === 'esbuild'
const minify =
config.build.minify === 'esbuild' &&
// Do not minify ES lib output since that would remove pure annotations
// and break tree-shaking
// https://github.com/vuejs/vue-next/issues/2860#issuecomment-926882793
!(config.build.lib && opts.format === 'es')

if ((!target || target === 'esnext') && !minify) {
return null
}
Expand Down
11 changes: 9 additions & 2 deletions packages/vite/src/node/plugins/terser.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Plugin } from '../plugin'
import { Worker } from 'okie'
import { Terser } from 'types/terser'
import { ResolvedConfig } from '..'

export function terserPlugin(options: Terser.MinifyOptions): Plugin {
export function terserPlugin(config: ResolvedConfig): Plugin {
const worker = new Worker(
(basedir: string, code: string, options: Terser.MinifyOptions) => {
// when vite is linked, the worker thread won't share the same resolve
Expand All @@ -20,9 +21,15 @@ export function terserPlugin(options: Terser.MinifyOptions): Plugin {
name: 'vite:terser',

async renderChunk(code, _chunk, outputOptions) {
// Do not minify ES lib output since that would remove pure annotations
// and break tree-shaking
if (config.build.lib && outputOptions.format === 'es') {
return null
}

const res = await worker.run(__dirname, code, {
safari10: true,
...options,
...config.build.terserOptions,
sourceMap: !!outputOptions.sourcemap,
module: outputOptions.format.startsWith('es'),
toplevel: outputOptions.format === 'cjs'
Expand Down

0 comments on commit 06d86e4

Please sign in to comment.