Skip to content

Commit ce95372

Browse files
authored
refactor(css): Remove unnecessary and structure code, add comments (#3529)
* refactor(css): Remove unnessary and structure code, add comments * Fix rust snapshot * Fix rust snapshot * Fix rust snapshot
1 parent 7d02b44 commit ce95372

File tree

25 files changed

+100
-135
lines changed

25 files changed

+100
-135
lines changed

crates/rspack_loader_sass/tests/fixtures/rspack_importer/expected/main.css

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
.color-red {
22
color: red;
33
}
4-
54
@charset "UTF-8";
65
body {
76
font: 100% Helvetica, sans-serif;
@@ -31,4 +30,4 @@ nav a {
3130
}
3231
.bar:before {
3332
content: "∑";
34-
}
33+
}

crates/rspack_plugin_css/src/plugin/impl_plugin_for_css_plugin.rs

Lines changed: 51 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ use rspack_core::rspack_sources::ReplaceSource;
77
use rspack_core::{
88
get_css_chunk_filename_template,
99
rspack_sources::{BoxSource, ConcatSource, MapOptions, RawSource, Source, SourceExt},
10-
ChunkKind, ModuleType, NormalModuleAstOrSource, ParserAndGenerator, PathData, Plugin,
11-
RenderManifestEntry, SourceType,
10+
Chunk, ChunkKind, ModuleType, ParserAndGenerator, PathData, Plugin, RenderManifestEntry,
11+
SourceType,
1212
};
13+
use rspack_core::{Compilation, ModuleIdentifier};
1314
use rspack_error::Result;
1415
use rspack_hash::RspackHash;
1516

@@ -18,6 +19,48 @@ use crate::swc_css_compiler::{SwcCssSourceMapGenConfig, SWC_COMPILER};
1819
use crate::utils::AUTO_PUBLIC_PATH_PLACEHOLDER_REGEX;
1920
use crate::CssPlugin;
2021

22+
impl CssPlugin {
23+
fn render_chunk_to_source(
24+
compilation: &Compilation,
25+
chunk: &Chunk,
26+
ordered_css_modules: &[ModuleIdentifier],
27+
) -> rspack_error::Result<ConcatSource> {
28+
let module_sources = ordered_css_modules
29+
.iter()
30+
.map(|module_id| {
31+
let code_gen_result = compilation
32+
.code_generation_results
33+
.get(module_id, Some(&chunk.runtime))?;
34+
35+
let module_source = code_gen_result
36+
.get(&SourceType::Css)
37+
.map(|result| result.ast_or_source.clone().try_into_source())
38+
.transpose();
39+
40+
module_source
41+
})
42+
.collect::<Result<Vec<Option<BoxSource>>>>()?;
43+
44+
let source = module_sources
45+
.into_par_iter()
46+
// TODO(hyf0): I couldn't think of a situation where a module doesn't have `Source`.
47+
// Should we return a Error if there is a `None` in `module_sources`?
48+
// Webpack doesn't throw. It just do a best-effort checking https://github.com/webpack/webpack/blob/5e3c4d0ddf8ae6a6e45fea42be4e8950fe49c0bb/lib/css/CssModulesPlugin.js#L565-L568
49+
.flatten()
50+
.fold(ConcatSource::default, |mut output, cur| {
51+
output.add(cur);
52+
output.add(RawSource::from("\n"));
53+
output
54+
})
55+
.reduce(ConcatSource::default, |mut acc, cur| {
56+
acc.add(cur);
57+
acc
58+
});
59+
60+
Ok(source)
61+
}
62+
}
63+
2164
#[async_trait::async_trait]
2265
impl Plugin for CssPlugin {
2366
fn name(&self) -> &'static str {
@@ -91,74 +134,24 @@ impl Plugin for CssPlugin {
91134
args: rspack_core::RenderManifestArgs<'_>,
92135
) -> rspack_core::PluginRenderManifestHookOutput {
93136
let compilation = args.compilation;
94-
let chunk = args.chunk();
137+
let chunk = args.chunk_ukey.as_ref(&compilation.chunk_by_ukey);
95138
if matches!(chunk.kind, ChunkKind::HotUpdate) {
96139
return Ok(vec![]);
97140
}
98-
let ordered_modules = Self::get_ordered_chunk_css_modules(
141+
142+
let ordered_css_modules = Self::get_ordered_chunk_css_modules(
99143
chunk,
100144
&compilation.chunk_graph,
101145
&compilation.module_graph,
102146
compilation,
103147
);
104148

105-
// Early bail if any of the normal modules were failed to build.
106-
if ordered_modules.iter().any(|ident| {
107-
args
108-
.compilation
109-
.module_graph
110-
.module_by_identifier(ident)
111-
.and_then(|module| module.as_normal_module())
112-
.map(|module| {
113-
matches!(
114-
module.ast_or_source(),
115-
NormalModuleAstOrSource::BuiltFailed(..)
116-
)
117-
})
118-
.unwrap_or(false)
119-
}) {
120-
return Ok(vec![]);
121-
}
122-
123-
let sources = ordered_modules
124-
.par_iter()
125-
.map(|module_id| {
126-
let code_gen_result = compilation
127-
.code_generation_results
128-
.get(module_id, Some(&chunk.runtime))?;
129-
130-
code_gen_result
131-
.get(&SourceType::Css)
132-
.map(|result| result.ast_or_source.clone().try_into_source())
133-
.transpose()
134-
})
135-
.filter(|result| {
136-
if let Ok(result) = result {
137-
return result.is_some();
138-
};
139-
false
140-
})
141-
.collect::<Result<Vec<Option<BoxSource>>>>()?;
142-
143-
if sources.is_empty() {
149+
// Prevent generating css files for chunks which doesn't contain css modules.
150+
if ordered_css_modules.is_empty() {
144151
return Ok(Default::default());
145152
}
146153

147-
let sources = sources
148-
.into_par_iter()
149-
.enumerate()
150-
.fold(ConcatSource::default, |mut output, (idx, cur)| {
151-
if let Some(source) = cur {
152-
if idx != 0 {
153-
output.add(RawSource::from("\n\n"));
154-
}
155-
output.add(source);
156-
}
157-
output
158-
})
159-
.collect::<Vec<ConcatSource>>();
160-
161-
let source = ConcatSource::new(sources);
154+
let source = Self::render_chunk_to_source(compilation, chunk, &ordered_css_modules)?;
162155

163156
let filename_template = get_css_chunk_filename_template(
164157
chunk,
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
@import url("http://example.com/test.css");
2-
32
body {
43
background: red;
5-
}
4+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
._style-module__foo---___6655ebe837449348-_66 {
22
color: hotpink;
3-
}
3+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
@keyframes play {}
1+
@keyframes play {}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
.test {
22
width: 2rem;
3-
}
3+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
@-webkit-keyframes play {}
22
@-moz-keyframes play {}
3-
@keyframes play {}
3+
@keyframes play {}

crates/rspack_plugin_css/tests/fixtures/webpack/at-charset/expected/main.css

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,35 @@
22
.ae {
33
background: green;
44
}
5-
65
@charset "utf-8";
76
.aa {
87
background: green;
98
}
10-
119
@charset "utf-8";
1210
.ab {
1311
background: green;
1412
}
15-
1613
@charset "utf-8";
1714
.ac {
1815
background: green;
1916
}
20-
2117
@charset "utf-8";
2218
.ad {
2319
background: green;
2420
}
25-
2621
@charset "utf-8";
2722
body {
2823
background: red;
2924
}
30-
3125
@charset "utf-8";
3226
.ba {
3327
background: green;
3428
}
35-
3629
@charset "utf-8";
3730
.bb {
3831
background: green;
3932
}
40-
4133
@charset "utf-8";
4234
body {
4335
background: yellow;
44-
}
36+
}

crates/rspack_plugin_css/tests/fixtures/webpack/at-import-in-the-middle/expected/main.css

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
body {
22
background: red;
33
}
4-
54
@import url("https://some/other/external/css");
6-
75
.c {
86
background: red;
97
}
108
.c {
119
color: yellow;
1210
}
13-
1411
@import url("https://some/external/css");
15-
1612
.b {
1713
background: red;
1814
}
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,27 @@
11
.ae {
22
background: green;
33
}
4-
54
.aa {
65
background: green;
76
}
8-
97
.ab {
108
background: green;
119
}
12-
1310
.ac {
1411
background: green;
1512
}
16-
1713
.ad {
1814
background: green;
1915
}
20-
2116
body {
2217
background: red;
2318
}
24-
2519
.ba {
2620
background: green;
2721
}
28-
2922
.bb {
3023
background: green;
3124
}
32-
3325
body {
3426
background: yellow;
35-
}
27+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
body {
22
background: red;
3-
}
3+
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
body {
22
background: red;
33
}
4-
54
body {
65
background: green;
7-
}
6+
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
body {
22
background: green;
33
}
4-
54
body {
65
background: red;
7-
}
6+
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
body {
22
background: red;
33
}
4-
54
.component {
65
background: blue;
76
}
8-
97
body {
108
background: green;
11-
}
9+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
body {
22
background: red;
3-
}
3+
}

crates/rspack_plugin_css/tests/fixtures/webpack/shared-import/expected/main.css

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
background-color: green;
44
border: 1px solid green;
55
}
6-
76
.shared {
87
color: red;
98
}
10-
119
.shared {
1210
background-color: red;
13-
}
11+
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
body {
22
background: red;
33
}
4-
54
body {
65
background: green;
7-
}
6+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
body {
22
background: red;
3-
}
3+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
body {
22
background: red;
3-
}
3+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
body {
22
background: red;
3-
}
3+
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
body {
22
background: red;
33
}
4-
54
body {
65
background: green;
7-
}
6+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
body {
22
background: red;
3-
}
3+
}

0 commit comments

Comments
 (0)