Skip to content

Commit 13e2dd8

Browse files
authored
Merge pull request #2268 from didi/fix-style-parser-syntax-error
fix: 单行注释在解析后导致后续的样式失效
2 parents 4a21ced + 3c26f18 commit 13e2dd8

17 files changed

Lines changed: 155 additions & 14 deletions

File tree

packages/webpack-plugin/lib/style-compiler/strip-conditional-loader.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,17 @@ async function stripByPostcss(options) {
193193
const syntax = styleSyntaxProcesserMap[options.lang]?.()
194194
const defs = options.defs ?? {}
195195

196-
const afterConditionStrip = stripCondition(options.css, defs)
196+
function stripContentCondition(content) {
197+
content = stripCondition(content, defs)
198+
199+
if (options.lang === 'stylus') {
200+
content = content.replace(/\t/g, ' ')
201+
}
202+
203+
return content
204+
}
205+
206+
const afterConditionStrip = stripContentCondition(options.css, defs)
197207

198208
/**
199209
* @type {import('postcss').AcceptedPlugin[]}
@@ -204,7 +214,7 @@ async function stripByPostcss(options) {
204214
let content = await fs.readFile(filename, 'utf-8')
205215
const processer = postcss(plugins)
206216

207-
content = stripCondition(content, defs)
217+
content = stripContentCondition(content, defs)
208218

209219
const { css } = await processer.process(content, {
210220
syntax,
@@ -228,11 +238,23 @@ async function stripByPostcss(options) {
228238
})
229239
})
230240
}
231-
})
241+
}),
242+
{
243+
// less/scss syntax 在 postcss 重新生成 css 后,`//` 注释后面不会保留换行,会和后续的 css 语句和注释连在一起,导致后续语法错误
244+
postcssPlugin: 'mpx-strip-conditional-loader-append-command',
245+
CommentExit(comment) {
246+
comment.raws.right ??= ''
247+
248+
if (comment.raws.right.endsWith('\n')) {
249+
return
250+
}
251+
252+
comment.raws.right += '\n'
253+
}
254+
}
232255
]
233256

234257
const processer = postcss(plugins)
235-
236258
return processer.process(afterConditionStrip, {
237259
from: options.resourcePath,
238260
syntax

packages/webpack-plugin/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
"@better-scroll/zoom": "^2.5.1",
3131
"@mpxjs/template-engine": "^2.8.7",
3232
"@mpxjs/utils": "^2.10.16",
33+
"postcss-less": "^6.0.0",
34+
"postcss-scss": "^4.0.9",
35+
"postcss-styl": "^0.12.3",
3336
"acorn": "^8.11.3",
3437
"acorn-walk": "^7.2.0",
3538
"async": "^2.6.0",

packages/webpack-plugin/test/platform/common/fixtures/css-condition/comment/index.styl

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@name: red; // hello world
2+
3+
.selector {
4+
color: blue // hello world
5+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## Config
2+
3+
```json
4+
{
5+
"lang": "less",
6+
"resourcePath": "index.less",
7+
"defs": {}
8+
}
9+
```
10+
11+
## Result
12+
13+
```less
14+
@name: red; // hello world
15+
.selector {
16+
color: blue // hello world
17+
}
18+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
$color: red; // this is a comment
3+
.selector {
4+
color: $color; // another comment
5+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## Config
2+
3+
```json
4+
{
5+
"lang": "scss",
6+
"resourcePath": "index.scss",
7+
"defs": {}
8+
}
9+
```
10+
11+
## Result
12+
13+
```scss
14+
15+
$color: red; // this is a comment
16+
.selector {
17+
color: $color; // another comment
18+
19+
}
20+
```

packages/webpack-plugin/test/platform/common/fixtures/css-condition/comment/config.json renamed to packages/webpack-plugin/test/platform/common/fixtures/css-condition/comment/stylus/config.json

File renamed without changes.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @import "./s1.styl"
2+
3+
/**
4+
@import "./s2.styl"
5+
*/
6+
7+
@import "./s3.styl"
8+
9+
$namespace = red // hello world
10+
.selector1 // hello world
11+
color: blue // hello world
12+
.selector2
13+
color: red

packages/webpack-plugin/test/platform/common/fixtures/css-condition/comment/output.md renamed to packages/webpack-plugin/test/platform/common/fixtures/css-condition/comment/stylus/output.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,19 @@
1515
```stylus
1616
// @import "./s1.styl"
1717
18+
1819
/**
1920
@import "./s2.styl"
2021
*/
2122
2223
.s3
2324
color green
2425
26+
$namespace = red // hello world
27+
28+
.selector1 // hello world
29+
color: blue // hello world
30+
31+
.selector2
32+
color: red
2533
```

0 commit comments

Comments
 (0)