Skip to content

Commit

Permalink
feat: preserve import.meta.url in ESM (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework authored Aug 12, 2024
1 parent 50573c6 commit 00d190c
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 2 deletions.
14 changes: 14 additions & 0 deletions e2e/cases/shims/esm/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { generateBundleEsmConfig } from '@e2e/helper';
import { defineConfig } from '@rslib/core';

export default defineConfig({
lib: [generateBundleEsmConfig(__dirname)],
output: {
target: 'node',
},
source: {
entry: {
main: './src/index.ts',
},
},
});
7 changes: 7 additions & 0 deletions e2e/cases/shims/esm/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const foo = () => {
const d1 = __dirname;
const d2 = __dirname;
const f1 = __filename;
const f2 = __filename;
const importMetaUrl = import.meta.url;
};
19 changes: 19 additions & 0 deletions e2e/cases/shims/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { join } from 'node:path';
import { buildAndGetResults } from '@e2e/helper';
import { expect, test } from 'vitest';

test('shims for __dirname and __filename in ESM', async () => {
const fixturePath = join(__dirname, 'esm');
const { entries } = await buildAndGetResults(fixturePath);
for (const shim of [
'import {fileURLToPath as __webpack_fileURLToPath__} from "url";',
"var src_dirname = __webpack_fileURLToPath__(import.meta.url + '/..').slice(0, -1);",
'var src_filename = __webpack_fileURLToPath__(import.meta.url);',
// import.meta.url should not be substituted
'const importMetaUrl = import.meta.url;',
]) {
expect(entries.esm).toContain(shim);
}
});

test.todo('shims for import.meta.url in CJS', async () => {});
10 changes: 10 additions & 0 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ const composeFormatConfig = (format: Format): RsbuildConfig => {
type: 'modern-module',
},
},
module: {
parser: {
javascript: {
importMeta: false,
},
},
},
optimization: {
concatenateModules: true,
},
Expand Down Expand Up @@ -430,6 +437,9 @@ const composeTargetConfig = (target = 'web'): RsbuildConfig => {
tools: {
rspack: {
target: ['node'],
// "__dirname" and "__filename" shims will automatically be enabled when `output.module` is `true`,
// and leave them as-is in the rest of the cases.
// { node: { __dirname: ..., __filename: ... } }
},
},
output: {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export { runCli } from './cli/commands';
export { defineConfig, loadConfig } from './config';
export { build } from './build';
export { logger } from './utils/logger';
export type * from './types';

export const version: string = RSLIB_VERSION;
4 changes: 2 additions & 2 deletions packages/core/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './config';
export * from './utils';
export type * from './config';
export type * from './utils';
7 changes: 7 additions & 0 deletions packages/core/tests/__snapshots__/config.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
"outputModule": true,
},
"externalsType": "module-import",
"module": {
"parser": {
"javascript": {
"importMeta": false,
},
},
},
"optimization": {
"concatenateModules": true,
},
Expand Down

0 comments on commit 00d190c

Please sign in to comment.