From 2c75fa0d9ece05144574a1ee4624845e72f96681 Mon Sep 17 00:00:00 2001 From: Joe Haddad Date: Fri, 1 Jan 2021 15:30:50 -0500 Subject: [PATCH] fix(source map): correctly generate source maps (#20672) Fixes #20500 --- packages/next/build/webpack-config.ts | 4 +--- .../next/build/webpack/config/blocks/base.ts | 4 ++-- .../terser-webpack-plugin/src/index.js | 10 ++++------ .../test/index.test.js | 20 ++++++++++++++++++- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/packages/next/build/webpack-config.ts b/packages/next/build/webpack-config.ts index 39a1c1371b..28ee470026 100644 --- a/packages/next/build/webpack-config.ts +++ b/packages/next/build/webpack-config.ts @@ -211,8 +211,6 @@ export default async function getBaseWebpackConfig( rewrites: Rewrite[] } ): Promise { - const productionBrowserSourceMaps = - config.productionBrowserSourceMaps && !isServer let plugins: PluginMetaData[] = [] let babelPresetPlugins: { dir: string; config: any }[] = [] @@ -1216,7 +1214,7 @@ export default async function getBaseWebpackConfig( isServer, assetPrefix: config.assetPrefix || '', sassOptions: config.sassOptions, - productionBrowserSourceMaps, + productionBrowserSourceMaps: config.productionBrowserSourceMaps, }) let originalDevtool = webpackConfig.devtool diff --git a/packages/next/build/webpack/config/blocks/base.ts b/packages/next/build/webpack/config/blocks/base.ts index 386a303694..34b117a440 100644 --- a/packages/next/build/webpack/config/blocks/base.ts +++ b/packages/next/build/webpack/config/blocks/base.ts @@ -35,8 +35,8 @@ export const base = curry(function base( config.devtool = 'eval-source-map' } } else { - // Enable browser sourcemaps - if (ctx.productionBrowserSourceMaps) { + // Enable browser sourcemaps: + if (ctx.productionBrowserSourceMaps && ctx.isClient) { config.devtool = 'source-map' } else { config.devtool = false diff --git a/packages/next/build/webpack/plugins/terser-webpack-plugin/src/index.js b/packages/next/build/webpack/plugins/terser-webpack-plugin/src/index.js index d4d4edd3a1..0d9273ba4c 100644 --- a/packages/next/build/webpack/plugins/terser-webpack-plugin/src/index.js +++ b/packages/next/build/webpack/plugins/terser-webpack-plugin/src/index.js @@ -226,11 +226,9 @@ class TerserPlugin { return traceAsyncFn(assetSpan, async () => { if (!output) { - let inputSourceMap - const { source: sourceFromInputSource, - map, + map: inputSourceMap, } = inputSource.sourceAndMap() const input = Buffer.isBuffer(sourceFromInputSource) @@ -240,7 +238,7 @@ class TerserPlugin { const options = { name, input, - inputSourceMap: map, + inputSourceMap, terserOptions: { ...this.options.terserOptions }, } @@ -337,8 +335,8 @@ class TerserPlugin { }) const handleHashForChunk = (hash, chunk) => { - // increment 'b' to invalidate cache - hash.update('b') + // increment 'c' to invalidate cache + hash.update('c') } if (isWebpack5) { diff --git a/test/integration/production-browser-sourcemaps/test/index.test.js b/test/integration/production-browser-sourcemaps/test/index.test.js index fd9a9133fb..34670bc006 100644 --- a/test/integration/production-browser-sourcemaps/test/index.test.js +++ b/test/integration/production-browser-sourcemaps/test/index.test.js @@ -1,7 +1,7 @@ /* eslint-env jest */ import fs from 'fs-extra' import { join } from 'path' -import { nextBuild } from 'next-test-utils' +import { nextBuild, getPageFileFromBuildManifest } from 'next-test-utils' import { recursiveReadDir } from 'next/dist/lib/recursive-readdir' jest.setTimeout(1000 * 60 * 2) @@ -24,6 +24,24 @@ function runTests() { expect(browserFiles.includes(`${file}.map`)).toBe(true) }) }) + + it('correctly generated the source map', async () => { + const map = JSON.parse( + await fs.readFile( + join( + appDir, + '.next', + (await getPageFileFromBuildManifest(appDir, '/static')) + '.map' + ), + 'utf8' + ) + ) + + expect(map.sources).toContainEqual( + expect.stringMatching(/pages[/\\]static\.js/) + ) + expect(map.names).toContainEqual('StaticPage') + }) } describe('Production browser sourcemaps', () => { -- GitLab