Skip to content

fix: use ESM chunkLoading for "esm" format #256

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
Oct 8, 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
32 changes: 14 additions & 18 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,13 +445,6 @@ const composeFormatConfig = (format: Format): RsbuildConfig => {
return {
tools: {
rspack: {
output: {
module: true,
chunkFormat: 'module',
library: {
type: 'modern-module',
},
},
module: {
parser: {
javascript: jsParserOptions,
Expand All @@ -461,6 +454,16 @@ const composeFormatConfig = (format: Format): RsbuildConfig => {
concatenateModules: true,
sideEffects: 'flag',
},
output: {
module: true,
chunkFormat: 'module',
library: {
type: 'modern-module',
},
chunkLoading: 'import',
workerChunkLoading: 'import',
wasmLoading: 'fetch',
},
experiments: {
outputModule: true,
},
Expand All @@ -483,6 +486,9 @@ const composeFormatConfig = (format: Format): RsbuildConfig => {
library: {
type: 'commonjs',
},
chunkLoading: 'require',
workerChunkLoading: 'async-node',
wasmLoading: 'async-node',
},
},
},
Expand Down Expand Up @@ -758,11 +764,6 @@ const composeTargetConfig = (
tools: {
rspack: {
target: ['web'],
output: {
chunkLoading: 'import',
workerChunkLoading: 'import',
wasmLoading: 'fetch',
},
},
},
};
Expand All @@ -772,13 +773,8 @@ const composeTargetConfig = (
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.
// and leave them as-is in the rest of the cases. Leave the comments here to explain the behavior.
// { node: { __dirname: ..., __filename: ... } }
output: {
chunkLoading: 'require',
workerChunkLoading: 'async-node',
wasmLoading: 'async-node',
},
},
},
output: {
Expand Down
21 changes: 6 additions & 15 deletions packages/core/tests/__snapshots__/config.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,17 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
},
"output": {
"chunkFormat": "module",
"chunkLoading": "import",
"library": {
"type": "modern-module",
},
"module": true,
"wasmLoading": "fetch",
"workerChunkLoading": "import",
},
},
[Function],
{
"output": {
"chunkLoading": "import",
"wasmLoading": "fetch",
"workerChunkLoading": "import",
},
"target": [
"web",
],
Expand Down Expand Up @@ -267,19 +265,17 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
},
"output": {
"chunkFormat": "commonjs",
"chunkLoading": "require",
"iife": false,
"library": {
"type": "commonjs",
},
"wasmLoading": "async-node",
"workerChunkLoading": "async-node",
},
},
[Function],
{
"output": {
"chunkLoading": "import",
"wasmLoading": "fetch",
"workerChunkLoading": "import",
},
"target": [
"web",
],
Expand Down Expand Up @@ -410,11 +406,6 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
},
[Function],
{
"output": {
"chunkLoading": "import",
"wasmLoading": "fetch",
"workerChunkLoading": "import",
},
"target": [
"web",
],
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.

6 changes: 6 additions & 0 deletions tests/integration/async-chunks/default/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "async-chunks-test",
"version": "1.0.0",
"private": true,
"type": "module"
}
21 changes: 21 additions & 0 deletions tests/integration/async-chunks/default/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { defineConfig } from '@rslib/core';
import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper';

export default defineConfig({
lib: [
generateBundleEsmConfig({
source: {
entry: {
index: './src/index.js',
},
},
}),
generateBundleCjsConfig({
source: {
entry: {
index: './src/index.js',
},
},
}),
],
});
1 change: 1 addition & 0 deletions tests/integration/async-chunks/default/src/dynamic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const dyn = 'dynamic';
6 changes: 6 additions & 0 deletions tests/integration/async-chunks/default/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const foo = async () => {
const { dyn } = await import('./dynamic.js');
return dyn;
};

export { foo };
13 changes: 13 additions & 0 deletions tests/integration/async-chunks/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { join } from 'node:path';
import { buildAndGetResults } from 'test-helper';
import { expect, test } from 'vitest';

test('should get correct value from async chunks', async () => {
const fixturePath = join(__dirname, 'default');
const { entryFiles } = await buildAndGetResults(fixturePath);

for (const format of ['esm', 'cjs']) {
const { foo } = await import(entryFiles[format]);
expect(await foo()).toBe('dynamic');
}
});
Loading