Skip to content

Commit 97adefa

Browse files
committed
test: env applied in node_modules
1 parent 4ae9c67 commit 97adefa

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

tests/fixtures.ts

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export const fixtureFiles = {
5757
if (
5858
process.env.NODE_ENV === 'production'
5959
|| process.env['NODE_ENV'] === 'production'
60+
|| process.env['PROD'] === 'true'
6061
) {
6162
console.log('production');
6263
require('./cjs.cjs');

tests/specs/builds/env.ts

+38-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default testSuite(({ describe }, nodePath: string) => {
1313
}),
1414
});
1515

16-
const pkgrollProcess = await pkgroll(['--env.NODE_ENV=development'], {
16+
const pkgrollProcess = await pkgroll(['--env.NODE_ENV=development', '--env.PROD=false'], {
1717
cwd: fixture.path,
1818
nodePath,
1919
});
@@ -25,5 +25,42 @@ export default testSuite(({ describe }, nodePath: string) => {
2525
expect(content).toMatch('development');
2626
expect(content).not.toMatch('production');
2727
});
28+
29+
test('dead code elimination via env in node_modules', async () => {
30+
await using fixture = await createFixture({
31+
'package.json': createPackageJson({
32+
main: './dist/index.mjs',
33+
}),
34+
'src/index.mjs': 'import "dep"',
35+
'node_modules/dep': {
36+
'package.json': createPackageJson({
37+
main: 'index.js',
38+
}),
39+
'index.js': `
40+
if (
41+
process.env.NODE_ENV === 'production'
42+
|| process.env['NODE_ENV'] === 'production'
43+
|| process.env['PROD'] === 'true'
44+
) {
45+
console.log('production');
46+
} else {
47+
console.log('development');
48+
}
49+
`,
50+
},
51+
});
52+
53+
const pkgrollProcess = await pkgroll(['--env.NODE_ENV=development', '--env.PROD=false'], {
54+
cwd: fixture.path,
55+
nodePath,
56+
});
57+
58+
expect(pkgrollProcess.exitCode).toBe(0);
59+
expect(pkgrollProcess.stderr).toBe('');
60+
61+
const content = await fixture.readFile('dist/index.mjs', 'utf8');
62+
expect(content).toMatch('development');
63+
expect(content).not.toMatch('production');
64+
});
2865
});
2966
});

0 commit comments

Comments
 (0)