Skip to content

Commit df63722

Browse files
authored
feat(webpack/chunk-loading)!: chunkLoading: 'lynx' for Rspack (#400)
<!-- Thank you for submitting a pull request! We appreciate the time and effort you have invested in making these changes. Please ensure that you provide enough information to allow others to review your pull request. Upon submission, your pull request will be automatically assigned with reviewers. If you want to learn more about contributing to this project, please visit: https://github.com/lynx-family/lynx-stack/blob/main/CONTRIBUTING.md. --> ## Summary <!-- Can you explain the reasoning behind implementing this change? What problem or issue does this pull request resolve? --> This patch deletes the `ChunkLoadingRspackPlugin` and use `ChunkLoadingWebpackPlugin` directly in Rspack. We are doing it now since we have implemented all the APIs needed. - web-infra-dev/rspack#9035 - web-infra-dev/rspack#9092 - web-infra-dev/rspack#9093 > [!CAUTION] > This is a **BREAKING CHANGE** since you need to use `chunkLoading: 'lynx'` instead of `chunkLoading: 'require'`. fix: #253 since we do not relay on `hooks.runtimeModule` which could cause issue. <!-- It would be helpful if you could provide any relevant context, such as GitHub issues or related discussions. --> ## Checklist <!--- Check and mark with an "x" --> - [ ] Tests updated (or not required). - [ ] Documentation updated (or not required).
1 parent ccee1a5 commit df63722

File tree

16 files changed

+62
-246
lines changed

16 files changed

+62
-246
lines changed

.changeset/bright-loops-walk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@lynx-js/chunk-loading-webpack-plugin": minor
3+
---
4+
5+
**BREAKING CHANGE**: Requires `@rspack/core` v1.3.0.

.changeset/smooth-maps-check.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
"@lynx-js/chunk-loading-webpack-plugin": minor
3+
---
4+
5+
**BREAKING CHANGE**: Remove the deprecated `ChunkLoadingRspackPlugin`, use `ChunkLoadingWebpackPlugin` with `output.chunkLoading: 'lynx'` instead.
6+
7+
```js
8+
import { ChunkLoadingWebpackPlugin } from '@lynx-js/chunk-loading-webpack-plugin';
9+
10+
export default {
11+
output: {
12+
chunkLoading: 'lynx',
13+
},
14+
plugins: [
15+
new ChunkLoadingWebpackPlugin(),
16+
],
17+
};
18+
```

packages/rspeedy/core/src/plugins/chunkLoading.plugin.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,12 @@ export function pluginChunkLoading(): RsbuildPlugin {
2929
.use(ChunkLoadingWebpackPlugin)
3030
.end()
3131
.output
32-
// Rspack needs `chunkLoading: 'require'` since we use runtimeModule hook
33-
// to override the chunk loading runtime.
34-
.chunkLoading('require')
32+
.chunkLoading('lynx')
3533
.chunkFormat('commonjs')
3634
.iife(false)
3735
.end()
3836
}
3937
})
40-
41-
api.modifyWebpackChain((chain, { environment }) => {
42-
if (isLynx(environment)) {
43-
chain
44-
.output
45-
// For webpack, we directly use `chunkLoading: 'lynx'`.
46-
.chunkLoading('lynx')
47-
.end()
48-
}
49-
})
5038
},
5139
}
5240
}

packages/rspeedy/core/test/plugins/__snapshots__/output.plugin.test.ts.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ exports[`Plugins - Output > defaults - Production 1`] = `
55
"assetModuleFilename": "static/assets/[name].[contenthash:8][ext]",
66
"chunkFilename": "static/js/async/[name].[contenthash:8].js",
77
"chunkFormat": "commonjs",
8-
"chunkLoading": "require",
8+
"chunkLoading": "lynx",
99
"filename": "static/js/[name].[contenthash:8].js",
1010
"hashFunction": "xxhash64",
1111
"iife": false,
@@ -21,7 +21,7 @@ exports[`Plugins - Output > defaults 1`] = `
2121
"assetModuleFilename": "static/assets/[name].[contenthash:8][ext]",
2222
"chunkFilename": "static/js/async/[name].js",
2323
"chunkFormat": "commonjs",
24-
"chunkLoading": "require",
24+
"chunkLoading": "lynx",
2525
"filename": "static/js/[name].js",
2626
"hashFunction": "xxhash64",
2727
"iife": false,
@@ -37,7 +37,7 @@ exports[`Plugins - Output > output.filename 1`] = `
3737
"assetModuleFilename": "static/assets/[name].[contenthash:8][ext]",
3838
"chunkFilename": "static/js/async/[name].js",
3939
"chunkFormat": "commonjs",
40-
"chunkLoading": "require",
40+
"chunkLoading": "lynx",
4141
"filename": "static/js/[name].js",
4242
"hashFunction": "xxhash64",
4343
"iife": false,
@@ -53,7 +53,7 @@ exports[`Plugins - Output > output.filename.js 1`] = `
5353
"assetModuleFilename": "static/assets/[name].[contenthash:8][ext]",
5454
"chunkFilename": "static/js/async/[name].js",
5555
"chunkFormat": "commonjs",
56-
"chunkLoading": "require",
56+
"chunkLoading": "lynx",
5757
"filename": "static/js/[name].js",
5858
"hashFunction": "xxhash64",
5959
"iife": false,

