Skip to content

Commit 02278ff

Browse files
authored
fix: correct ESM interop (#1193)
1 parent dfb007c commit 02278ff

File tree

11 files changed

+3557
-2
lines changed

11 files changed

+3557
-2
lines changed

packages/core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"chokidar": "^4.0.3",
5656
"fs-extra": "^11.3.1",
5757
"memfs": "^4.38.1",
58+
"path-serializer": "0.5.1",
5859
"picocolors": "1.1.1",
5960
"prebundle": "1.4.1",
6061
"rsbuild-plugin-publint": "^0.3.3",

packages/core/rstest.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ import { shared } from '../../rstest.workspace';
44
export default defineConfig({
55
...shared,
66
name: 'unit',
7+
setupFiles: ['./setupRstestTests.ts'],
78
});

packages/core/setupRstestTests.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import path from 'node:path';
2+
import process from 'node:process';
3+
import { beforeEach, expect } from '@rstest/core';
4+
import { createSnapshotSerializer } from 'path-serializer';
5+
6+
beforeEach(() => {
7+
// since our NODE_ENV injection logic is via cli, we need to
8+
// delete "test" NODE_ENV to avoid affecting the default build config
9+
delete process.env.NODE_ENV;
10+
});
11+
12+
function removeTrailingSlash(inputPath: string): string {
13+
return path.normalize(inputPath).replace(/[\\/]+$/, '');
14+
}
15+
expect.addSnapshotSerializer(
16+
createSnapshotSerializer({
17+
root: removeTrailingSlash(path.join(__dirname, '../../')),
18+
workspace: __dirname,
19+
features: {
20+
replaceHomeDir: false,
21+
replaceWorkspace: true,
22+
replaceRoot: true,
23+
escapeDoubleQuotes: false,
24+
transformCLR: false,
25+
},
26+
}),
27+
);
28+
29+
if (process.env.ECO_CI) {
30+
expect.extend({
31+
toMatchSnapshot: () => ({
32+
pass: true,
33+
message: () => 'Snapshot always passes',
34+
}),
35+
toMatchInlineSnapshot: () => ({
36+
pass: true,
37+
message: () => 'Snapshot always passes',
38+
}),
39+
});
40+
}

packages/core/src/config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,16 @@ const disableUrlParseRsbuildPlugin = (): RsbuildPlugin => ({
818818
},
819819
});
820820

821+
// Port https://github.com/web-infra-dev/rsbuild/pull/5955 before it merged into Rsbuild.
822+
const fixJsModuleTypePlugin = (): RsbuildPlugin => ({
823+
name: 'rsbuild:fix-js-module-type',
824+
setup(api) {
825+
api.modifyBundlerChain((config, { CHAIN_ID }) => {
826+
config.module.rule(CHAIN_ID.RULE.JS).delete('type');
827+
});
828+
},
829+
});
830+
821831
const composeShimsConfig = (
822832
format: Format,
823833
shims?: Shims,
@@ -866,6 +876,7 @@ const composeShimsConfig = (
866876
plugins: [
867877
resolvedShims.esm.require && pluginEsmRequireShim(),
868878
disableUrlParseRsbuildPlugin(),
879+
fixJsModuleTypePlugin(),
869880
].filter(Boolean),
870881
};
871882
break;
@@ -875,12 +886,16 @@ const composeShimsConfig = (
875886
plugins: [
876887
resolvedShims.cjs['import.meta.url'] && pluginCjsImportMetaUrlShim(),
877888
disableUrlParseRsbuildPlugin(),
889+
fixJsModuleTypePlugin(),
878890
].filter(Boolean),
879891
};
880892
break;
881893
case 'umd':
882894
case 'iife':
883895
case 'mf':
896+
rsbuildConfig = {
897+
plugins: [fixJsModuleTypePlugin()],
898+
};
884899
break;
885900
default:
886901
throw new Error(`Unsupported format: ${format}`);

0 commit comments

Comments
 (0)