Skip to content

Commit c751e11

Browse files
authored
Merge pull request #1909 from romainmenke/no-work-result-vs-lazy-result--inventive-pygmy-marmoset-5bbea7d7dd
Ensure that `NoWorkResult` and `LazyResult` have the same outcome.
2 parents a0d9f10 + 3c2fa2a commit c751e11

3 files changed

Lines changed: 80 additions & 2 deletions

File tree

lib/map-generator.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class MapGenerator {
1616
this.root = root
1717
this.opts = opts
1818
this.css = cssString
19+
this.originalCSS = cssString
1920
this.usesFileUrls = !this.mapOpts.from && this.mapOpts.absolute
2021

2122
this.memoizedFileURLs = new Map()
@@ -74,7 +75,7 @@ class MapGenerator {
7475
}
7576
}
7677
} else if (this.css) {
77-
this.css = this.css.replace(/(\n)?\/\*#[\S\s]*?\*\/$/gm, '')
78+
this.css = this.css.replace(/\n*?\/\*#[\S\s]*?\*\/$/gm, '')
7879
}
7980
}
8081

@@ -276,7 +277,7 @@ class MapGenerator {
276277
}
277278
})
278279
} else {
279-
let input = new Input(this.css, this.opts)
280+
let input = new Input(this.originalCSS, this.opts)
280281
if (input.map) this.previousMaps.push(input.map)
281282
}
282283
}

lib/no-work-result.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class NoWorkResult {
3737
if (generatedMap) {
3838
this.result.map = generatedMap
3939
}
40+
} else {
41+
map.clearAnnotation();
42+
this.result.css = map.css;
4043
}
4144
}
4245

test/no-work-result.test.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import postcss = require('../lib/postcss.js')
2+
import stringify = require('../lib/stringify.js')
13
import { spy } from 'nanospy'
24
import { SourceMapGenerator } from 'source-map-js'
35
import { test } from 'uvu'
46
import { equal, instance, is, not, throws, type } from 'uvu/assert'
57

68
import NoWorkResult from '../lib/no-work-result.js'
9+
import parse from '../lib/parse.js'
710
import Processor from '../lib/processor.js'
811

912
let processor = new Processor()
@@ -98,4 +101,75 @@ test('prints its object type', () => {
98101
is(Object.prototype.toString.call(result), '[object NoWorkResult]')
99102
})
100103

104+
test('no work result matches lazy result', async () => {
105+
let source = '.foo { color: red }\n';
106+
107+
let noWorkResult = await postcss([]).process(source, {
108+
from: 'foo.css',
109+
map: false
110+
});
111+
112+
let lazyResult = await postcss([]).process(source, {
113+
from: 'foo.css',
114+
map: false,
115+
syntax: { parse, stringify }
116+
});
117+
118+
equal(noWorkResult.css, lazyResult.css);
119+
})
120+
121+
// https://github.com/postcss/postcss/issues/1911
122+
test('no work result matches lazy result when map is true', async () => {
123+
let source = '.foo { color: red }\n';
124+
125+
let noWorkResult = await postcss([]).process(source, {
126+
from: 'foo.css',
127+
map: true
128+
});
129+
130+
equal(noWorkResult.css, '.foo { color: red }\n\n/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZvby5jc3MiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEiLCJmaWxlIjoiZm9vLmNzcyIsInNvdXJjZXNDb250ZW50IjpbIi5mb28geyBjb2xvcjogcmVkIH1cbiJdfQ== */');
131+
132+
let lazyResult = await postcss([]).process(source, {
133+
from: 'foo.css',
134+
map: true,
135+
syntax: { parse, stringify }
136+
});
137+
138+
equal(lazyResult.css, '.foo { color: red }\n\n/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZvby5jc3MiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxXQUFXIiwiZmlsZSI6ImZvby5jc3MiLCJzb3VyY2VzQ29udGVudCI6WyIuZm9vIHsgY29sb3I6IHJlZCB9XG4iXX0= */');
139+
})
140+
141+
test('no work result matches lazy result when the source contains an inline source map', async () => {
142+
let source = '.foo { color: red }\n\n/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZvby5jc3MiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxXQUFXIiwiZmlsZSI6ImZvby5jc3MiLCJzb3VyY2VzQ29udGVudCI6WyIuZm9vIHsgY29sb3I6IHJlZCB9XG4iXX0= */\n';
143+
144+
let noWorkResult = await postcss([]).process(source, {
145+
from: 'foo.css',
146+
map: false
147+
});
148+
149+
let lazyResult = await postcss([]).process(source, {
150+
from: 'foo.css',
151+
map: false,
152+
syntax: { parse, stringify }
153+
});
154+
155+
equal(noWorkResult.css, lazyResult.css);
156+
})
157+
158+
test('no work result matches lazy result when map is true and the source contains an inline source map', async () => {
159+
let source = '.foo { color: red }\n\n/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZvby5jc3MiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxXQUFXIiwiZmlsZSI6ImZvby5jc3MiLCJzb3VyY2VzQ29udGVudCI6WyIuZm9vIHsgY29sb3I6IHJlZCB9XG4iXX0= */\n';
160+
161+
let lazyResult = await postcss([]).process(source, {
162+
from: 'bar.css',
163+
map: true,
164+
syntax: { parse, stringify }
165+
});
166+
167+
let noWorkResult = await postcss([]).process(source, {
168+
from: 'bar.css',
169+
map: true
170+
});
171+
172+
equal(noWorkResult.css, lazyResult.css);
173+
})
174+
101175
test.run()

0 commit comments

Comments
 (0)