From f7b1964d3a93a21f80b61638fa6ae9606d0a6f4f Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Thu, 26 Dec 2024 11:24:20 +0900 Subject: [PATCH] fix: fix `minify` when `builder.sharedPlugins: true` (#19025) --- .../vite/src/node/__tests__/build.spec.ts | 59 +++++++++++++++++++ .../fixtures/shared-plugins/minify/entry.js | 4 ++ packages/vite/src/node/build.ts | 2 +- packages/vite/src/node/plugins/esbuild.ts | 6 +- pnpm-lock.yaml | 20 ------- 5 files changed, 69 insertions(+), 22 deletions(-) create mode 100644 packages/vite/src/node/__tests__/fixtures/shared-plugins/minify/entry.js diff --git a/packages/vite/src/node/__tests__/build.spec.ts b/packages/vite/src/node/__tests__/build.spec.ts index cd6bd6e42c4a5d..3c4be301e6d528 100644 --- a/packages/vite/src/node/__tests__/build.spec.ts +++ b/packages/vite/src/node/__tests__/build.spec.ts @@ -733,6 +733,65 @@ test('default sharedConfigBuild true on build api', async () => { expect(counter).toBe(1) }) +test.for([true, false])( + 'minify per environment (builder.sharedPlugins: %s)', + async (sharedPlugins) => { + const root = resolve(__dirname, 'fixtures/shared-plugins/minify') + const builder = await createBuilder({ + root, + logLevel: 'warn', + environments: { + client: { + build: { + outDir: './dist/client', + rollupOptions: { + input: '/entry.js', + }, + }, + }, + ssr: { + build: { + outDir: './dist/server', + rollupOptions: { + input: '/entry.js', + }, + }, + }, + custom1: { + build: { + minify: true, + outDir: './dist/custom1', + rollupOptions: { + input: '/entry.js', + }, + }, + }, + custom2: { + build: { + minify: false, + outDir: './dist/custom2', + rollupOptions: { + input: '/entry.js', + }, + }, + }, + }, + builder: { + sharedPlugins, + }, + }) + const client = await builder.build(builder.environments.client) + const ssr = await builder.build(builder.environments.ssr) + const custom1 = await builder.build(builder.environments.custom1) + const custom2 = await builder.build(builder.environments.custom2) + expect( + ([client, ssr, custom1, custom2] as RollupOutput[]).map( + (o) => o.output[0].code.split('\n').length, + ), + ).toEqual([2, 5, 2, 5]) + }, +) + test('adjust worker build error for worker.format', async () => { try { await build({ diff --git a/packages/vite/src/node/__tests__/fixtures/shared-plugins/minify/entry.js b/packages/vite/src/node/__tests__/fixtures/shared-plugins/minify/entry.js new file mode 100644 index 00000000000000..2aaffb41b7db8c --- /dev/null +++ b/packages/vite/src/node/__tests__/fixtures/shared-plugins/minify/entry.js @@ -0,0 +1,4 @@ +function main() { + console.log('test') +} +main() diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 2eae8a28fac705..ea19a35d95bf55 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -481,7 +481,7 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{ ], post: [ buildImportAnalysisPlugin(config), - ...(config.esbuild !== false ? [buildEsbuildPlugin(config)] : []), + buildEsbuildPlugin(), terserPlugin(config), ...(!config.isWorker ? [manifestPlugin(), ssrManifestPlugin(), buildReporterPlugin(config)] diff --git a/packages/vite/src/node/plugins/esbuild.ts b/packages/vite/src/node/plugins/esbuild.ts index 54e0f5df4963a5..83fa1af4248ab5 100644 --- a/packages/vite/src/node/plugins/esbuild.ts +++ b/packages/vite/src/node/plugins/esbuild.ts @@ -317,15 +317,19 @@ const rollupToEsbuildFormatMap: Record< iife: undefined, } -export const buildEsbuildPlugin = (config: ResolvedConfig): Plugin => { +export const buildEsbuildPlugin = (): Plugin => { return { name: 'vite:esbuild-transpile', + applyToEnvironment(environment) { + return environment.config.esbuild !== false + }, async renderChunk(code, chunk, opts) { // @ts-expect-error injected by @vitejs/plugin-legacy if (opts.__vite_skip_esbuild__) { return null } + const config = this.environment.config const options = resolveEsbuildTranspileOptions(config, opts.format) if (!options) { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ee0b75548ec8c1..594be74e4ee7fe 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2907,42 +2907,36 @@ packages: engines: {node: '>= 10.0.0'} cpu: [arm] os: [linux] - libc: [glibc] '@parcel/watcher-linux-arm-musl@2.5.0': resolution: {integrity: sha512-6uHywSIzz8+vi2lAzFeltnYbdHsDm3iIB57d4g5oaB9vKwjb6N6dRIgZMujw4nm5r6v9/BQH0noq6DzHrqr2pA==} engines: {node: '>= 10.0.0'} cpu: [arm] os: [linux] - libc: [musl] '@parcel/watcher-linux-arm64-glibc@2.5.0': resolution: {integrity: sha512-BfNjXwZKxBy4WibDb/LDCriWSKLz+jJRL3cM/DllnHH5QUyoiUNEp3GmL80ZqxeumoADfCCP19+qiYiC8gUBjA==} engines: {node: '>= 10.0.0'} cpu: [arm64] os: [linux] - libc: [glibc] '@parcel/watcher-linux-arm64-musl@2.5.0': resolution: {integrity: sha512-S1qARKOphxfiBEkwLUbHjCY9BWPdWnW9j7f7Hb2jPplu8UZ3nes7zpPOW9bkLbHRvWM0WDTsjdOTUgW0xLBN1Q==} engines: {node: '>= 10.0.0'} cpu: [arm64] os: [linux] - libc: [musl] '@parcel/watcher-linux-x64-glibc@2.5.0': resolution: {integrity: sha512-d9AOkusyXARkFD66S6zlGXyzx5RvY+chTP9Jp0ypSTC9d4lzyRs9ovGf/80VCxjKddcUvnsGwCHWuF2EoPgWjw==} engines: {node: '>= 10.0.0'} cpu: [x64] os: [linux] - libc: [glibc] '@parcel/watcher-linux-x64-musl@2.5.0': resolution: {integrity: sha512-iqOC+GoTDoFyk/VYSFHwjHhYrk8bljW6zOhPuhi5t9ulqiYq1togGJB5e3PwYVFFfeVgc6pbz3JdQyDoBszVaA==} engines: {node: '>= 10.0.0'} cpu: [x64] os: [linux] - libc: [musl] '@parcel/watcher-win32-arm64@2.5.0': resolution: {integrity: sha512-twtft1d+JRNkM5YbmexfcH/N4znDtjgysFaV9zvZmmJezQsKpkfLYJ+JFV3uygugK6AtIM2oADPkB2AdhBrNig==} @@ -3083,61 +3077,51 @@ packages: resolution: {integrity: sha512-QAg11ZIt6mcmzpNE6JZBpKfJaKkqTm1A9+y9O+frdZJEuhQxiugM05gnCWiANHj4RmbgeVJpTdmKRmH/a+0QbA==} cpu: [arm] os: [linux] - libc: [glibc] '@rollup/rollup-linux-arm-musleabihf@4.28.1': resolution: {integrity: sha512-dRP9PEBfolq1dmMcFqbEPSd9VlRuVWEGSmbxVEfiq2cs2jlZAl0YNxFzAQS2OrQmsLBLAATDMb3Z6MFv5vOcXg==} cpu: [arm] os: [linux] - libc: [musl] '@rollup/rollup-linux-arm64-gnu@4.28.1': resolution: {integrity: sha512-uGr8khxO+CKT4XU8ZUH1TTEUtlktK6Kgtv0+6bIFSeiSlnGJHG1tSFSjm41uQ9sAO/5ULx9mWOz70jYLyv1QkA==} cpu: [arm64] os: [linux] - libc: [glibc] '@rollup/rollup-linux-arm64-musl@4.28.1': resolution: {integrity: sha512-QF54q8MYGAqMLrX2t7tNpi01nvq5RI59UBNx+3+37zoKX5KViPo/gk2QLhsuqok05sSCRluj0D00LzCwBikb0A==} cpu: [arm64] os: [linux] - libc: [musl] '@rollup/rollup-linux-loongarch64-gnu@4.28.1': resolution: {integrity: sha512-vPul4uodvWvLhRco2w0GcyZcdyBfpfDRgNKU+p35AWEbJ/HPs1tOUrkSueVbBS0RQHAf/A+nNtDpvw95PeVKOA==} cpu: [loong64] os: [linux] - libc: [glibc] '@rollup/rollup-linux-powerpc64le-gnu@4.28.1': resolution: {integrity: sha512-pTnTdBuC2+pt1Rmm2SV7JWRqzhYpEILML4PKODqLz+C7Ou2apEV52h19CR7es+u04KlqplggmN9sqZlekg3R1A==} cpu: [ppc64] os: [linux] - libc: [glibc] '@rollup/rollup-linux-riscv64-gnu@4.28.1': resolution: {integrity: sha512-vWXy1Nfg7TPBSuAncfInmAI/WZDd5vOklyLJDdIRKABcZWojNDY0NJwruY2AcnCLnRJKSaBgf/GiJfauu8cQZA==} cpu: [riscv64] os: [linux] - libc: [glibc] '@rollup/rollup-linux-s390x-gnu@4.28.1': resolution: {integrity: sha512-/yqC2Y53oZjb0yz8PVuGOQQNOTwxcizudunl/tFs1aLvObTclTwZ0JhXF2XcPT/zuaymemCDSuuUPXJJyqeDOg==} cpu: [s390x] os: [linux] - libc: [glibc] '@rollup/rollup-linux-x64-gnu@4.28.1': resolution: {integrity: sha512-fzgeABz7rrAlKYB0y2kSEiURrI0691CSL0+KXwKwhxvj92VULEDQLpBYLHpF49MSiPG4sq5CK3qHMnb9tlCjBw==} cpu: [x64] os: [linux] - libc: [glibc] '@rollup/rollup-linux-x64-musl@4.28.1': resolution: {integrity: sha512-xQTDVzSGiMlSshpJCtudbWyRfLaNiVPXt1WgdWTwWz9n0U12cI2ZVtWe/Jgwyv/6wjL7b66uu61Vg0POWVfz4g==} cpu: [x64] os: [linux] - libc: [musl] '@rollup/rollup-win32-arm64-msvc@4.28.1': resolution: {integrity: sha512-wSXmDRVupJstFP7elGMgv+2HqXelQhuNf+IS4V+nUpNVi/GUiBgDmfwD0UGN3pcAnWsgKG3I52wMOBnk1VHr/A==} @@ -5357,28 +5341,24 @@ packages: engines: {node: '>= 12.0.0'} cpu: [arm64] os: [linux] - libc: [glibc] lightningcss-linux-arm64-musl@1.28.2: resolution: {integrity: sha512-1SPG1ZTNnphWvAv8RVOymlZ8BDtAg69Hbo7n4QxARvkFVCJAt0cgjAw1Fox0WEhf4PwnyoOBaVH0Z5YNgzt4dA==} engines: {node: '>= 12.0.0'} cpu: [arm64] os: [linux] - libc: [musl] lightningcss-linux-x64-gnu@1.28.2: resolution: {integrity: sha512-ZhQy0FcO//INWUdo/iEdbefntTdpPVQ0XJwwtdbBuMQe+uxqZoytm9M+iqR9O5noWFaxK+nbS2iR/I80Q2Ofpg==} engines: {node: '>= 12.0.0'} cpu: [x64] os: [linux] - libc: [glibc] lightningcss-linux-x64-musl@1.28.2: resolution: {integrity: sha512-alb/j1NMrgQmSFyzTbN1/pvMPM+gdDw7YBuQ5VSgcFDypN3Ah0BzC2dTZbzwzaMdUVDszX6zH5MzjfVN1oGuww==} engines: {node: '>= 12.0.0'} cpu: [x64] os: [linux] - libc: [musl] lightningcss-win32-arm64-msvc@1.28.2: resolution: {integrity: sha512-WnwcjcBeAt0jGdjlgbT9ANf30pF0C/QMb1XnLnH272DQU8QXh+kmpi24R55wmWBwaTtNAETZ+m35ohyeMiNt+g==}