packages/rspeedy/core/test/plugins/chunkLoading.plugin.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('Plugins - chunkLoading', () => {
1111

1212
const config = await rspeedy.unwrapConfig()
1313

14-
expect(config.output?.chunkLoading).toBe('require')
14+
expect(config.output?.chunkLoading).toBe('lynx')
1515
expect(config.output?.chunkFormat).toBe('commonjs')
1616
expect(
1717
config.plugins?.some(plugin =>
@@ -82,7 +82,7 @@ describe('Plugins - chunkLoading', () => {
8282
expect(webConfig?.output?.chunkFormat).not.toBe('commonjs')
8383
expect(webConfig?.output?.iife).not.toBe(false)
8484

85-
expect(lynxConfig?.output?.chunkLoading).toBe('require')
85+
expect(lynxConfig?.output?.chunkLoading).toBe('lynx')
8686
expect(lynxConfig?.output?.chunkFormat).toBe('commonjs')
8787
expect(lynxConfig?.output?.iife).toBe(false)
8888
})

packages/webpack/chunk-loading-webpack-plugin/etc/chunk-loading-webpack-plugin.api.md

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,18 @@
33
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
44
55
```ts
6-
import type { Compiler } from '@rspack/core';
7-
8-
// @public @deprecated
9-
export class ChunkLoadingRspackPlugin {
10-
constructor(options?: Partial<ChunkLoadingRspackPluginOptions>);
11-
apply(compiler: Compiler): void;
12-
static defaultOptions: Readonly<Required<ChunkLoadingRspackPluginOptions>>;
13-
}
146

15-
// @public
16-
export interface ChunkLoadingRspackPluginOptions {
17-
}
7+
import type { Compiler } from '@rspack/core';
188

199
// @public
2010
export class ChunkLoadingWebpackPlugin {
21-
constructor(options?: Partial<ChunkLoadingWebpackPluginOptions>);
22-
apply(compiler: Compiler): void;
23-
static defaultOptions: Readonly<Required<ChunkLoadingWebpackPluginOptions>>;
11+
constructor(options?: Partial<ChunkLoadingWebpackPluginOptions>);
12+
apply(compiler: Compiler): void;
13+
static defaultOptions: Readonly<Required<ChunkLoadingWebpackPluginOptions>>;
2414
}
2515

2616
// @public
2717
export interface ChunkLoadingWebpackPluginOptions {
2818
}
19+
2920
```

packages/webpack/chunk-loading-webpack-plugin/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,16 @@
4141
},
4242
"devDependencies": {
4343
"@lynx-js/test-tools": "workspace:*",
44+
"@lynx-js/vitest-setup": "workspace:*",
4445
"@microsoft/api-extractor": "catalog:",
46+
"@rspack/cli": "^1.3.0",
4547
"@rspack/core": "catalog:rspack",
4648
"css-loader": "^7.1.2",
4749
"mini-css-extract-plugin": "^2.9.2",
4850
"webpack": "^5.98.0"
4951
},
5052
"peerDependencies": {
51-
"@rspack/core": "^1.0.0"
53+
"@rspack/core": "^1.3.0"
5254
},
5355
"engines": {
5456
"node": ">=18"

packages/webpack/chunk-loading-webpack-plugin/src/ChunkLoadingRspackPlugin.ts

Lines changed: 0 additions & 188 deletions
This file was deleted.

packages/webpack/chunk-loading-webpack-plugin/src/ChunkLoadingWebpackPlugin.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ export class ChunkLoadingWebpackPluginImpl {
3939
const ChunkLoadingRuntimeModule = createChunkLoadingRuntimeModule(
4040
compiler.webpack,
4141
);
42-
const onceForChunkSet = new WeakSet<Chunk>();
42+
// TODO(colinaaa): enable this after https://github.com/web-infra-dev/rspack/issues/9849 is fixed.
43+
// const onceForChunkSet = new WeakSet<Chunk>();
4344

4445
const globalChunkLoading = compilation.outputOptions.chunkLoading;
4546
/**
@@ -55,8 +56,8 @@ export class ChunkLoadingWebpackPluginImpl {
5556
};
5657

5758
const handler = (chunk: Chunk, runtimeRequirements: Set<string>) => {
58-
if (onceForChunkSet.has(chunk)) return;
59-
onceForChunkSet.add(chunk);
59+
// if (onceForChunkSet.has(chunk)) return;
60+
// onceForChunkSet.add(chunk);
6061
if (!isEnabledForChunk(chunk)) return;
6162
runtimeRequirements.add(RuntimeGlobals.getChunkUpdateScriptFilename);
6263
runtimeRequirements.add(RuntimeGlobals.moduleFactoriesAddOnly);

packages/webpack/chunk-loading-webpack-plugin/src/index.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import type { Compiler } from '@rspack/core';
1212

13-
import { ChunkLoadingRspackPluginImpl } from './ChunkLoadingRspackPlugin.js';
1413
import { ChunkLoadingWebpackPluginImpl } from './ChunkLoadingWebpackPlugin.js';
1514

1615
/**
@@ -80,16 +79,9 @@ export class ChunkLoadingWebpackPlugin {
8079
ChunkLoadingWebpackPlugin.defaultOptions,
8180
this.options,
8281
);
83-
if (compiler.webpack.rspackVersion) {
84-
new ChunkLoadingRspackPluginImpl(compiler, options);
85-
} else {
86-
new ChunkLoadingWebpackPluginImpl(
87-
compiler as unknown as import('webpack').Compiler,
88-
options,
89-
);
90-
}
82+
new ChunkLoadingWebpackPluginImpl(
83+
compiler as unknown as import('webpack').Compiler,
84+
options,
85+
);
9186
}
9287
}
93-
94-
export { ChunkLoadingRspackPlugin } from './ChunkLoadingRspackPlugin.js';
95-
export type { ChunkLoadingRspackPluginOptions } from './ChunkLoadingRspackPlugin.js';

0 commit comments

Comments
 (0)