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

fix: fix minify when builder.sharedPlugins: true #19025

Merged
merged 4 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 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,69 @@ test('default sharedConfigBuild true on build api', async () => {
expect(counter).toBe(1)
})

test('minify per environment when builder.sharedPlugins', async () => {
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: true,
},
})
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,
),
).toMatchInlineSnapshot(`
[
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 @@ -482,7 +482,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,7 +317,7 @@ const rollupToEsbuildFormatMap: Record<
iife: undefined,
}

export const buildEsbuildPlugin = (config: ResolvedConfig): Plugin => {
export const buildEsbuildPlugin = (): Plugin => {
return {
name: 'vite:esbuild-transpile',
async renderChunk(code, chunk, opts) {
Expand All @@ -326,6 +326,10 @@ export const buildEsbuildPlugin = (config: ResolvedConfig): Plugin => {
return null
}

const config = this.environment.config
if (config.esbuild === false) {
return
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using applyToEnvironment hook?

const options = resolveEsbuildTranspileOptions(config, opts.format)

if (!options) {
Expand Down
Loading