Skip to content

Commit 7648f3c

Browse files
authored
fix(pack): preserve css module styles for dual imports (#3295)
1 parent 5718201 commit 7648f3c

13 files changed

Lines changed: 408 additions & 9 deletions

File tree

crates/pack-core/src/shared/transforms/inline_css/source_asset.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ impl Asset for InlineCssFileSource {
4949
#[turbo_tasks::function]
5050
async fn content(&self) -> Result<Vc<AssetContent>> {
5151
let ident = self.css.ident().await?;
52-
let ident_str = ident.path.to_string();
52+
let mut ident_str = ident.path.to_string();
53+
// Keep global and CSS Modules imports of the same file as distinct runtime styles.
54+
ident_str.push_str(&ident.query);
5355
let content_import = StringifyJs(INLINE_CSS_CONTENT);
5456
let insert_js = StringifyJs(&*self.insert);
5557
let id_js = StringifyJs(&*ident_str);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const assert = require("node:assert");
2+
const fs = require("node:fs");
3+
const path = require("node:path");
4+
5+
const outputDir = path.join(__dirname, "output");
6+
const outputFiles = fs.readdirSync(outputDir);
7+
const bundle = fs.readFileSync(
8+
path.join(outputDir, "index.morpho.min.js"),
9+
"utf8",
10+
);
11+
const inlineStyleIds = [
12+
...bundle.matchAll(
13+
/var update = [\s\S]*?\(\[\s*\[\s*"([^"]+\.css(?:\?modules)?)",/g,
14+
),
15+
].map((match) => match[1]);
16+
17+
assert.deepStrictEqual(inlineStyleIds.sort(), [
18+
"library/inline_css_dual_import/input/reward.less.css",
19+
"library/inline_css_dual_import/input/reward.less.css?modules",
20+
]);
21+
assert.match(bundle, /\.reward-less__[\w]+__prizeBackgroundImage \{/);
22+
assert.match(bundle, /\\n\.prizeBackgroundImage \{/);
23+
assert.doesNotMatch(
24+
outputFiles.join("\n"),
25+
/\.css$/,
26+
"inline CSS must not require a standalone stylesheet",
27+
);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"runtimeType": "Dummy",
3+
"config": {
4+
"entry": [
5+
{
6+
"import": "input/index.ts",
7+
"name": "main",
8+
"library": {
9+
"name": "InlineCssDualImportLibrary"
10+
}
11+
}
12+
],
13+
"output": {
14+
"filename": "index.morpho.min.js",
15+
"cssFilename": "index.morpho.min.css"
16+
},
17+
"sourceMaps": true,
18+
"styles": {
19+
"inlineCss": {}
20+
}
21+
}
22+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { prizeBackgroundImageClassName } from "./reward";
2+
import "./reward.less";
3+
4+
export default prizeBackgroundImageClassName;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.prizeBackgroundImage {
2+
width: 528px;
3+
height: 563px;
4+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import styles from "./reward.less";
2+
3+
export const prizeBackgroundImageClassName = styles.prizeBackgroundImage;

crates/pack-tests/tests/snapshot/library/inline_css_dual_import/output/index.morpho.min.css.map

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)