Fix source maps issue resulting in a crash (#11319)

* only try to pretty print `err.source` if it exists

* fix small typo

* drop unnecessary path resolving

* add `from` and `to` to `run` test helper

* enable all source map tests

* ensure source maps are generated properly

* update annotation values in tests

* handle `map: true`

* add test for `map: { inline: true }`

* force Lightning CSS to handle an inline source map

* base64 encode the test name

This will get rid of special characters that can cause issues /
confusion when debugging tests.

* ensure utilities and components with variants are pointing to the correct source

Co-authored-by: Jordan Pittman <jordan@cryptica.me>

* update changelog

---------

Co-authored-by: Jordan Pittman <jordan@cryptica.me>
This commit is contained in:
Robin Malfait 2023-05-31 18:28:11 +02:00 • committed by GitHub
parent ecebde1d52
commit 352c563d99
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 419 additions and 416 deletions

View file

@ -40,38 +40,52 @@ module.exports = function tailwindcss(configOrPath) {
processTailwindFeatures(context)(root, result)
},
function lightningCssPlugin(_root, result) {
let map = result.map ?? result.opts.map
let intermediateResult = result.root.toResult({
map: map ? { inline: true } : false,
})
let intermediateMap = intermediateResult.map?.toJSON?.() ?? map
try {
let transformed = lightningcss.transform({
filename: result.opts.from,
code: Buffer.from(result.root.toString()),
code: Buffer.from(intermediateResult.css),
minify: false,
sourceMap: !!result.map,
inputSourceMap: result.map ? result.map.toString() : undefined,
sourceMap: !!intermediateMap,
targets:
typeof process !== 'undefined' && process.env.JEST_WORKER_ID
? { chrome: 111 << 16 }
: lightningcss.browserslistToTargets(
browserslist(require('../package.json').browserslist)
),
drafts: {
nesting: true,
customMedia: true,
},
})
result.map = Object.assign(result.map ?? {}, {
toJSON() {
return transformed.map.toJSON()
},
toString() {
return transformed.map.toString()
},
})
let code = transformed.code.toString()
result.root = postcss.parse(transformed.code.toString('utf8'))
// https://postcss.org/api/#sourcemapoptions
if (intermediateMap && transformed.map != null) {
let prev = transformed.map.toString()
if (typeof intermediateMap === 'object') {
intermediateMap.prev = prev
} else {
code = `${code}\n/*# sourceMappingURL=data:application/json;base64,${Buffer.from(
prev
).toString('base64')} */`
}
}
result.root = postcss.parse(code, {
...result.opts,
map: intermediateMap,
})
} catch (err) {
if (typeof process !== 'undefined' && process.env.JEST_WORKER_ID) {
if (err.source && typeof process !== 'undefined' && process.env.JEST_WORKER_ID) {
let lines = err.source.split('\n')
err = new Error(
[