Skip to content

Commit 5e5b639

Browse files
authored
fix: use ESM chunkLoading for "esm" format (#256)
1 parent d1be3a7 commit 5e5b639

File tree

8 files changed

+69
-33
lines changed

8 files changed

+69
-33
lines changed

packages/core/src/config.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -445,13 +445,6 @@ const composeFormatConfig = (format: Format): RsbuildConfig => {
445445
return {
446446
tools: {
447447
rspack: {
448-
output: {
449-
module: true,
450-
chunkFormat: 'module',
451-
library: {
452-
type: 'modern-module',
453-
},
454-
},
455448
module: {
456449
parser: {
457450
javascript: jsParserOptions,
@@ -461,6 +454,16 @@ const composeFormatConfig = (format: Format): RsbuildConfig => {
461454
concatenateModules: true,
462455
sideEffects: 'flag',
463456
},
457+
output: {
458+
module: true,
459+
chunkFormat: 'module',
460+
library: {
461+
type: 'modern-module',
462+
},
463+
chunkLoading: 'import',
464+
workerChunkLoading: 'import',
465+
wasmLoading: 'fetch',
466+
},
464467
experiments: {
465468
outputModule: true,
466469
},
@@ -483,6 +486,9 @@ const composeFormatConfig = (format: Format): RsbuildConfig => {
483486
library: {
484487
type: 'commonjs',
485488
},
489+
chunkLoading: 'require',
490+
workerChunkLoading: 'async-node',
491+
wasmLoading: 'async-node',
486492
},
487493
},
488494
},
@@ -758,11 +764,6 @@ const composeTargetConfig = (
758764
tools: {
759765
rspack: {
760766
target: ['web'],
761-
output: {
762-
chunkLoading: 'import',
763-
workerChunkLoading: 'import',
764-
wasmLoading: 'fetch',
765-
},
766767
},
767768
},
768769
};
@@ -772,13 +773,8 @@ const composeTargetConfig = (
772773
rspack: {
773774
target: ['node'],
774775
// "__dirname" and "__filename" shims will automatically be enabled when `output.module` is `true`,
775-
// and leave them as-is in the rest of the cases.
776+
// and leave them as-is in the rest of the cases. Leave the comments here to explain the behavior.
776777
// { node: { __dirname: ..., __filename: ... } }
777-
output: {
778-
chunkLoading: 'require',
779-
workerChunkLoading: 'async-node',
780-
wasmLoading: 'async-node',
781-
},
782778
},
783779
},
784780
output: {

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

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,17 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
116116
},
117117
"output": {
118118
"chunkFormat": "module",
119+
"chunkLoading": "import",
119120
"library": {
120121
"type": "modern-module",
121122
},
122123
"module": true,
124+
"wasmLoading": "fetch",
125+
"workerChunkLoading": "import",
123126
},
124127
},
125128
[Function],
126129
{
127-
"output": {
128-
"chunkLoading": "import",
129-
"wasmLoading": "fetch",
130-
"workerChunkLoading": "import",
131-
},
132130
"target": [
133131
"web",
134132
],
@@ -267,19 +265,17 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
267265
},
268266
"output": {
269267
"chunkFormat": "commonjs",
268+
"chunkLoading": "require",
270269
"iife": false,
271270
"library": {
272271
"type": "commonjs",
273272
},
273+
"wasmLoading": "async-node",
274+
"workerChunkLoading": "async-node",
274275
},
275276
},
276277
[Function],
277278
{
278-
"output": {
279-
"chunkLoading": "import",
280-
"wasmLoading": "fetch",
281-
"workerChunkLoading": "import",
282-
},
283279
"target": [
284280
"web",
285281
],
@@ -410,11 +406,6 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
410406
},
411407
[Function],
412408
{
413-
"output": {
414-
"chunkLoading": "import",
415-
"wasmLoading": "fetch",
416-
"workerChunkLoading": "import",
417-
},
418409
"target": [
419410
"web",
420411
],

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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "async-chunks-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module"
6+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { defineConfig } from '@rslib/core';
2+
import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper';
3+
4+
export default defineConfig({
5+
lib: [
6+
generateBundleEsmConfig({
7+
source: {
8+
entry: {
9+
index: './src/index.js',
10+
},
11+
},
12+
}),
13+
generateBundleCjsConfig({
14+
source: {
15+
entry: {
16+
index: './src/index.js',
17+
},
18+
},
19+
}),
20+
],
21+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const dyn = 'dynamic';
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const foo = async () => {
2+
const { dyn } = await import('./dynamic.js');
3+
return dyn;
4+
};
5+
6+
export { foo };
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { join } from 'node:path';
2+
import { buildAndGetResults } from 'test-helper';
3+
import { expect, test } from 'vitest';
4+
5+
test('should get correct value from async chunks', async () => {
6+
const fixturePath = join(__dirname, 'default');
7+
const { entryFiles } = await buildAndGetResults(fixturePath);
8+
9+
for (const format of ['esm', 'cjs']) {
10+
const { foo } = await import(entryFiles[format]);
11+
expect(await foo()).toBe('dynamic');
12+
}
13+
});

0 commit comments

Comments
 (0)