Unverified Commit 708688dd authored by Gal Schlezinger's avatar Gal Schlezinger Committed by GitHub

[edge] enable edge compiler source maps by default (#38365)

since we no longer have a single compiler for browser & edge, we can enable
source map generation by default for all edge functions. This would make it
much easier to debug and understand what's happening when deploying to prod
as the log statements will show us the actual code location instead of post
bundling and minified location.

This also removes the experimental flag as it's not needed anymore.
parent f3969888
...@@ -1449,12 +1449,6 @@ export default async function getBaseWebpackConfig( ...@@ -1449,12 +1449,6 @@ export default async function getBaseWebpackConfig(
].filter(Boolean), ].filter(Boolean),
}, },
plugins: [ plugins: [
...(!dev &&
isEdgeServer &&
!!config.experimental.middlewareSourceMaps &&
!config.productionBrowserSourceMaps
? require('./webpack/plugins/middleware-source-maps-plugin').getMiddlewareSourceMapPlugins()
: []),
dev && isClient && new ReactRefreshWebpackPlugin(webpack), dev && isClient && new ReactRefreshWebpackPlugin(webpack),
// Makes sure `Buffer` and `process` are polyfilled in client and flight bundles (same behavior as webpack 4) // Makes sure `Buffer` and `process` are polyfilled in client and flight bundles (same behavior as webpack 4)
(isClient || isEdgeServer) && (isClient || isEdgeServer) &&
......
...@@ -32,8 +32,11 @@ export const base = curry(function base( ...@@ -32,8 +32,11 @@ export const base = curry(function base(
config.devtool = 'eval-source-map' config.devtool = 'eval-source-map'
} }
} else { } else {
// Enable browser sourcemaps: if (
if (ctx.productionBrowserSourceMaps && ctx.isClient) { ctx.isEdgeRuntime ||
// Enable browser sourcemaps:
(ctx.productionBrowserSourceMaps && ctx.isClient)
) {
config.devtool = 'source-map' config.devtool = 'source-map'
} else { } else {
config.devtool = false config.devtool = false
......
import { webpack } from 'next/dist/compiled/webpack/webpack'
import { MIDDLEWARE_LOCATION_REGEXP } from '../../../lib/constants'
import type { webpack5 } from 'next/dist/compiled/webpack/webpack'
/**
* Produce source maps for middlewares.
* Currently we use the same compiler for browser and middlewares,
*/
export const getMiddlewareSourceMapPlugins = () => {
return [
new webpack.SourceMapDevToolPlugin({
filename: '[file].map',
include: [
new RegExp(`^${MIDDLEWARE_LOCATION_REGEXP}\\.`),
/^edge-chunks\//,
],
}),
new MiddlewareSourceMapsPlugin(),
]
}
/**
* Produce source maps for middlewares.
* Currently we use the same compiler for browser and middlewares,
* so we can avoid having the custom plugins if the browser source maps
* are emitted.
*/
class MiddlewareSourceMapsPlugin {
apply(compiler: webpack5.Compiler): void {
const PLUGIN_NAME = 'NextJsMiddlewareSourceMapsPlugin'
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
compilation.hooks.buildModule.tap(PLUGIN_NAME, (module) => {
module.useSourceMap = module.layer === 'middleware'
})
})
}
}
...@@ -119,7 +119,6 @@ export interface ExperimentalConfig { ...@@ -119,7 +119,6 @@ export interface ExperimentalConfig {
unoptimized?: boolean unoptimized?: boolean
allowFutureImage?: boolean allowFutureImage?: boolean
} }
middlewareSourceMaps?: boolean
modularizeImports?: Record< modularizeImports?: Record<
string, string,
{ {
...@@ -549,7 +548,6 @@ export const defaultConfig: NextConfig = { ...@@ -549,7 +548,6 @@ export const defaultConfig: NextConfig = {
disablePostcssPresetEnv: undefined, disablePostcssPresetEnv: undefined,
amp: undefined, amp: undefined,
urlImports: undefined, urlImports: undefined,
middlewareSourceMaps: undefined,
modularizeImports: undefined, modularizeImports: undefined,
}, },
} }
......
...@@ -33,7 +33,10 @@ const clientGlobs = [ ...@@ -33,7 +33,10 @@ const clientGlobs = [
}, },
{ {
name: 'Middleware size', name: 'Middleware size',
globs: ['.next/server/middleware*', '.next/server/edge-runtime-webpack.js'], globs: [
'.next/server/middleware*.js',
'.next/server/edge-runtime-webpack.js',
],
}, },
] ]
......
...@@ -3,68 +3,47 @@ import { NextInstance } from 'test/lib/next-modes/base' ...@@ -3,68 +3,47 @@ import { NextInstance } from 'test/lib/next-modes/base'
import fs from 'fs-extra' import fs from 'fs-extra'
import path from 'path' import path from 'path'
const files = { describe('Middleware source maps', () => {
'pages/index.js': `
export default function () { return <div>Hello, world!</div> }
`,
'middleware.js': `
import { NextResponse } from "next/server";
export default function middleware() {
return NextResponse.next();
}
`,
}
describe('experimental.middlewareSourceMaps: true', () => {
let next: NextInstance let next: NextInstance
const nextConfig = { experimental: { middlewareSourceMaps: true } } beforeAll(async () => {
afterEach(() => next.destroy())
it('generates a source map', async () => {
next = await createNext({ nextConfig, files })
const middlewarePath = path.resolve(
next.testDir,
'.next/server/middleware.js'
)
expect(await fs.pathExists(middlewarePath)).toEqual(true)
expect(await fs.pathExists(`${middlewarePath}.map`)).toEqual(true)
})
it('generates a source map from src', async () => {
next = await createNext({ next = await createNext({
nextConfig, files: {
files: Object.fromEntries( 'pages/index.js': `
Object.entries(files).map(([filename, content]) => [ export default function () { return <div>Hello, world!</div> }
`src/${filename}`, `,
content, 'pages/api/edge.js': `
]) export const config = { runtime: 'experimental-edge' };
), export default function (req) {
return new Response("Hello from " + req.url);
}
`,
'middleware.js': `
import { NextResponse } from "next/server";
export default function middleware() {
return NextResponse.next();
}
`,
},
}) })
})
afterAll(() => next.destroy())
it('generates a source map for Middleware', async () => {
const middlewarePath = path.resolve( const middlewarePath = path.resolve(
next.testDir, next.testDir,
'.next/server/src/middleware.js' '.next/server/middleware.js'
) )
expect(await fs.pathExists(middlewarePath)).toEqual(true) expect(await fs.pathExists(middlewarePath)).toEqual(true)
expect(await fs.pathExists(`${middlewarePath}.map`)).toEqual(true) expect(await fs.pathExists(`${middlewarePath}.map`)).toEqual(true)
}) })
})
describe('experimental.middlewareSourceMaps: false', () => {
let next: NextInstance
afterEach(() => next.destroy())
it('does not generate a source map', async () => { it('generates a source map for Edge API', async () => {
next = await createNext({ files }) const edgePath = path.resolve(
const middlewarePath = path.resolve(
next.testDir, next.testDir,
'.next/server/middleware.js' '.next/server/pages/api/edge.js'
) )
expect(await fs.pathExists(middlewarePath)).toEqual(true) expect(await fs.pathExists(edgePath)).toEqual(true)
expect(await fs.pathExists(`${middlewarePath}.map`)).toEqual(false) expect(await fs.pathExists(`${edgePath}.map`)).toEqual(true)
}) })
}) })
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