Skip to content

Commit 7f9f8f1

Browse files
authored
fix(css): sourcemap crash with postcss (#7982)
1 parent f6ae60d commit 7f9f8f1

File tree

6 files changed

+59
-18
lines changed

6 files changed

+59
-18
lines changed

packages/playground/vue-sourcemap/Css.vue

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<p class="css">&lt;css&gt;</p>
33
<p :class="$style['css-module']">&lt;css&gt; module</p>
44
<p class="css-scoped">&lt;css&gt; scoped</p>
5+
<p class="css-scoped-nested">&lt;css&gt; scoped with nested</p>
56
</template>
67

78
<style>
@@ -21,3 +22,13 @@
2122
color: red;
2223
}
2324
</style>
25+
26+
<style scoped>
27+
.css-scoped-nested {
28+
color: red;
29+
.dummy {
30+
color: green;
31+
}
32+
font-weight: bold;
33+
}
34+
</style>

packages/playground/vue-sourcemap/__tests__/serve.spec.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ if (!isBuild) {
8080
const map = extractSourcemap(css)
8181
expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(`
8282
Object {
83-
"mappings": ";AAOA;EACE,UAAU;AACZ",
83+
"mappings": ";AAQA;EACE,UAAU;AACZ",
8484
"sources": Array [
8585
"/root/Css.vue",
8686
],
@@ -89,6 +89,7 @@ if (!isBuild) {
8989
<p class=\\"css\\">&lt;css&gt;</p>
9090
<p :class=\\"$style['css-module']\\">&lt;css&gt; module</p>
9191
<p class=\\"css-scoped\\">&lt;css&gt; scoped</p>
92+
<p class=\\"css-scoped-nested\\">&lt;css&gt; scoped with nested</p>
9293
</template>
9394
9495
<style>
@@ -108,6 +109,16 @@ if (!isBuild) {
108109
color: red;
109110
}
110111
</style>
112+
113+
<style scoped>
114+
.css-scoped-nested {
115+
color: red;
116+
.dummy {
117+
color: green;
118+
}
119+
font-weight: bold;
120+
}
121+
</style>
111122
",
112123
],
113124
"version": 3,
@@ -120,7 +131,7 @@ if (!isBuild) {
120131
const map = extractSourcemap(css)
121132
expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(`
122133
Object {
123-
"mappings": ";AAaA;EACE,UAAU;AACZ",
134+
"mappings": ";AAcA;EACE,UAAU;AACZ",
124135
"sources": Array [
125136
"/root/Css.vue",
126137
],
@@ -129,6 +140,7 @@ if (!isBuild) {
129140
<p class=\\"css\\">&lt;css&gt;</p>
130141
<p :class=\\"$style['css-module']\\">&lt;css&gt; module</p>
131142
<p class=\\"css-scoped\\">&lt;css&gt; scoped</p>
143+
<p class=\\"css-scoped-nested\\">&lt;css&gt; scoped with nested</p>
132144
</template>
133145
134146
<style>
@@ -148,6 +160,16 @@ if (!isBuild) {
148160
color: red;
149161
}
150162
</style>
163+
164+
<style scoped>
165+
.css-scoped-nested {
166+
color: red;
167+
.dummy {
168+
color: green;
169+
}
170+
font-weight: bold;
171+
}
172+
</style>
151173
",
152174
],
153175
"version": 3,
@@ -160,7 +182,7 @@ if (!isBuild) {
160182
const map = extractSourcemap(css)
161183
expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(`
162184
Object {
163-
"mappings": ";AAmBA;EACE,UAAU;AACZ",
185+
"mappings": ";AAoBA;EACE,UAAU;AACZ",
164186
"sources": Array [
165187
"/root/Css.vue",
166188
],
@@ -169,6 +191,7 @@ if (!isBuild) {
169191
<p class=\\"css\\">&lt;css&gt;</p>
170192
<p :class=\\"$style['css-module']\\">&lt;css&gt; module</p>
171193
<p class=\\"css-scoped\\">&lt;css&gt; scoped</p>
194+
<p class=\\"css-scoped-nested\\">&lt;css&gt; scoped with nested</p>
172195
</template>
173196
174197
<style>
@@ -188,6 +211,16 @@ if (!isBuild) {
188211
color: red;
189212
}
190213
</style>
214+
215+
<style scoped>
216+
.css-scoped-nested {
217+
color: red;
218+
.dummy {
219+
color: green;
220+
}
221+
font-weight: bold;
222+
}
223+
</style>
191224
",
192225
],
193226
"version": 3,

packages/playground/vue-sourcemap/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"devDependencies": {
1212
"@vitejs/plugin-vue": "workspace:*",
1313
"less": "^4.1.2",
14-
"sass": "^1.43.4"
14+
"sass": "^1.43.4",
15+
"postcss-nested": "^5.0.6"
1516
},
1617
"dependencies": {
1718
"vue": "^3.2.31"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
plugins: [require('postcss-nested')]
3+
}

packages/vite/src/node/plugins/css.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -877,32 +877,23 @@ export function formatPostcssSourceMap(
877877
): ExistingRawSourceMap {
878878
const inputFileDir = path.dirname(file)
879879

880-
const sources: string[] = []
881-
const sourcesContent: string[] = []
882-
for (const [i, source] of rawMap.sources.entries()) {
883-
// remove <no source> from sources, to prevent source map to be combined incorrectly
884-
if (source === '<no source>') continue
885-
880+
const sources = rawMap.sources.map((source) => {
886881
const cleanSource = cleanUrl(decodeURIComponent(source))
887882

888883
// postcss returns virtual files
889884
if (/^<.+>$/.test(cleanSource)) {
890-
sources.push(`\0${cleanSource}`)
891-
} else {
892-
sources.push(normalizePath(path.resolve(inputFileDir, cleanSource)))
885+
return `\0${cleanSource}`
893886
}
894887

895-
if (rawMap.sourcesContent) {
896-
sourcesContent.push(rawMap.sourcesContent[i])
897-
}
898-
}
888+
return normalizePath(path.resolve(inputFileDir, cleanSource))
889+
})
899890

900891
return {
901892
file,
902893
mappings: rawMap.mappings,
903894
names: rawMap.names,
904895
sources,
905-
sourcesContent,
896+
sourcesContent: rawMap.sourcesContent,
906897
version: rawMap.version
907898
}
908899
}

pnpm-lock.yaml

Lines changed: 2 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)