Skip to content

Commit a839c14

Browse files
sapphi-redhi-ogawa
andauthored
fix(css): import using imports field in CS was not resolved (#15985)
Co-authored-by: Hiroshi Ogawa <hi.ogawa.zz@gmail.com>
1 parent 4fe971f commit a839c14

9 files changed

Lines changed: 62 additions & 3 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@
125125
},
126126
"patchedDependencies": {
127127
"chokidar@3.6.0": "patches/chokidar@3.6.0.patch",
128-
"sirv@2.0.4": "patches/sirv@2.0.4.patch"
128+
"sirv@2.0.4": "patches/sirv@2.0.4.patch",
129+
"postcss-import@16.0.1": "patches/postcss-import@16.0.1.patch"
129130
},
130131
"peerDependencyRules": {
131132
"allowedVersions": {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
diff --git a/lib/parse-styles.js b/lib/parse-styles.js
2+
index 68a1fef604fad82ac367f6bd63e845027bf86089..4617545e86301f58bfcd649f66c0536e29bda50f 100644
3+
--- a/lib/parse-styles.js
4+
+++ b/lib/parse-styles.js
5+
@@ -223,19 +223,20 @@ function isProcessableURL(uri) {
6+
return false
7+
}
8+
9+
- // check for fragment or query
10+
- try {
11+
- // needs a base to parse properly
12+
- const url = new URL(uri, "https://example.com")
13+
-
14+
- if (url.hash) {
15+
- return false
16+
- }
17+
-
18+
- if (url.search) {
19+
- return false
20+
- }
21+
- } catch {} // Ignore
22+
+ // PATCH: comment out this part to support resolving imports field
23+
+ // // check for fragment or query
24+
+ // try {
25+
+ // // needs a base to parse properly
26+
+ // const url = new URL(uri, "https://example.com")
27+
+
28+
+ // if (url.hash) {
29+
+ // return false
30+
+ // }
31+
+
32+
+ // if (url.search) {
33+
+ // return false
34+
+ // }
35+
+ // } catch {} // Ignore
36+
37+
return true
38+
}

playground/css/__tests__/css.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,10 @@ test('aliased css has content', async () => {
476476
expect(await getColor('.aliased-module')).toBe('blue')
477477
})
478478

479+
test('resolve imports field in CSS', async () => {
480+
expect(await getColor('.imports-field')).toBe('red')
481+
})
482+
479483
test.runIf(isBuild)('warning can be suppressed by esbuild.logOverride', () => {
480484
serverLogs.forEach((log) => {
481485
// no warning from esbuild css minifier

playground/css/imports-field.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.imports-field {
2+
color: red;
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import '#imports';

playground/css/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ <h1>CSS</h1>
200200
<p class="aliased">import '#alias': this should be blue</p>
201201
<pre class="aliased-content"></pre>
202202
<p class="aliased-module">import '#alias-module': this should be blue</p>
203+
204+
<p>Imports field</p>
205+
<p class="imports-field">import '#imports': this should be red</p>
203206
</div>
204207
<style>
205208
@import url(./imported.scss);

playground/css/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,5 @@ import './async/index'
126126

127127
import('./same-name/sub1/sub')
128128
import('./same-name/sub2/sub')
129+
130+
import './imports-imports-field.css'

playground/css/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,8 @@
2727
"sass": "^1.71.0",
2828
"stylus": "^0.62.0",
2929
"sugarss": "^4.0.1"
30+
},
31+
"imports": {
32+
"#imports": "./imports-field.css"
3033
}
3134
}

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)