Skip to content

Commit

Permalink
feat(optimizer): allow users to specify their esbuild platform opti…
Browse files Browse the repository at this point in the history
…on (#18611)

Co-authored-by: 翠 / green <[email protected]>
  • Loading branch information
dario-piotrowicz and sapphi-red authored Nov 8, 2024
1 parent 2c6d3db commit 0924879
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
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

0 comments on commit 0924879

Please sign in to comment.