Skip to content

Commit f7b1964

Browse files
authored
fix: fix minify when builder.sharedPlugins: true (#19025)
1 parent 5c2b4a0 commit f7b1964

File tree

5 files changed

+69
-22
lines changed

5 files changed

+69
-22
lines changed

Diff for: packages/vite/src/node/__tests__/build.spec.ts

+59
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,65 @@ test('default sharedConfigBuild true on build api', async () => {
733733
expect(counter).toBe(1)
734734
})
735735

736+
test.for([true, false])(
737+
'minify per environment (builder.sharedPlugins: %s)',
738+
async (sharedPlugins) => {
739+
const root = resolve(__dirname, 'fixtures/shared-plugins/minify')
740+
const builder = await createBuilder({
741+
root,
742+
logLevel: 'warn',
743+
environments: {
744+
client: {
745+
build: {
746+
outDir: './dist/client',
747+
rollupOptions: {
748+
input: '/entry.js',
749+
},
750+
},
751+
},
752+
ssr: {
753+
build: {
754+
outDir: './dist/server',
755+
rollupOptions: {
756+
input: '/entry.js',
757+
},
758+
},
759+
},
760+
custom1: {
761+
build: {
762+
minify: true,
763+
outDir: './dist/custom1',
764+
rollupOptions: {
765+
input: '/entry.js',
766+
},
767+
},
768+
},
769+
custom2: {
770+
build: {
771+
minify: false,
772+
outDir: './dist/custom2',
773+
rollupOptions: {
774+
input: '/entry.js',
775+
},
776+
},
777+
},
778+
},
779+
builder: {
780+
sharedPlugins,
781+
},
782+
})
783+
const client = await builder.build(builder.environments.client)
784+
const ssr = await builder.build(builder.environments.ssr)
785+
const custom1 = await builder.build(builder.environments.custom1)
786+
const custom2 = await builder.build(builder.environments.custom2)
787+
expect(
788+
([client, ssr, custom1, custom2] as RollupOutput[]).map(
789+
(o) => o.output[0].code.split('\n').length,
790+
),
791+
).toEqual([2, 5, 2, 5])
792+
},
793+
)
794+
736795
test('adjust worker build error for worker.format', async () => {
737796
try {
738797
await build({
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function main() {
2+
console.log('test')
3+
}
4+
main()

Diff for: packages/vite/src/node/build.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{
481481
],
482482
post: [
483483
buildImportAnalysisPlugin(config),
484-
...(config.esbuild !== false ? [buildEsbuildPlugin(config)] : []),
484+
buildEsbuildPlugin(),
485485
terserPlugin(config),
486486
...(!config.isWorker
487487
? [manifestPlugin(), ssrManifestPlugin(), buildReporterPlugin(config)]

Diff for: packages/vite/src/node/plugins/esbuild.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -317,15 +317,19 @@ const rollupToEsbuildFormatMap: Record<
317317
iife: undefined,
318318
}
319319

320-
export const buildEsbuildPlugin = (config: ResolvedConfig): Plugin => {
320+
export const buildEsbuildPlugin = (): Plugin => {
321321
return {
322322
name: 'vite:esbuild-transpile',
323+
applyToEnvironment(environment) {
324+
return environment.config.esbuild !== false
325+
},
323326
async renderChunk(code, chunk, opts) {
324327
// @ts-expect-error injected by @vitejs/plugin-legacy
325328
if (opts.__vite_skip_esbuild__) {
326329
return null
327330
}
328331

332+
const config = this.environment.config
329333
const options = resolveEsbuildTranspileOptions(config, opts.format)
330334

331335
if (!options) {

Diff for: pnpm-lock.yaml

-20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)