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

feat(optimizer): allow users to specify their esbuild platform option #18611

Merged
merged 3 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion packages/vite/src/node/optimizer/esbuildDepPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ const matchesEntireLine = (text: string) => `^${escapeRegex(text)}$`
// https://github.com/evanw/esbuild/issues/566#issuecomment-735551834
export function esbuildCjsExternalPlugin(
externals: string[],
platform: 'node' | 'browser',
platform: 'node' | 'browser' | 'neutral',
): Plugin {
return {
name: 'cjs-external',
Expand Down
13 changes: 9 additions & 4 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,15 @@ async function prepareEsbuildOptimizerRun(
),
}

const platform = environment.config.consumer === 'client' ? 'browser' : 'node'
const platform =
optimizeDeps.esbuildOptions?.platform ??
// We generally don't want to use platform 'neutral', as esbuild has custom handling
// when the platform is 'node' or 'browser' that can't be emulated by using mainFields
// and conditions
(environment.config.consumer === 'client' ||
environment.config.ssr.target === 'webworker'
? 'browser'
: 'node')

const external = [...(optimizeDeps?.exclude ?? [])]

Expand All @@ -775,9 +783,6 @@ async function prepareEsbuildOptimizerRun(
absWorkingDir: process.cwd(),
entryPoints: Object.keys(flatIdDeps),
bundle: true,
// We can't use platform 'neutral', as esbuild has custom handling
// when the platform is 'node' or 'browser' that can't be emulated
// by using mainFields and conditions
platform,
define,
format: 'esm',
Expand Down