Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: respect esbuild minify config #8754

Merged
merged 10 commits into from Jun 25, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/config/build-options.md
Expand Up @@ -145,7 +145,7 @@ Produce SSR-oriented build. The value can be a string to directly specify the SS

Set to `false` to disable minification, or specify the minifier to use. The default is [esbuild](https://github.com/evanw/esbuild) which is 20 ~ 40x faster than terser and only 1 ~ 2% worse compression. [Benchmarks](https://github.com/privatenumber/minification-benchmarks)

Note the `build.minify` option is not available when using the `'es'` format in lib mode.
Note the `build.minify` option does not minify whitespaces when using the `'es'` format in lib mode, as it removes pure annotations and break tree-shaking.

Terser must be installed when it is set to `'terser'`.

Expand Down
2 changes: 2 additions & 0 deletions docs/config/shared-options.md
Expand Up @@ -280,6 +280,8 @@ export default defineConfig({
})
```

When `build.minify` is `true`, you can configure to only minify certain aspects of the code by setting either of `esbuild.minifyIdentifiers`, `esbuild.minifySyntax`, and `esbuild.minifyWhitespace` to `true`. Note the `esbuild.minify` option can't be used to override `build.minify`.
bluwy marked this conversation as resolved.
Show resolved Hide resolved

Set to `false` to disable esbuild transforms.

## assetsInclude
Expand Down
76 changes: 63 additions & 13 deletions packages/vite/src/node/plugins/esbuild.ts
Expand Up @@ -37,6 +37,10 @@ export interface ESBuildOptions extends TransformOptions {
include?: string | RegExp | string[] | RegExp[]
exclude?: string | RegExp | string[] | RegExp[]
jsxInject?: string
/**
* This option is not respected. Use `build.minify` instead.
*/
minify?: never
}

export type ESBuildTransformResult = Omit<TransformResult, 'map'> & {
Expand Down Expand Up @@ -170,6 +174,17 @@ export function esbuildPlugin(options: ESBuildOptions = {}): Plugin {
options.exclude || /\.js$/
)

// Remove optimization options for dev as we only need to transpile them,
// and for build as the final optimization is in `buildEsbuildPlugin`
const transformOptions: TransformOptions = {
...options,
minify: false,
minifyIdentifiers: false,
minifySyntax: false,
minifyWhitespace: false,
treeShaking: false
}

return {
name: 'vite:esbuild',
configureServer(_server) {
Expand All @@ -188,7 +203,7 @@ export function esbuildPlugin(options: ESBuildOptions = {}): Plugin {
},
async transform(code, id) {
if (filter(id) || filter(cleanUrl(id))) {
const result = await transformWithEsbuild(code, id, options)
const result = await transformWithEsbuild(code, id, transformOptions)
if (result.warnings.length) {
result.warnings.forEach((m) => {
this.warn(prettifyMessage(m, code))
Expand Down Expand Up @@ -243,20 +258,55 @@ export const buildEsbuildPlugin = (config: ResolvedConfig): Plugin => {
return null
}

const res = await transformWithEsbuild(code, chunk.fileName, {
const isEsLibBuild = config.build.lib && opts.format === 'es'
let options: TransformOptions = {
...config.esbuild,
target: target || undefined,
...(minify
? {
// Do not minify ES lib output since that would remove pure annotations
// and break tree-shaking
// https://github.com/vuejs/core/issues/2860#issuecomment-926882793
minify: !(config.build.lib && opts.format === 'es'),
treeShaking: true,
format: rollupToEsbuildFormatMap[opts.format]
}
: undefined)
})
format: rollupToEsbuildFormatMap[opts.format]
bluwy marked this conversation as resolved.
Show resolved Hide resolved
}

if (minify) {
// If user enable fine-grain minify options, minify with their options instead
if (
options.minifyIdentifiers === true ||
options.minifySyntax === true ||
options.minifyWhitespace === true
bluwy marked this conversation as resolved.
Show resolved Hide resolved
) {
options = { ...options, treeShaking: true }
// Do not minify whitespace for ES lib output since that would remove
// pure annotations and break tree-shaking
// https://github.com/vuejs/core/issues/2860#issuecomment-926882793
if (isEsLibBuild) {
options = { ...options, minifyWhitespace: true }
bluwy marked this conversation as resolved.
Show resolved Hide resolved
}
} else if (isEsLibBuild) {
// Enable all minify except whitespace, which doesn't work
options = {
...options,
minify: false,
minifyIdentifiers: true,
minifySyntax: true,
treeShaking: true
}
} else {
options = {
...options,
minify: true,
treeShaking: true
}
}
} else {
options = {
...options,
minify: false,
minifyIdentifiers: false,
minifySyntax: false,
minifyWhitespace: false,
treeShaking: false
}
}
bluwy marked this conversation as resolved.
Show resolved Hide resolved

const res = await transformWithEsbuild(code, chunk.fileName, options)

if (config.build.lib) {
// #7188, esbuild adds helpers out of the UMD and IIFE wrappers, and the
Expand Down