Skip to content

Commit c28a0a2

Browse files
authored
fix: should use specific runtime to get target url (#11372)
1 parent 2462e17 commit c28a0a2

File tree

6 files changed

+68
-5
lines changed

6 files changed

+68
-5
lines changed

crates/rspack_core/src/artifacts/code_generation_results.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,26 @@ impl CodeGenerationResults {
275275
}
276276
}
277277

278+
/**
279+
* This API should be used carefully, it will return one of the code generation result,
280+
* make sure the module has the same code generation result for all runtimes.
281+
*/
282+
pub fn get_one(
283+
&self,
284+
module_identifier: &ModuleIdentifier,
285+
) -> &BindingCell<CodeGenerationResult> {
286+
self
287+
.map
288+
.get(module_identifier)
289+
.and_then(|entry| {
290+
entry
291+
.values()
292+
.next()
293+
.and_then(|m| self.module_generation_result_map.get(m))
294+
})
295+
.unwrap_or_else(|| panic!("No code generation result for {module_identifier}"))
296+
}
297+
278298
pub fn add(
279299
&mut self,
280300
module_identifier: ModuleIdentifier,

crates/rspack_plugin_css/src/dependency/url.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,9 @@ impl CssUrlDependency {
3535
identifier: &ModuleIdentifier,
3636
compilation: &Compilation,
3737
) -> Option<String> {
38-
// Here we need the asset module's runtime to get the code generation result, which is not equal to
39-
// the css module's runtime, but actually multiple runtime optimization doesn't affect asset module,
40-
// in different runtime asset module will always have the same code generation result, so we use
41-
// `runtime: None` to get the only one code generation result
42-
let code_gen_result = compilation.code_generation_results.get(identifier, None);
38+
// url points to asset modules, and asset modules should have same codegen results for all runtimes
39+
let code_gen_result = compilation.code_generation_results.get_one(identifier);
40+
4341
if let Some(url) = code_gen_result.data.get::<CodeGenerationDataUrl>() {
4442
Some(url.inner().to_string())
4543
} else if let Some(data) = code_gen_result.data.get::<CodeGenerationDataFilename>() {
Lines changed: 17 additions & 0 deletions
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.test {
2+
background: url('./image.svg');
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
it('should compile without panic', () => {
2+
3+
})
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module.exports = {
2+
entry: {
3+
asset: "./image.svg",
4+
style: "./main.css",
5+
main: "./main.js"
6+
},
7+
target: "web",
8+
module: {
9+
rules: [
10+
{
11+
test: /\.svg$/,
12+
type: "asset/resource"
13+
}
14+
]
15+
},
16+
output: {
17+
filename: "[name].js"
18+
},
19+
experiments: {
20+
css: true
21+
}
22+
};

0 commit comments

Comments
 (0)