Skip to content

Commit 00d190c

Browse files
authored
feat: preserve import.meta.url in ESM (#83)
1 parent 50573c6 commit 00d190c

File tree

7 files changed

+60
-2
lines changed

7 files changed

+60
-2
lines changed

e2e/cases/shims/esm/rslib.config.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { generateBundleEsmConfig } from '@e2e/helper';
2+
import { defineConfig } from '@rslib/core';
3+
4+
export default defineConfig({
5+
lib: [generateBundleEsmConfig(__dirname)],
6+
output: {
7+
target: 'node',
8+
},
9+
source: {
10+
entry: {
11+
main: './src/index.ts',
12+
},
13+
},
14+
});

e2e/cases/shims/esm/src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const foo = () => {
2+
const d1 = __dirname;
3+
const d2 = __dirname;
4+
const f1 = __filename;
5+
const f2 = __filename;
6+
const importMetaUrl = import.meta.url;
7+
};

e2e/cases/shims/index.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { join } from 'node:path';
2+
import { buildAndGetResults } from '@e2e/helper';
3+
import { expect, test } from 'vitest';
4+
5+
test('shims for __dirname and __filename in ESM', async () => {
6+
const fixturePath = join(__dirname, 'esm');
7+
const { entries } = await buildAndGetResults(fixturePath);
8+
for (const shim of [
9+
'import {fileURLToPath as __webpack_fileURLToPath__} from "url";',
10+
"var src_dirname = __webpack_fileURLToPath__(import.meta.url + '/..').slice(0, -1);",
11+
'var src_filename = __webpack_fileURLToPath__(import.meta.url);',
12+
// import.meta.url should not be substituted
13+
'const importMetaUrl = import.meta.url;',
14+
]) {
15+
expect(entries.esm).toContain(shim);
16+
}
17+
});
18+
19+
test.todo('shims for import.meta.url in CJS', async () => {});

packages/core/src/config.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,13 @@ const composeFormatConfig = (format: Format): RsbuildConfig => {
186186
type: 'modern-module',
187187
},
188188
},
189+
module: {
190+
parser: {
191+
javascript: {
192+
importMeta: false,
193+
},
194+
},
195+
},
189196
optimization: {
190197
concatenateModules: true,
191198
},
@@ -430,6 +437,9 @@ const composeTargetConfig = (target = 'web'): RsbuildConfig => {
430437
tools: {
431438
rspack: {
432439
target: ['node'],
440+
// "__dirname" and "__filename" shims will automatically be enabled when `output.module` is `true`,
441+
// and leave them as-is in the rest of the cases.
442+
// { node: { __dirname: ..., __filename: ... } }
433443
},
434444
},
435445
output: {

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ export { runCli } from './cli/commands';
33
export { defineConfig, loadConfig } from './config';
44
export { build } from './build';
55
export { logger } from './utils/logger';
6+
export type * from './types';
67

78
export const version: string = RSLIB_VERSION;

packages/core/src/types/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export * from './config';
2-
export * from './utils';
1+
export type * from './config';
2+
export type * from './utils';

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
4343
"outputModule": true,
4444
},
4545
"externalsType": "module-import",
46+
"module": {
47+
"parser": {
48+
"javascript": {
49+
"importMeta": false,
50+
},
51+
},
52+
},
4653
"optimization": {
4754
"concatenateModules": true,
4855
},

0 commit comments

Comments
 (0)