Skip to content

Commit

Permalink
fix: fix minify when builder.sharedPlugins: true (#19025)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Dec 26, 2024
1 parent 5c2b4a0 commit f7b1964
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 22 deletions.
59 changes: 59 additions & 0 deletions packages/vite/src/node/__tests__/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function main() {
console.log('test')
}
main()
2 changes: 1 addition & 1 deletion packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
6 changes: 5 additions & 1 deletion packages/vite/src/node/plugins/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
20 changes: 0 additions & 20 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f7b1964

Please sign in to comment.