Skip to content

Commit 09eaac7

Browse files
authored
feat: support arrow function in on_chunks_loaded callback (#12193)
* feat: support arrow function in on_chunks_loaded callback * test: add case
1 parent 8abe4b1 commit 09eaac7

File tree

7 files changed

+130
-2
lines changed

7 files changed

+130
-2
lines changed

crates/rspack_plugin_javascript/src/plugin/mod.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@ impl JsPlugin {
243243
let mut header: Vec<Cow<str>> = Vec::new();
244244
let mut startup: Vec<Cow<str>> = Vec::new();
245245
let mut allow_inline_startup = true;
246+
let supports_arrow_function = compilation
247+
.options
248+
.output
249+
.environment
250+
.supports_arrow_function();
246251

247252
if allow_inline_startup && module_factories {
248253
startup.push("// module factories are used so entry inlining is disabled".into());
@@ -419,17 +424,25 @@ impl JsPlugin {
419424
}
420425

421426
if !chunk_ids.is_empty() {
427+
let on_chunks_loaded_callback = if supports_arrow_function {
428+
format!("() => {}({module_id_expr})", RuntimeGlobals::REQUIRE)
429+
} else {
430+
format!(
431+
"function() {{ return {}({module_id_expr}) }}",
432+
RuntimeGlobals::REQUIRE
433+
)
434+
};
422435
buf2.push(
423436
format!(
424-
"{}{}(undefined, {}, function() {{ return {}({module_id_expr}) }});",
437+
"{}{}(undefined, {}, {});",
425438
if i + 1 == entries.len() {
426439
format!("var {} = ", RuntimeGlobals::EXPORTS)
427440
} else {
428441
"".to_string()
429442
},
430443
RuntimeGlobals::ON_CHUNKS_LOADED,
431444
stringify_array(&chunk_ids),
432-
RuntimeGlobals::REQUIRE
445+
on_chunks_loaded_callback
433446
)
434447
.into(),
435448
);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Case split-chunks-arrow-function: Step 0
2+
3+
## Changed Files
4+
5+
6+
## Asset Files
7+
- Bundle: 0.js
8+
- Bundle: [runtime of 1].js
9+
10+
## Manifest
11+
12+
13+
## Update
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Case split-chunks-arrow-function: Step 1
2+
3+
## Changed Files
4+
- node_modules/vendor.js
5+
6+
## Asset Files
7+
- Bundle: 0.js
8+
- Bundle: [runtime of 1].js
9+
- Manifest: [runtime of 1].LAST_HASH.hot-update.json, size: 29
10+
- Update: 0.LAST_HASH.hot-update.js, size: 227
11+
- Update: 1.LAST_HASH.hot-update.js, size: 179
12+
13+
## Manifest
14+
15+
### [runtime of 1].LAST_HASH.hot-update.json
16+
17+
```json
18+
{"c":["0","1"],"r":[],"m":[]}
19+
```
20+
21+
22+
## Update
23+
24+
25+
### 0.LAST_HASH.hot-update.js
26+
27+
#### Changed Modules
28+
- ./node_modules/vendor.js
29+
30+
#### Changed Runtime Modules
31+
32+
33+
#### Changed Content
34+
```js
35+
self["webpackHotUpdate"]("0", {
36+
"./node_modules/vendor.js":
37+
/*!********************************!*\
38+
!*** ./node_modules/vendor.js ***!
39+
\********************************/
40+
(function (module) {
41+
module.exports = "2";
42+
43+
44+
}),
45+
46+
});
47+
```
48+
49+
50+
51+
### 1.LAST_HASH.hot-update.js
52+
53+
#### Changed Modules
54+
55+
56+
#### Changed Runtime Modules
57+
- webpack/runtime/get_full_hash
58+
59+
#### Changed Content
60+
```js
61+
"use strict";
62+
self["webpackHotUpdate"]("1", {},function(__webpack_require__) {
63+
// webpack/runtime/get_full_hash
64+
(() => {
65+
__webpack_require__.h = () => ("CURRENT_HASH")
66+
})();
67+
68+
}
69+
);
70+
```
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import vendor from "vendor";
2+
3+
it("should hot update a splitted initial chunk with `environment.arrowFunction: true`", async () => {
4+
expect(vendor).toBe("1");
5+
await NEXT_HMR();
6+
expect(vendor).toBe("2");
7+
});
8+
9+
module.hot.accept(["vendor"]);

tests/rspack-test/hotCases/chunks/split-chunks-arrow-function/node_modules/vendor.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = {
2+
output: {
3+
filename: "[name].js",
4+
environment: {
5+
arrowFunction: true
6+
}
7+
},
8+
optimization: {
9+
chunkIds: "total-size",
10+
splitChunks: {
11+
chunks: "all",
12+
minSize: 0
13+
}
14+
}
15+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
findBundle: function (i, options) {
3+
return ["main.js", "0.js"];
4+
}
5+
};

0 commit comments

Comments
 (0)