Skip to content
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
3 changes: 3 additions & 0 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,9 @@ const composeFormatConfig = ({
type: 'umd',
},
},
optimization: {
nodeEnv: process.env.NODE_ENV,
},
},
},
};
Expand Down
3 changes: 3 additions & 0 deletions packages/core/tests/__snapshots__/config.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,9 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
},
},
},
"optimization": {
"nodeEnv": "test",
},
"output": {
"asyncChunks": false,
"library": {
Expand Down
4 changes: 3 additions & 1 deletion tests/integration/umd/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/umd/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const { addPrefix } = require('./utils');

module.exports = (str) => addPrefix('DEBUG:', str);
module.exports = (str) => addPrefix('DEBUG:', str, process.env.NODE_ENV);
2 changes: 1 addition & 1 deletion tests/integration/umd/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const addPrefix = (prefix, str) => `${prefix}${str}`;
const addPrefix = (prefix, str, env) => `${env}: ${prefix}${str}`;

module.exports = {
addPrefix,
Expand Down
2 changes: 1 addition & 1 deletion website/docs/en/guide/basic/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.

:::

Expand Down
Loading