Skip to content

Commit 4506d64

Browse files
authored
fix: should not inject css prefetch/preload runtime when only prefetch/preload js chunks (#12112)
* fix: should not inject css prefetch/preload runtime when only prefetch javascript chunks * fix: should not inject css prefetch/preload runtime when only prefetch javascript chunks * fix: should not inject css prefetch/preload runtime when only prefetch javascript chunks * fix: should not inject css prefetch/preload runtime when only prefetch javascript chunks
1 parent d8463bd commit 4506d64

File tree

23 files changed

+264
-71
lines changed

23 files changed

+264
-71
lines changed

crates/rspack_core/src/chunk.rs

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -809,30 +809,63 @@ impl Chunk {
809809

810810
pub fn get_child_ids_by_order<F: Fn(&ChunkUkey, &Compilation) -> bool>(
811811
&self,
812-
order: &ChunkGroupOrderKey,
812+
order_key: &ChunkGroupOrderKey,
813813
compilation: &Compilation,
814814
filter_fn: &F,
815815
) -> Option<Vec<ChunkId>> {
816-
self
817-
.get_children_of_type_in_order(order, compilation, true)
818-
.map(|order_children| {
819-
order_children
820-
.iter()
821-
.flat_map(|(_, child_chunks)| {
822-
child_chunks.iter().filter_map(|chunk_ukey| {
823-
if filter_fn(chunk_ukey, compilation) {
824-
compilation
825-
.chunk_by_ukey
826-
.expect_get(chunk_ukey)
827-
.id(&compilation.chunk_ids_artifact)
828-
.cloned()
829-
} else {
830-
None
831-
}
816+
let mut list = vec![];
817+
for group_ukey in self.get_sorted_groups_iter(&compilation.chunk_group_by_ukey) {
818+
let group = compilation.chunk_group_by_ukey.expect_get(group_ukey);
819+
if group
820+
.chunks
821+
.last()
822+
.is_some_and(|chunk_ukey| chunk_ukey.eq(&self.ukey))
823+
{
824+
for child_group_ukey in group.children_iterable() {
825+
let child_group = compilation.chunk_group_by_ukey.expect_get(child_group_ukey);
826+
if let Some(order) = child_group
827+
.kind
828+
.get_normal_options()
829+
.and_then(|o| match order_key {
830+
ChunkGroupOrderKey::Prefetch => o.prefetch_order,
831+
ChunkGroupOrderKey::Preload => o.preload_order,
832832
})
833-
})
834-
.collect_vec()
835-
})
833+
{
834+
list.push((order, *child_group_ukey));
835+
}
836+
}
837+
}
838+
}
839+
840+
list.sort_by(|a, b| {
841+
let order = b.0.cmp(&a.0);
842+
match order {
843+
Ordering::Equal => compare_chunk_group(&a.1, &b.1, compilation),
844+
_ => order,
845+
}
846+
});
847+
848+
let mut chunk_ids = vec![];
849+
for (_, child_group_ukey) in list.iter() {
850+
let child_group = compilation.chunk_group_by_ukey.expect_get(child_group_ukey);
851+
for chunk_ukey in child_group.chunks.iter() {
852+
if filter_fn(chunk_ukey, compilation)
853+
&& let Some(chunk_id) = compilation
854+
.chunk_by_ukey
855+
.expect_get(chunk_ukey)
856+
.id(&compilation.chunk_ids_artifact)
857+
.cloned()
858+
{
859+
chunk_ids.push(chunk_id);
860+
}
861+
}
862+
}
863+
864+
if chunk_ids.is_empty() {
865+
return None;
866+
}
867+
868+
Some(chunk_ids)
836869
}
837870

838871
pub fn get_child_ids_by_orders_map<F: Fn(&ChunkUkey, &Compilation) -> bool + Sync>(

packages/rspack-test-tools/src/case/hot-step.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,9 @@ function createHotStepProcessor(
150150
}, str);
151151
};
152152

153-
const fileList = stats
154-
.assets!.map(i => {
153+
const assets = stats.assets!.sort((a, b) => a.name.localeCompare(b.name));
154+
const fileList = assets
155+
.map(i => {
155156
const fileName = i.name;
156157
const renderName = replaceFileName(fileName);
157158
const content = replaceContent(

tests/rspack-test/HotSnapshot.hottest.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ describeByWalk(__filename, (name, src, dist) => {
66
createHotStepCase(name, src, dist, path.join(tempDir, name), "web");
77
}, {
88
source: path.resolve(__dirname, "./hotCases"),
9-
dist: path.resolve(__dirname, `./js/hot-snapshot`)
9+
dist: path.resolve(__dirname, `./js/hot-snapshot`),
10+
exclude: [/remove-add-worker/]
1011
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = () => "FIXME: can not find the css folder"
1+
module.exports = () => "TODO: generate empty css asset when loader throws error"

tests/rspack-test/configCases/css/import/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import "./style.css";
2-
import path from "path";
32

43
it("should compile", () => {
5-
const links = document.getElementsByTagName("link");
4+
const links = Array.from(document.getElementsByTagName("link"));
5+
const path = __non_webpack_require__("path");
66
const css = [];
77

88
// Skip first because import it by default
99
for (const link of links.slice(1)) {
10-
css.push(link.sheet.css);
10+
css.push(getLinkSheet(link));
1111
}
1212

1313
expect(css).toMatchFileSnapshot(path.join(__SNAPSHOT__, 'bundle0.css.txt'));
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
"use strict";
22

33
module.exports = {
4-
moduleScope(scope) {
4+
moduleScope(scope, stats) {
5+
const __STATS_I__ = stats().__index__;
56
const link = scope.window.document.createElement("link");
67
link.rel = "stylesheet";
7-
link.href = `bundle${scope.__STATS_I__}.css`;
8+
link.href = `bundle${__STATS_I__}.css`;
89
scope.window.document.head.appendChild(link);
910
}
1011
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = () => "FIXME: can not resolve errors";
1+
module.exports = () => "TODO: support css/global";

tests/rspack-test/configCases/css/prefetch-preload-module/index.mjs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,19 @@ it("should prefetch and preload child chunks on chunk load", () => {
3636
// Test normal script loading
3737
link = document.head._children[3];
3838
expect(link._type).toBe("link");
39-
expect(link.rel).toBe("modulepreload");
40-
expect(link.href).toBe("https://example.com/public/path/chunk1-b.mjs");
41-
42-
link = document.head._children[4];
43-
expect(link._type).toBe("link");
4439
expect(link.rel).toBe("preload");
4540
expect(link.as).toBe("style");
4641
expect(link.href).toBe("https://example.com/public/path/chunk1-a-css.css");
4742

48-
link = document.head._children[5];
43+
link = document.head._children[4];
4944
expect(link._type).toBe("link");
5045
expect(link.rel).toBe("modulepreload");
5146
expect(link.href).toBe("https://example.com/public/path/chunk1-a-css.mjs");
5247

53-
48+
link = document.head._children[5];
49+
expect(link._type).toBe("link");
50+
expect(link.rel).toBe("modulepreload");
51+
expect(link.href).toBe("https://example.com/public/path/chunk1-b.mjs");
5452

5553
return promise.then(() => {
5654
expect(document.head._children).toHaveLength(8);

tests/rspack-test/configCases/web/prefetch-preload-module-only-js/index.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ it("should prefetch and preload child chunks on chunk load", () => {
2525
expect(link._type).toBe("link");
2626
expect(link.rel).toBe("modulepreload");
2727
expect(link.href).toBe("https://example.com/public/path/chunk1-b.mjs");
28-
expect(link.charset).toBe("utf-8");
2928
expect(link.getAttribute("nonce")).toBe("nonce");
3029
expect(link.crossOrigin).toBe("anonymous");
3130

tests/rspack-test/configCases/web/prefetch-preload-module-only-js/test.filter.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)