Skip to content

Commit 4ae9c67

Browse files
committed
feat: enhance env replacement with esbuild
1 parent 7057b55 commit 4ae9c67

File tree

7 files changed

+8
-39
lines changed

7 files changed

+8
-39
lines changed

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
"@rollup/plugin-inject": "^5.0.5",
4444
"@rollup/plugin-json": "^6.1.0",
4545
"@rollup/plugin-node-resolve": "^16.0.0",
46-
"@rollup/plugin-replace": "^6.0.2",
4746
"@rollup/pluginutils": "^5.1.4",
4847
"esbuild": "^0.24.2",
4948
"magic-string": "^0.30.17",

pnpm-lock.yaml

-19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rollup/configs/pkg.ts

+1-15
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import nodeResolve from '@rollup/plugin-node-resolve';
44
import commonjs from '@rollup/plugin-commonjs';
55
import json from '@rollup/plugin-json';
66
import alias from '@rollup/plugin-alias';
7-
import replace from '@rollup/plugin-replace';
87
import dynamicImportVars from '@rollup/plugin-dynamic-import-vars';
98
import type { TsConfigResult } from 'get-tsconfig';
109
import type { AliasMap } from '../../types.js';
@@ -27,6 +26,7 @@ export const getPkgConfig = (
2726
const esbuildConfig: TransformOptions = {
2827
target: options.target,
2928
tsconfigRaw: tsconfig?.config,
29+
define: env,
3030
};
3131

3232
return {
@@ -47,20 +47,6 @@ export const getPkgConfig = (
4747
extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json'],
4848
exportConditions: options.exportCondition,
4949
}),
50-
...(
51-
Object.keys(env).length > 0
52-
? [replace({
53-
preventAssignment: true,
54-
55-
/**
56-
* Seems this currently doesn't work:
57-
* https://github.com/rollup/plugins/pull/1084#discussion_r861447543
58-
*/
59-
objectGuards: true,
60-
values: env,
61-
})]
62-
: []
63-
),
6450
stripHashbang(),
6551
json(),
6652
esbuildTransform(esbuildConfig),

src/rollup/plugins/esbuild.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const esbuildTransform = (
66
options?: TransformOptions,
77
): Plugin => {
88
const filter = createFilter(
9-
/\.([cm]?ts|[jt]sx)$/,
9+
/\.([cm]?[jt]s|[jt]sx)$/,
1010
);
1111

1212
return {

tests/fixtures.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ export const fixtureFiles = {
5454
`,
5555

5656
'conditional-require.js': outdent`
57-
if (process.env.NODE_ENV === 'production') {
57+
if (
58+
process.env.NODE_ENV === 'production'
59+
|| process.env['NODE_ENV'] === 'production'
60+
) {
5861
console.log('production');
5962
require('./cjs.cjs');
6063
} else {

tests/specs/builds/env.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default testSuite(({ describe }, nodePath: string) => {
2323

2424
const content = await fixture.readFile('dist/conditional-require.js', 'utf8');
2525
expect(content).toMatch('development');
26-
expect(content).not.toMatch('2 ** 3');
26+
expect(content).not.toMatch('production');
2727
});
2828
});
2929
});

tests/specs/builds/output-module.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ export default testSuite(({ describe }, nodePath: string) => {
224224
expect(pkgrollProcess.stderr).toBe('');
225225

226226
const content = await fixture.readFile('dist/conditional-require.mjs', 'utf8');
227-
expect(content).toMatch('\tconsole.log(\'side effect\');');
227+
expect(content).toMatch('\tconsole.log("side effect");');
228228
});
229229

230230
test('require() & createRequire gets completely removed on conditional', async () => {

0 commit comments

Comments
 (0)