Unverified Commit 0ac302d3 authored by Xanaado's avatar Xanaado Committed by GitHub

Turbopack: fix `source` field in CSS sourcemaps (#88225)

Closes #88294


Currently, CSS source maps in Turbopack always use index `0` for all
source file references. This can lead to misleading mappings and makes
debugging CSS generated from multiple sources more difficult.
parent 00b29d66
import React from 'react'
import App from 'next/app'
import '../styles/global.scss'
class MyApp extends App {
render() {
const { Component, pageProps } = this.props
return <Component {...pageProps} />
}
}
export default MyApp
export default function Home() {
return (
<>
<div className="from-main">main</div>
<div className="from-partial">partial</div>
</>
)
}
/* eslint-env jest */
import { nextTestSetup } from 'e2e-utils'
describe('SCSS Multi-Source Sourcemap', () => {
const { next } = nextTestSetup({
files: __dirname,
dependencies: { sass: '1.54.0' },
nextConfig: {
productionBrowserSourceMaps: true,
},
})
it('emits a sourcemap whose mappings reference each combined source', async () => {
const $ = await next.render$('/')
const cssSheet = $('link[rel="stylesheet"]')
expect(cssSheet.length).toBe(1)
const stylesheetUrl = cssSheet.attr('href')
const cssContent = await next.fetch(stylesheetUrl).then((res) => res.text())
// Sanity-check both rules are emitted into a single bundle.
const cssContentWithoutSourceMap = cssContent
.replace(/\/\*.*?\*\//g, '')
.trim()
expect(cssContentWithoutSourceMap).toContain('.from-partial')
expect(cssContentWithoutSourceMap).toContain('.from-main')
// Contains a source map.
expect(cssContent).toMatch(/\/\*#\s*sourceMappingURL=(.+\.map)\s*\*\//)
const sourceMapUrl = /\/\*#\s*sourceMappingURL=(.+\.map)\s*\*\//.exec(
cssContent
)[1]
const actualSourceMapUrl = stylesheetUrl.replace(
/(?<=^|\/)[^/?]+(?=$|\?)/,
sourceMapUrl
)
const sourceMapContent = await next
.fetch(actualSourceMapUrl)
.then((res) => res.text())
const sourceMapContentParsed = JSON.parse(sourceMapContent)
// With multiple inputs combined via `@import`, both source files must
// be declared as sources and their content preserved.
expect(sourceMapContentParsed.sources).toEqual(
expect.arrayContaining([
expect.stringContaining('global.scss'),
expect.stringContaining('_partial.scss'),
])
)
expect(sourceMapContentParsed.sourcesContent).toEqual(
expect.arrayContaining([
expect.stringContaining('.from-partial'),
expect.stringContaining('.from-main'),
])
)
// Ensure it doesn't have a specific path in the snapshot.
delete sourceMapContentParsed.file
delete sourceMapContentParsed.sources
if (process.env.IS_TURBOPACK_TEST) {
expect(sourceMapContentParsed).toMatchInlineSnapshot(`
{
"mappings": "AAAA,wBCEA",
"names": [],
"sourcesContent": [
".from-partial {
color: red;
}
",
"@import './partial';
.from-main {
color: blue;
}
",
],
"version": 3,
}
`)
} else if (process.env.NEXT_RSPACK) {
expect(sourceMapContentParsed).toMatchInlineSnapshot(`
{
"mappings": "AAAA,cACE,SAAA,CCCF,WACE,UAAA",
"names": [],
"sourcesContent": [
".from-partial {
color: red;
}
",
"@import './partial';
.from-main {
color: blue;
}
",
],
"version": 3,
}
`)
} else {
expect(sourceMapContentParsed).toMatchInlineSnapshot(`
{
"ignoreList": [],
"mappings": "AAAA,cACE,SAAA,CCCF,WACE,UAAA",
"names": [],
"sourceRoot": "",
"sourcesContent": [
".from-partial {
color: red;
}
",
"@import './partial';
.from-main {
color: blue;
}
",
],
"version": 3,
}
`)
}
})
})
@import './partial';
.from-main {
color: blue;
}
...@@ -780,7 +780,7 @@ fn generate_css_source_map( ...@@ -780,7 +780,7 @@ fn generate_css_source_map(
m.generated_column, m.generated_column,
m.original.map(|v| v.original_line).unwrap_or_default(), m.original.map(|v| v.original_line).unwrap_or_default(),
m.original.map(|v| v.original_column).unwrap_or_default(), m.original.map(|v| v.original_column).unwrap_or_default(),
Some(0), m.original.map(|v| v.source).or(Some(0)),
None, None,
false, false,
); );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment