Skip to content

Commit c8f15f4

Browse files
authored
fix(esm): disable module.parser.javascript.url (#509)
1 parent cff510b commit c8f15f4

File tree

8 files changed

+86
-3
lines changed

8 files changed

+86
-3
lines changed

packages/core/src/config.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path, { dirname, extname, isAbsolute, join } from 'node:path';
33
import {
44
type EnvironmentConfig,
55
type RsbuildConfig,
6+
type RsbuildPlugin,
67
defineConfig as defineRsbuildConfig,
78
loadConfig as loadRsbuildConfig,
89
mergeRsbuildConfig,
@@ -612,6 +613,19 @@ const composeFormatConfig = ({
612613
}
613614
};
614615

616+
const formatRsbuildPlugin = (): RsbuildPlugin => ({
617+
name: 'rsbuild:format',
618+
setup(api) {
619+
api.modifyBundlerChain((config, { CHAIN_ID }) => {
620+
// Fix for https://github.com/web-infra-dev/rslib/issues/499.
621+
// Prevent parsing and try bundling `new URL()` in ESM format.
622+
config.module.rule(CHAIN_ID.RULE.JS).parser({
623+
url: false,
624+
});
625+
});
626+
},
627+
});
628+
615629
const composeShimsConfig = (
616630
format: Format,
617631
shims?: Shims,
@@ -657,9 +671,10 @@ const composeShimsConfig = (
657671
},
658672
},
659673
},
660-
plugins: [resolvedShims.esm.require && pluginEsmRequireShim()].filter(
661-
Boolean,
662-
),
674+
plugins: [
675+
resolvedShims.esm.require && pluginEsmRequireShim(),
676+
formatRsbuildPlugin(),
677+
].filter(Boolean),
663678
};
664679
break;
665680
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
108108
},
109109
},
110110
"plugins": [
111+
{
112+
"name": "rsbuild:format",
113+
"setup": [Function],
114+
},
111115
{
112116
"name": "rsbuild:lib-entry-chunk",
113117
"setup": [Function],

pnpm-lock.yaml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { buildAndGetResults } from 'test-helper';
2+
import { expect, test } from 'vitest';
3+
4+
test('esm', async () => {
5+
const fixturePath = __dirname;
6+
const { files, entries, entryFiles } = await buildAndGetResults({
7+
fixturePath,
8+
});
9+
expect(files).toMatchInlineSnapshot(`
10+
{
11+
"esm": [
12+
"<ROOT>/tests/integration/format/dist/esm/index.js",
13+
],
14+
}
15+
`);
16+
expect(entries.esm).toMatchInlineSnapshot(`
17+
"import * as __WEBPACK_EXTERNAL_MODULE_node_url__ from "node:url";
18+
const packageDirectory = __WEBPACK_EXTERNAL_MODULE_node_url__["default"].fileURLToPath(new URL('.', import.meta.url));
19+
const foo = 'foo';
20+
export { foo, packageDirectory };
21+
"
22+
`);
23+
24+
const result = await import(entryFiles.esm);
25+
expect(result).toMatchInlineSnapshot(`
26+
{
27+
"foo": "foo",
28+
"packageDirectory": "<ROOT>/tests/integration/format/dist/esm/",
29+
}
30+
`);
31+
});

tests/integration/format/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "format-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module"
6+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { defineConfig } from '@rslib/core';
2+
import { generateBundleEsmConfig } from 'test-helper';
3+
4+
export default defineConfig({
5+
lib: [
6+
generateBundleEsmConfig({
7+
output: {
8+
distPath: {
9+
root: './dist/esm',
10+
},
11+
},
12+
}),
13+
],
14+
source: {
15+
entry: {
16+
index: './src/index.js',
17+
},
18+
},
19+
});

tests/integration/format/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { packageDirectory, foo } from './value.js';

tests/integration/format/src/value.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import url from 'node:url';
2+
export const packageDirectory = url.fileURLToPath(
3+
new URL('.', import.meta.url),
4+
);
5+
export const foo = 'foo';

0 commit comments

Comments
 (0)