From f2cfa6838cee03df48c3fa93526e8991bf08e27a Mon Sep 17 00:00:00 2001 From: Tobias Trumm Date: Fri, 22 Nov 2024 15:17:32 +0100 Subject: [PATCH] fix: Allow enabling esbuild.minifyWhitespace for es format in lib mode (fix #6555) To produce es bundles that can be tree-shaken, lib mode disables minifyWhitespace by default when using the es format. But there are cases where we want an es bundle that is as small as possible and that does not need to be tree-shakable. This change allows minifyWhitespace to be re-enabled if the user so desires. --- docs/config/build-options.md | 2 +- packages/vite/src/node/__tests__/plugins/esbuild.spec.ts | 2 +- packages/vite/src/node/plugins/esbuild.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/config/build-options.md b/docs/config/build-options.md index 3419070bd474a6..7d8d6fdaceb619 100644 --- a/docs/config/build-options.md +++ b/docs/config/build-options.md @@ -230,7 +230,7 @@ During the SSR build, static assets aren't emitted as it is assumed they would b 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 does not minify whitespaces when using the `'es'` format in lib mode, as it removes pure annotations and breaks tree-shaking. +Note the `build.minify` option does not minify whitespaces when using the `'es'` format in lib mode, as it removes pure annotations and breaks tree-shaking. To force whitespace minification for `'es'` format in lib mode, explicitly set `esbuild.minifyWhitespace` to `true`. Terser must be installed when it is set to `'terser'`. diff --git a/packages/vite/src/node/__tests__/plugins/esbuild.spec.ts b/packages/vite/src/node/__tests__/plugins/esbuild.spec.ts index 936415f9c33826..ae16fbe45b65f7 100644 --- a/packages/vite/src/node/__tests__/plugins/esbuild.spec.ts +++ b/packages/vite/src/node/__tests__/plugins/esbuild.spec.ts @@ -201,7 +201,7 @@ describe('resolveEsbuildTranspileOptions', () => { minify: false, minifyIdentifiers: true, minifySyntax: true, - minifyWhitespace: false, + minifyWhitespace: true, treeShaking: true, supported: { 'dynamic-import': true, diff --git a/packages/vite/src/node/plugins/esbuild.ts b/packages/vite/src/node/plugins/esbuild.ts index 6d3ffc86f33b06..c2f2b14525478f 100644 --- a/packages/vite/src/node/plugins/esbuild.ts +++ b/packages/vite/src/node/plugins/esbuild.ts @@ -426,7 +426,7 @@ export function resolveEsbuildTranspileOptions( minify: false, minifyIdentifiers: options.minifyIdentifiers ?? true, minifySyntax: options.minifySyntax ?? true, - minifyWhitespace: false, + minifyWhitespace: options.minifyWhitespace ?? false, treeShaking: true, } } else {