Skip to content

Commit f23f935

Browse files
authored
Turbopack: respect productionBrowserSourceMaps (#78014)
Let's try this again ~~Blocked by #78058 Blocked by #82448 - [x] Add regression test - [x] Update failing assertions Closes PACK-4304
1 parent 0ec5511 commit f23f935

File tree

8 files changed

+88
-103
lines changed

8 files changed

+88
-103
lines changed

crates/next-core/src/next_config.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,12 +1701,12 @@ impl NextConfig {
17011701
}
17021702

17031703
#[turbo_tasks::function]
1704-
pub fn client_source_maps(&self, _mode: Vc<NextMode>) -> Result<Vc<bool>> {
1705-
// Temporarily always enable client source maps as tests regress.
1706-
// TODO: Respect both `self.experimental.turbopack_source_maps` and
1707-
// `self.production_browser_source_maps`
1704+
pub async fn client_source_maps(&self, mode: Vc<NextMode>) -> Result<Vc<bool>> {
17081705
let source_maps = self.experimental.turbopack_source_maps;
1709-
Ok(Vc::cell(source_maps.unwrap_or(true)))
1706+
Ok(Vc::cell(source_maps.unwrap_or(match &*mode.await? {
1707+
NextMode::Development => true,
1708+
NextMode::Build => self.production_browser_source_maps,
1709+
})))
17101710
}
17111711

17121712
#[turbo_tasks::function]

test/integration/css-fixtures/next.config.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,5 @@ module.exports = {
33
// Make sure entries are not getting disposed.
44
maxInactiveAge: 1000 * 60 * 60,
55
},
6-
webpack(cfg) {
7-
cfg.devtool = 'source-map'
8-
return cfg
9-
},
6+
productionBrowserSourceMaps: true,
107
}

test/integration/css/test/css-compilation.test.js

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -116,39 +116,8 @@ module.exports = {
116116
delete sourceMapContentParsed.file
117117
delete sourceMapContentParsed.sources
118118

119-
if (process.env.IS_TURBOPACK_TEST && useLightningcss) {
120-
expect(sourceMapContentParsed).toMatchInlineSnapshot(`
121-
{
122-
"mappings": "AAAA,qDACE,2BAKF,0DAIA,kDAIA,uCAIA",
123-
"names": [],
124-
"sourcesContent": [
125-
"@media (480px <= width < 768px) {
126-
::placeholder {
127-
color: green;
128-
}
129-
}
130-
131-
.flex-parsing {
132-
flex: 0 0 calc(50% - var(--vertical-gutter));
133-
}
134-
135-
.transform-parsing {
136-
transform: translate3d(0px, 0px);
137-
}
138-
139-
.css-grid-shorthand {
140-
grid-column: span 2;
141-
}
142-
143-
.g-docs-sidenav .filter::-webkit-input-placeholder {
144-
opacity: 80%;
145-
}
146-
",
147-
],
148-
"version": 3,
149-
}
150-
`)
151-
} else if (process.env.IS_TURBOPACK_TEST && !useLightningcss) {
119+
if (process.env.IS_TURBOPACK_TEST) {
120+
// Turbopack always uses lightningcss
152121
expect(sourceMapContentParsed).toMatchInlineSnapshot(`
153122
{
154123
"mappings": "AAAA,qDACE,2BAKF,0DAIA,kDAIA,uCAIA",
@@ -183,6 +152,7 @@ module.exports = {
183152
} else if (useLightningcss) {
184153
expect(sourceMapContentParsed).toMatchInlineSnapshot(`
185154
{
155+
"ignoreList": [],
186156
"mappings": "AAAA,qDACE,cACE,WACF,CACF,CAEA,cACE,2CACF,CAEA,mBACE,0BACF,CAEA,oBACE,kBACF,CAEA,mDACE,UACF",
187157
"names": [],
188158
"sourceRoot": "",
@@ -217,6 +187,7 @@ module.exports = {
217187
} else {
218188
expect(sourceMapContentParsed).toMatchInlineSnapshot(`
219189
{
190+
"ignoreList": [],
220191
"mappings": "AAAA,+CACE,cACE,WACF,CACF,CAEA,cACE,2CACF,CAEA,mBACE,0BACF,CAEA,oBACE,kBACF,CAEA,mDACE,WACF",
221192
"names": [],
222193
"sourceRoot": "",

test/integration/production-browser-sourcemaps/next.config.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

test/integration/production-browser-sourcemaps/test/index.test.js

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import fs from 'fs-extra'
2+
import path from 'path'
3+
import { getBuildManifest } from 'next-test-utils'
4+
import { recursiveReadDir } from 'next/dist/lib/recursive-readdir'
5+
import { nextTestSetup } from 'e2e-utils'
6+
7+
function extractSourceMappingURL(jsContent): string | null {
8+
// Matches both //# and //@ sourceMappingURL=...
9+
const match = jsContent.match(/\/\/[#@] sourceMappingURL=([^\s]+)/)
10+
return match ? match[1] : null
11+
}
12+
13+
async function sourceMapExistsForFile(jsFilePath) {
14+
const jsContent = await fs.readFile(jsFilePath, 'utf8')
15+
const sourceMappingURL = extractSourceMappingURL(jsContent)
16+
if (!sourceMappingURL) {
17+
if (/^__turbopack_load_page_chunks__\(["']/.test(jsContent)) {
18+
// There is no sourcemap in these loader chunks, ignore
19+
return undefined
20+
}
21+
return false
22+
}
23+
const mapPath = path.join(path.dirname(jsFilePath), sourceMappingURL)
24+
return await fs.pathExists(mapPath)
25+
}
26+
27+
describe('Production browser sourcemaps', () => {
28+
describe.each([false, true] as const)(
29+
'productionBrowserSourceMaps = %s',
30+
(productionBrowserSourceMaps) => {
31+
const { next, skipped } = nextTestSetup({
32+
files: __dirname,
33+
nextConfig: {
34+
productionBrowserSourceMaps,
35+
},
36+
skipDeployment: true,
37+
})
38+
39+
if (skipped) {
40+
return
41+
}
42+
43+
it('check sourcemaps for all browser files', async () => {
44+
const buildManifest = getBuildManifest(next.testDir)
45+
46+
// These currently don't have sourcemaps
47+
let polyfillFiles = new Set(
48+
buildManifest.polyfillFiles.map((f) => '/' + path.basename(f))
49+
)
50+
51+
const staticDir = path.join(next.testDir, '.next', 'static', 'chunks')
52+
const browserFiles = await recursiveReadDir(staticDir)
53+
const jsFiles = browserFiles.filter(
54+
(file) => file.endsWith('.js') && !polyfillFiles.has(file)
55+
)
56+
expect(jsFiles).not.toBeEmpty()
57+
58+
for (const file of jsFiles) {
59+
const jsPath = path.join(staticDir, file)
60+
expect(await sourceMapExistsForFile(jsPath)).toBeOneOf([
61+
productionBrowserSourceMaps,
62+
undefined,
63+
])
64+
}
65+
66+
for (let page of ['/ssr', '/static']) {
67+
const jsFiles = buildManifest.pages[page]
68+
for (const file of jsFiles) {
69+
const jsPath = path.join(next.testDir, '.next', file)
70+
expect(await sourceMapExistsForFile(jsPath)).toBe(
71+
productionBrowserSourceMaps
72+
)
73+
}
74+
}
75+
})
76+
}
77+
)
78+
})

0 commit comments

Comments
 (0)