Unverified Commit 1a0d149c authored by Gal Schlezinger's avatar Gal Schlezinger Committed by GitHub

produce source maps for middlewares (#34409)

This PR will generate source maps for middlewares. It's not under any flag (should it be?)

Closes #34523 
parent cc5345bb
...@@ -51,6 +51,7 @@ import { getRawPageExtensions } from './utils' ...@@ -51,6 +51,7 @@ import { getRawPageExtensions } from './utils'
import browserslist from 'next/dist/compiled/browserslist' import browserslist from 'next/dist/compiled/browserslist'
import loadJsConfig from './load-jsconfig' import loadJsConfig from './load-jsconfig'
import { shouldUseReactRoot } from '../server/config' import { shouldUseReactRoot } from '../server/config'
import { getMiddlewareSourceMapPlugins } from './webpack/plugins/middleware-source-maps-plugin'
const watchOptions = Object.freeze({ const watchOptions = Object.freeze({
aggregateTimeout: 5, aggregateTimeout: 5,
...@@ -1272,6 +1273,12 @@ export default async function getBaseWebpackConfig( ...@@ -1272,6 +1273,12 @@ export default async function getBaseWebpackConfig(
].filter(Boolean), ].filter(Boolean),
}, },
plugins: [ plugins: [
...(!dev &&
!isServer &&
!!config.experimental.middlewareSourceMaps &&
!config.productionBrowserSourceMaps
? getMiddlewareSourceMapPlugins()
: []),
hasReactRefresh && new ReactRefreshWebpackPlugin(webpack), hasReactRefresh && 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)
targetWeb && targetWeb &&
......
import { webpack } from 'next/dist/compiled/webpack/webpack'
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: [
// Middlewares are the only ones who have `server/pages/[name]` as their filename
/^server\/pages\//,
// All middleware chunks
/^server\/middleware-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'
})
})
}
}
...@@ -106,6 +106,7 @@ export interface ExperimentalConfig { ...@@ -106,6 +106,7 @@ export interface ExperimentalConfig {
urlImports?: NonNullable<webpack5.Configuration['experiments']>['buildHttp'] urlImports?: NonNullable<webpack5.Configuration['experiments']>['buildHttp']
outputFileTracingRoot?: string outputFileTracingRoot?: string
outputStandalone?: boolean outputStandalone?: boolean
middlewareSourceMaps?: boolean
} }
/** /**
......
import { createNext } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import fs from 'fs-extra'
import path from 'path'
describe('experimental.middlewareSourceMaps: true', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
nextConfig: {
experimental: {
middlewareSourceMaps: true,
},
},
files: {
'pages/_middleware.js': `
export default function middleware() {
return new Response("Hello, world!");
}
`,
},
dependencies: {},
})
})
afterAll(() => next.destroy())
it('generates a source map', async () => {
const middlewarePath = path.resolve(
next.testDir,
'.next/server/pages/_middleware.js'
)
expect(await fs.pathExists(middlewarePath)).toEqual(true)
expect(await fs.pathExists(`${middlewarePath}.map`)).toEqual(true)
})
})
describe('experimental.middlewareSourceMaps: false', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: {
'pages/_middleware.js': `
export default function middleware() {
return new Response("Hello, world!");
}
`,
},
dependencies: {},
})
})
afterAll(() => next.destroy())
it('does not generate a source map', async () => {
const middlewarePath = path.resolve(
next.testDir,
'.next/server/pages/_middleware.js'
)
expect(await fs.pathExists(middlewarePath)).toEqual(true)
expect(await fs.pathExists(`${middlewarePath}.map`)).toEqual(false)
})
})
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