diff --git a/packages/vite/src/node/plugins/define.ts b/packages/vite/src/node/plugins/define.ts index ca0f446cc1f58e..6edb1bd8858a11 100644 --- a/packages/vite/src/node/plugins/define.ts +++ b/packages/vite/src/node/plugins/define.ts @@ -10,15 +10,23 @@ const isNonJsRequest = (request: string): boolean => nonJsRe.test(request) export function definePlugin(config: ResolvedConfig): Plugin { const isBuild = config.command === 'build' - - const processNodeEnv: Record = { - 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || config.mode), - 'global.process.env.NODE_ENV': JSON.stringify( - process.env.NODE_ENV || config.mode - ), - 'globalThis.process.env.NODE_ENV': JSON.stringify( - process.env.NODE_ENV || config.mode - ) + const isBuildLib = isBuild && config.build.lib + + // ignore replace process.env in lib build + const processEnv: Record = {} + const processNodeEnv: Record = {} + if (!isBuildLib) { + const nodeEnv = process.env.NODE_ENV || config.mode + Object.assign(processEnv, { + 'process.env.': `({}).`, + 'global.process.env.': `({}).`, + 'globalThis.process.env.': `({}).` + }) + 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) + }) } const userDefine: Record = {} @@ -27,7 +35,8 @@ export function definePlugin(config: ResolvedConfig): Plugin { userDefine[key] = typeof val === 'string' ? val : JSON.stringify(val) } - // during dev, import.meta properties are handled by importAnalysis plugin + // during dev, import.meta properties are handled by importAnalysis plugin. + // ignore replace import.meta.env in lib build const importMetaKeys: Record = {} if (isBuild) { const env: Record = { @@ -47,22 +56,13 @@ export function definePlugin(config: ResolvedConfig): Plugin { function generatePattern( ssr: boolean ): [Record, RegExp | null] { - const processEnv: Record = {} - const isNeedProcessEnv = !ssr || config.ssr?.target === 'webworker' - - if (isNeedProcessEnv) { - Object.assign(processEnv, { - 'process.env.': `({}).`, - 'global.process.env.': `({}).`, - 'globalThis.process.env.': `({}).` - }) - } + const replaceProcessEnv = !ssr || config.ssr?.target === 'webworker' const replacements: Record = { - ...(isNeedProcessEnv ? processNodeEnv : {}), + ...(replaceProcessEnv ? processNodeEnv : {}), ...userDefine, ...importMetaKeys, - ...processEnv + ...(replaceProcessEnv ? processEnv : {}) } const replacementsKeys = Object.keys(replacements) diff --git a/playground/lib/__tests__/lib.spec.ts b/playground/lib/__tests__/lib.spec.ts index 32c6edba1bdeb9..9046097f7e8375 100644 --- a/playground/lib/__tests__/lib.spec.ts +++ b/playground/lib/__tests__/lib.spec.ts @@ -1,11 +1,9 @@ -import path from 'path' -import fs from 'fs' import { isBuild, isServe, page, + readFile, serverLogs, - testDir, untilUpdated } from '~utils' @@ -16,20 +14,14 @@ describe.runIf(isBuild)('build', () => { test('umd', async () => { expect(await page.textContent('.umd')).toBe('It works') - const code = fs.readFileSync( - path.join(testDir, 'dist/my-lib-custom-filename.umd.js'), - 'utf-8' - ) + const code = readFile('dist/my-lib-custom-filename.umd.js') // esbuild helpers are injected inside of the UMD wrapper expect(code).toMatch(/^\(function\(/) }) test('iife', async () => { expect(await page.textContent('.iife')).toBe('It works') - const code = fs.readFileSync( - path.join(testDir, 'dist/my-lib-custom-filename.iife.js'), - 'utf-8' - ) + const code = readFile('dist/my-lib-custom-filename.iife.js') // esbuild helpers are injected inside of the IIFE wrapper expect(code).toMatch(/^const MyLib=function\(\){"use strict";/) }) @@ -39,10 +31,7 @@ describe.runIf(isBuild)('build', () => { () => page.textContent('.dynamic-import-message'), 'hello vite' ) - const code = fs.readFileSync( - path.join(testDir, 'dist/lib/dynamic-import-message.es.mjs'), - 'utf-8' - ) + const code = readFile('dist/lib/dynamic-import-message.es.mjs') expect(code).not.toMatch('__vitePreload') // Test that library chunks are hashed @@ -55,6 +44,15 @@ describe.runIf(isBuild)('build', () => { expect(log).not.toMatch('All "@import" rules must come first') }) }) + + test('preserve process.env', () => { + const es = readFile('dist/my-lib-custom-filename.mjs') + const iife = readFile('dist/my-lib-custom-filename.iife.js') + const umd = readFile('dist/my-lib-custom-filename.umd.js') + expect(es).toMatch('process.env.NODE_ENV') + expect(iife).toMatch('process.env.NODE_ENV') + expect(umd).toMatch('process.env.NODE_ENV') + }) }) test.runIf(isServe)('dev', async () => { diff --git a/playground/lib/index.dist.html b/playground/lib/index.dist.html index 02362a9c111e5e..99f08c73396fea 100644 --- a/playground/lib/index.dist.html +++ b/playground/lib/index.dist.html @@ -4,6 +4,15 @@
+ +