Skip to content

Commit 3e82edc

Browse files
authored
Keep non-annotation comments when the processor has no plugins (#2150)
clearAnnotation() has two implementations. The AST one removes only a comment whose text starts with `# sourceMappingURL=`, and PreviousMap#loadAnnotation finds annotations by the same marker. The string one, which is the only path NoWorkResult can take, searched for `/*#` and removed every comment that began with it. So postcss().process(css) silently deleted `/*#region */` and `/*#endregion */` folding markers, and any other comment starting with `#`, while the same CSS through a processor with one no-op plugin kept them. It happens with `map` unset too, because NoWorkResult calls clearAnnotation() unconditionally. Searching for the whole `/*# sourceMappingURL=` marker lines the string path up with the other two and keeps the plain string scan. Co-authored-by: Dylan Pulver <dylanpulver@users.noreply.github.com>
1 parent 6d23bc3 commit 3e82edc

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

‎lib/map-generator.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ class MapGenerator {
7575
}
7676
}
7777
} else if (this.css) {
78+
let annotation = '/*# sourceMappingURL='
7879
let startIndex
79-
while ((startIndex = this.css.lastIndexOf('/*#')) !== -1) {
80-
let endIndex = this.css.indexOf('*/', startIndex + 3)
80+
while ((startIndex = this.css.lastIndexOf(annotation)) !== -1) {
81+
let endIndex = this.css.indexOf('*/', startIndex + annotation.length)
8182
if (endIndex === -1) break
8283
while (startIndex > 0 && this.css[startIndex - 1] === '\n') {
8384
startIndex--

‎test/map.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,20 @@ test('generates correct inline map and multiple comments', () => {
693693
match(result.css, /a {}\nb {}\n\/\*# sourceMappingURL=/)
694694
})
695695

696+
test('keeps non-annotation comments with empty processor', () => {
697+
let css = '/*#region layout */\na {}\n/*#endregion */\n'
698+
let result = postcss().process(css, { from: undefined })
699+
700+
is(result.css, css)
701+
})
702+
703+
test('clears the annotation but keeps other comments after it', () => {
704+
let css = 'a {}\n/*# sourceMappingURL=a.css.map */\n/*#endregion */\n'
705+
let result = postcss().process(css, { from: undefined })
706+
707+
is(result.css, 'a {}\n/*#endregion */\n')
708+
})
709+
696710
test('generates correct sources with empty processor', () => {
697711
let result = postcss().process('a {} /*hello world*/', {
698712
from: 'a.css',

0 commit comments

Comments
 (0)