Skip to content

Commit 8ca5c7b

Browse files
authored
feat: do not preserve nodeEnv when format is umd (#520)
1 parent ecf9d6f commit 8ca5c7b

File tree

6 files changed

+12
-4
lines changed

6 files changed

+12
-4
lines changed

packages/core/src/config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,9 @@ const composeFormatConfig = ({
583583
type: 'umd',
584584
},
585585
},
586+
optimization: {
587+
nodeEnv: process.env.NODE_ENV,
588+
},
586589
},
587590
},
588591
};

packages/core/tests/__snapshots__/config.test.ts.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,9 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
640640
},
641641
},
642642
},
643+
"optimization": {
644+
"nodeEnv": "test",
645+
},
643646
"output": {
644647
"asyncChunks": false,
645648
"library": {

tests/integration/umd/index.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import { buildAndGetResults } from 'test-helper';
22
import { expect, test } from 'vitest';
33

44
test('read UMD value in CommonJS', async () => {
5+
process.env.NODE_ENV = 'production';
56
const fixturePath = __dirname;
67
const { entryFiles } = await buildAndGetResults({
78
fixturePath,
89
});
910

1011
const fn = require(entryFiles.umd);
11-
expect(fn('ok')).toBe('DEBUG:ok');
12+
expect(fn('ok')).toBe('production: DEBUG:ok');
13+
delete process.env.NODE_ENV;
1214
});
1315

1416
test('throw error when using UMD with `bundle: false`', async () => {

tests/integration/umd/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
const { addPrefix } = require('./utils');
22

3-
module.exports = (str) => addPrefix('DEBUG:', str);
3+
module.exports = (str) => addPrefix('DEBUG:', str, process.env.NODE_ENV);

tests/integration/umd/src/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const addPrefix = (prefix, str) => `${prefix}${str}`;
1+
const addPrefix = (prefix, str, env) => `${env}: ${prefix}${str}`;
22

33
module.exports = {
44
addPrefix,

website/docs/en/guide/basic/cli.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ You can see more details in [Environment Variables](https://rsbuild.dev/guide/ad
5353

5454
::: note
5555

56-
`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.
56+
`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.
5757

5858
:::
5959

0 commit comments

Comments
 (0)