Skip to content

fix(esm): disable module.parser.javascript.url #509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path, { dirname, extname, isAbsolute, join } from 'node:path';
import {
type EnvironmentConfig,
type RsbuildConfig,
type RsbuildPlugin,
defineConfig as defineRsbuildConfig,
loadConfig as loadRsbuildConfig,
mergeRsbuildConfig,
Expand Down Expand Up @@ -612,6 +613,19 @@ const composeFormatConfig = ({
}
};

const formatRsbuildPlugin = (): RsbuildPlugin => ({
name: 'rsbuild:format',
setup(api) {
api.modifyBundlerChain((config, { CHAIN_ID }) => {
// Fix for https://github.com/web-infra-dev/rslib/issues/499.
// Prevent parsing and try bundling `new URL()` in ESM format.
config.module.rule(CHAIN_ID.RULE.JS).parser({
url: false,
});
});
},
});

const composeShimsConfig = (
format: Format,
shims?: Shims,
Expand Down Expand Up @@ -657,9 +671,10 @@ const composeShimsConfig = (
},
},
},
plugins: [resolvedShims.esm.require && pluginEsmRequireShim()].filter(
Boolean,
),
plugins: [
resolvedShims.esm.require && pluginEsmRequireShim(),
formatRsbuildPlugin(),
].filter(Boolean),
};
break;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/core/tests/__snapshots__/config.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
},
},
"plugins": [
{
"name": "rsbuild:format",
"setup": [Function],
},
{
"name": "rsbuild:lib-entry-chunk",
"setup": [Function],
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions tests/integration/format/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { buildAndGetResults } from 'test-helper';
import { expect, test } from 'vitest';

test('esm', async () => {
const fixturePath = __dirname;
const { files, entries, entryFiles } = await buildAndGetResults({
fixturePath,
});
expect(files).toMatchInlineSnapshot(`
{
"esm": [
"<ROOT>/tests/integration/format/dist/esm/index.js",
],
}
`);
expect(entries.esm).toMatchInlineSnapshot(`
"import * as __WEBPACK_EXTERNAL_MODULE_node_url__ from "node:url";
const packageDirectory = __WEBPACK_EXTERNAL_MODULE_node_url__["default"].fileURLToPath(new URL('.', import.meta.url));
const foo = 'foo';
export { foo, packageDirectory };
"
`);

const result = await import(entryFiles.esm);
expect(result).toMatchInlineSnapshot(`
{
"foo": "foo",
"packageDirectory": "<ROOT>/tests/integration/format/dist/esm/",
}
`);
});
6 changes: 6 additions & 0 deletions tests/integration/format/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "format-test",
"version": "1.0.0",
"private": true,
"type": "module"
}
19 changes: 19 additions & 0 deletions tests/integration/format/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { defineConfig } from '@rslib/core';
import { generateBundleEsmConfig } from 'test-helper';

export default defineConfig({
lib: [
generateBundleEsmConfig({
output: {
distPath: {
root: './dist/esm',
},
},
}),
],
source: {
entry: {
index: './src/index.js',
},
},
});
1 change: 1 addition & 0 deletions tests/integration/format/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { packageDirectory, foo } from './value.js';
5 changes: 5 additions & 0 deletions tests/integration/format/src/value.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import url from 'node:url';
export const packageDirectory = url.fileURLToPath(
new URL('.', import.meta.url),
);
export const foo = 'foo';
Loading