diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index 1ef86c6aad9b2f..d9b5d4897d667e 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -533,15 +533,30 @@ export async function runOptimizeDeps( flatIdToExports[flatId] = exportsData } + // esbuild automatically replaces process.env.NODE_ENV for platform 'browser' + // In lib mode, we need to keep process.env.NODE_ENV untouched, so to at build + // time we replace it by __vite_process_env_NODE_ENV. This placeholder will be + // later replaced by the define plugin + const define = { + 'process.env.NODE_ENV': isBuild + ? '__vite_process_env_NODE_ENV' + : JSON.stringify(process.env.NODE_ENV || config.mode) + } + const start = performance.now() const result = await build({ absWorkingDir: process.cwd(), entryPoints: Object.keys(flatIdDeps), bundle: true, - // Ensure resolution is handled by esbuildDepPlugin and - // avoid replacing `process.env.NODE_ENV` for 'browser' - platform: 'neutral', + // 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: + config.build.ssr && config.ssr?.target !== 'webworker' + ? 'node' + : 'browser', + define, format: 'esm', target: config.build.target || undefined, external: config.optimizeDeps?.exclude, diff --git a/packages/vite/src/node/plugins/define.ts b/packages/vite/src/node/plugins/define.ts index 6edb1bd8858a11..9859676e693a23 100644 --- a/packages/vite/src/node/plugins/define.ts +++ b/packages/vite/src/node/plugins/define.ts @@ -25,7 +25,8 @@ export function definePlugin(config: ResolvedConfig): Plugin { Object.assign(processNodeEnv, { 'process.env.NODE_ENV': JSON.stringify(nodeEnv), 'global.process.env.NODE_ENV': JSON.stringify(nodeEnv), - 'globalThis.process.env.NODE_ENV': JSON.stringify(nodeEnv) + 'globalThis.process.env.NODE_ENV': JSON.stringify(nodeEnv), + __vite_process_env_NODE_ENV: JSON.stringify(nodeEnv) }) } @@ -65,6 +66,10 @@ export function definePlugin(config: ResolvedConfig): Plugin { ...(replaceProcessEnv ? processEnv : {}) } + if (isBuild && !replaceProcessEnv) { + replacements['__vite_process_env_NODE_ENV'] = 'process.env.NODE_ENV' + } + const replacementsKeys = Object.keys(replacements) const pattern = replacementsKeys.length ? new RegExp( diff --git a/playground/optimize-deps/__tests__/optimize-deps.spec.ts b/playground/optimize-deps/__tests__/optimize-deps.spec.ts index cc712fbbdc4a09..df1713b3fc76f8 100644 --- a/playground/optimize-deps/__tests__/optimize-deps.spec.ts +++ b/playground/optimize-deps/__tests__/optimize-deps.spec.ts @@ -77,6 +77,10 @@ test('import from dep with .notjs files', async () => { expect(await page.textContent('.not-js')).toMatch(`[success]`) }) +test('Import from dependency which uses relative path which needs to be resolved by main field', async () => { + expect(await page.textContent('.relative-to-main')).toMatch(`[success]`) +}) + test('dep with dynamic import', async () => { expect(await page.textContent('.dep-with-dynamic-import')).toMatch( `[success]` diff --git a/playground/optimize-deps/dep-relative-to-main/entry.js b/playground/optimize-deps/dep-relative-to-main/entry.js new file mode 100644 index 00000000000000..d70a1adf8fee78 --- /dev/null +++ b/playground/optimize-deps/dep-relative-to-main/entry.js @@ -0,0 +1 @@ +module.exports = require('./') diff --git a/playground/optimize-deps/dep-relative-to-main/lib/main.js b/playground/optimize-deps/dep-relative-to-main/lib/main.js new file mode 100644 index 00000000000000..d27286071c483f --- /dev/null +++ b/playground/optimize-deps/dep-relative-to-main/lib/main.js @@ -0,0 +1 @@ +module.exports = '[success] imported from main' diff --git a/playground/optimize-deps/dep-relative-to-main/package.json b/playground/optimize-deps/dep-relative-to-main/package.json new file mode 100644 index 00000000000000..bc11dc9397ed36 --- /dev/null +++ b/playground/optimize-deps/dep-relative-to-main/package.json @@ -0,0 +1,6 @@ +{ + "name": "dep-relative-to-main", + "private": true, + "version": "1.0.0", + "main": "lib/main.js" +} diff --git a/playground/optimize-deps/index.html b/playground/optimize-deps/index.html index 00cf4c48e6e9e6..7b0c43e82fdcbc 100644 --- a/playground/optimize-deps/index.html +++ b/playground/optimize-deps/index.html @@ -50,6 +50,12 @@