Unverified Commit f23f9354 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: respect `productionBrowserSourceMaps` (#78014)

Let's try this again

~~Blocked by https://github.com/vercel/next.js/pull/78058~~
Blocked by https://github.com/vercel/next.js/pull/82448

- [x] Add regression test
- [x] Update failing assertions

Closes PACK-4304
parent 0ec5511d
...@@ -1701,12 +1701,12 @@ impl NextConfig { ...@@ -1701,12 +1701,12 @@ impl NextConfig {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn client_source_maps(&self, _mode: Vc<NextMode>) -> Result<Vc<bool>> { pub async fn client_source_maps(&self, mode: Vc<NextMode>) -> Result<Vc<bool>> {
// Temporarily always enable client source maps as tests regress.
// TODO: Respect both `self.experimental.turbopack_source_maps` and
// `self.production_browser_source_maps`
let source_maps = self.experimental.turbopack_source_maps; let source_maps = self.experimental.turbopack_source_maps;
Ok(Vc::cell(source_maps.unwrap_or(true))) Ok(Vc::cell(source_maps.unwrap_or(match &*mode.await? {
NextMode::Development => true,
NextMode::Build => self.production_browser_source_maps,
})))
} }
#[turbo_tasks::function] #[turbo_tasks::function]
......
...@@ -3,8 +3,5 @@ module.exports = { ...@@ -3,8 +3,5 @@ module.exports = {
// Make sure entries are not getting disposed. // Make sure entries are not getting disposed.
maxInactiveAge: 1000 * 60 * 60, maxInactiveAge: 1000 * 60 * 60,
}, },
webpack(cfg) { productionBrowserSourceMaps: true,
cfg.devtool = 'source-map'
return cfg
},
} }
...@@ -116,39 +116,8 @@ module.exports = { ...@@ -116,39 +116,8 @@ module.exports = {
delete sourceMapContentParsed.file delete sourceMapContentParsed.file
delete sourceMapContentParsed.sources delete sourceMapContentParsed.sources
if (process.env.IS_TURBOPACK_TEST && useLightningcss) { if (process.env.IS_TURBOPACK_TEST) {
expect(sourceMapContentParsed).toMatchInlineSnapshot(` // Turbopack always uses lightningcss
{
"mappings": "AAAA,qDACE,2BAKF,0DAIA,kDAIA,uCAIA",
"names": [],
"sourcesContent": [
"@media (480px <= width < 768px) {
::placeholder {
color: green;
}
}
.flex-parsing {
flex: 0 0 calc(50% - var(--vertical-gutter));
}
.transform-parsing {
transform: translate3d(0px, 0px);
}
.css-grid-shorthand {
grid-column: span 2;
}
.g-docs-sidenav .filter::-webkit-input-placeholder {
opacity: 80%;
}
",
],
"version": 3,
}
`)
} else if (process.env.IS_TURBOPACK_TEST && !useLightningcss) {
expect(sourceMapContentParsed).toMatchInlineSnapshot(` expect(sourceMapContentParsed).toMatchInlineSnapshot(`
{ {
"mappings": "AAAA,qDACE,2BAKF,0DAIA,kDAIA,uCAIA", "mappings": "AAAA,qDACE,2BAKF,0DAIA,kDAIA,uCAIA",
...@@ -183,6 +152,7 @@ module.exports = { ...@@ -183,6 +152,7 @@ module.exports = {
} else if (useLightningcss) { } else if (useLightningcss) {
expect(sourceMapContentParsed).toMatchInlineSnapshot(` expect(sourceMapContentParsed).toMatchInlineSnapshot(`
{ {
"ignoreList": [],
"mappings": "AAAA,qDACE,cACE,WACF,CACF,CAEA,cACE,2CACF,CAEA,mBACE,0BACF,CAEA,oBACE,kBACF,CAEA,mDACE,UACF", "mappings": "AAAA,qDACE,cACE,WACF,CACF,CAEA,cACE,2CACF,CAEA,mBACE,0BACF,CAEA,oBACE,kBACF,CAEA,mDACE,UACF",
"names": [], "names": [],
"sourceRoot": "", "sourceRoot": "",
...@@ -217,6 +187,7 @@ module.exports = { ...@@ -217,6 +187,7 @@ module.exports = {
} else { } else {
expect(sourceMapContentParsed).toMatchInlineSnapshot(` expect(sourceMapContentParsed).toMatchInlineSnapshot(`
{ {
"ignoreList": [],
"mappings": "AAAA,+CACE,cACE,WACF,CACF,CAEA,cACE,2CACF,CAEA,mBACE,0BACF,CAEA,oBACE,kBACF,CAEA,mDACE,WACF", "mappings": "AAAA,+CACE,cACE,WACF,CACF,CAEA,cACE,2CACF,CAEA,mBACE,0BACF,CAEA,oBACE,kBACF,CAEA,mDACE,WACF",
"names": [], "names": [],
"sourceRoot": "", "sourceRoot": "",
......
module.exports = {
productionBrowserSourceMaps: true,
}
/* eslint-env jest */
import fs from 'fs-extra'
import { dirname, join } from 'path'
import { nextBuild, getPageFilesFromBuildManifest } from 'next-test-utils'
import { recursiveReadDir } from 'next/dist/lib/recursive-readdir'
const appDir = join(__dirname, '../')
function extractSourceMappingURL(jsContent) {
// Matches both //# and //@ sourceMappingURL=...
const match = jsContent.match(/\/\/[#@] sourceMappingURL=([^\s]+)/)
return match ? match[1] : null
}
async function checkSourceMapExistsForFile(jsFilePath) {
const jsContent = await fs.readFile(jsFilePath, 'utf8')
const sourceMappingURL = extractSourceMappingURL(jsContent)
if (!sourceMappingURL) {
return
}
expect(sourceMappingURL).toBeTruthy()
const mapPath = join(dirname(jsFilePath), sourceMappingURL)
expect(await fs.pathExists(mapPath)).toBe(true)
}
describe('Production browser sourcemaps', () => {
;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode',
() => {
describe('Server support', () => {
beforeAll(async () => {
await nextBuild(appDir, [], {})
})
it('includes sourcemaps for all browser files', async () => {
const staticDir = join(appDir, '.next', 'static', 'chunks')
const browserFiles = await recursiveReadDir(staticDir)
const jsFiles = browserFiles.filter((file) => file.endsWith('.js'))
expect(jsFiles).not.toBeEmpty()
for (const file of jsFiles) {
const jsPath = join(staticDir, file)
checkSourceMapExistsForFile(jsPath)
}
})
it('correctly generated the source map', async () => {
const dotNextDir = join(appDir, '.next')
const jsFiles = getPageFilesFromBuildManifest(appDir, '/static')
for (const file of jsFiles) {
const jsPath = join(dotNextDir, file)
checkSourceMapExistsForFile(jsPath)
}
})
})
}
)
})
import fs from 'fs-extra'
import path from 'path'
import { getBuildManifest } from 'next-test-utils'
import { recursiveReadDir } from 'next/dist/lib/recursive-readdir'
import { nextTestSetup } from 'e2e-utils'
function extractSourceMappingURL(jsContent): string | null {
// Matches both //# and //@ sourceMappingURL=...
const match = jsContent.match(/\/\/[#@] sourceMappingURL=([^\s]+)/)
return match ? match[1] : null
}
async function sourceMapExistsForFile(jsFilePath) {
const jsContent = await fs.readFile(jsFilePath, 'utf8')
const sourceMappingURL = extractSourceMappingURL(jsContent)
if (!sourceMappingURL) {
if (/^__turbopack_load_page_chunks__\(["']/.test(jsContent)) {
// There is no sourcemap in these loader chunks, ignore
return undefined
}
return false
}
const mapPath = path.join(path.dirname(jsFilePath), sourceMappingURL)
return await fs.pathExists(mapPath)
}
describe('Production browser sourcemaps', () => {
describe.each([false, true] as const)(
'productionBrowserSourceMaps = %s',
(productionBrowserSourceMaps) => {
const { next, skipped } = nextTestSetup({
files: __dirname,
nextConfig: {
productionBrowserSourceMaps,
},
skipDeployment: true,
})
if (skipped) {
return
}
it('check sourcemaps for all browser files', async () => {
const buildManifest = getBuildManifest(next.testDir)
// These currently don't have sourcemaps
let polyfillFiles = new Set(
buildManifest.polyfillFiles.map((f) => '/' + path.basename(f))
)
const staticDir = path.join(next.testDir, '.next', 'static', 'chunks')
const browserFiles = await recursiveReadDir(staticDir)
const jsFiles = browserFiles.filter(
(file) => file.endsWith('.js') && !polyfillFiles.has(file)
)
expect(jsFiles).not.toBeEmpty()
for (const file of jsFiles) {
const jsPath = path.join(staticDir, file)
expect(await sourceMapExistsForFile(jsPath)).toBeOneOf([
productionBrowserSourceMaps,
undefined,
])
}
for (let page of ['/ssr', '/static']) {
const jsFiles = buildManifest.pages[page]
for (const file of jsFiles) {
const jsPath = path.join(next.testDir, '.next', file)
expect(await sourceMapExistsForFile(jsPath)).toBe(
productionBrowserSourceMaps
)
}
}
})
}
)
})
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