|
| 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