diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index e1a357a65..3e3242436 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -583,6 +583,9 @@ const composeFormatConfig = ({ type: 'umd', }, }, + optimization: { + nodeEnv: process.env.NODE_ENV, + }, }, }, }; diff --git a/packages/core/tests/__snapshots__/config.test.ts.snap b/packages/core/tests/__snapshots__/config.test.ts.snap index c7de92413..876fe2079 100644 --- a/packages/core/tests/__snapshots__/config.test.ts.snap +++ b/packages/core/tests/__snapshots__/config.test.ts.snap @@ -640,6 +640,9 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1 }, }, }, + "optimization": { + "nodeEnv": "test", + }, "output": { "asyncChunks": false, "library": { diff --git a/tests/integration/umd/index.test.ts b/tests/integration/umd/index.test.ts index 021b9fbac..d1dd2fddc 100644 --- a/tests/integration/umd/index.test.ts +++ b/tests/integration/umd/index.test.ts @@ -2,13 +2,15 @@ import { buildAndGetResults } from 'test-helper'; import { expect, test } from 'vitest'; test('read UMD value in CommonJS', async () => { + process.env.NODE_ENV = 'production'; const fixturePath = __dirname; const { entryFiles } = await buildAndGetResults({ fixturePath, }); const fn = require(entryFiles.umd); - expect(fn('ok')).toBe('DEBUG:ok'); + expect(fn('ok')).toBe('production: DEBUG:ok'); + delete process.env.NODE_ENV; }); test('throw error when using UMD with `bundle: false`', async () => { diff --git a/tests/integration/umd/src/index.js b/tests/integration/umd/src/index.js index a33d9aa51..668b67dab 100644 --- a/tests/integration/umd/src/index.js +++ b/tests/integration/umd/src/index.js @@ -1,3 +1,3 @@ const { addPrefix } = require('./utils'); -module.exports = (str) => addPrefix('DEBUG:', str); +module.exports = (str) => addPrefix('DEBUG:', str, process.env.NODE_ENV); diff --git a/tests/integration/umd/src/utils.js b/tests/integration/umd/src/utils.js index 280e6c37b..983dc84b5 100644 --- a/tests/integration/umd/src/utils.js +++ b/tests/integration/umd/src/utils.js @@ -1,4 +1,4 @@ -const addPrefix = (prefix, str) => `${prefix}${str}`; +const addPrefix = (prefix, str, env) => `${env}: ${prefix}${str}`; module.exports = { addPrefix, diff --git a/website/docs/en/guide/basic/cli.mdx b/website/docs/en/guide/basic/cli.mdx index c3fb989f6..701b42c81 100644 --- a/website/docs/en/guide/basic/cli.mdx +++ b/website/docs/en/guide/basic/cli.mdx @@ -53,7 +53,7 @@ You can see more details in [Environment Variables](https://rsbuild.dev/guide/ad ::: note -`process.env.NODE_ENV` will be preserved in the build output when [format](/config/lib/format) is set to `esm` or `cjs`. For `mf` format, it will be preserved to make output directly usable. +`process.env.NODE_ENV` will be preserved in the build output when [format](/config/lib/format) is set to `esm` or `cjs`. For `mf` or `umd` format, it will be preserved to make output directly usable. :::