Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

produce source maps for middlewares #34409

Merged
merged 8 commits into from Feb 24, 2022
5 changes: 5 additions & 0 deletions packages/next/build/webpack-config.ts
Expand Up @@ -51,6 +51,7 @@ import { getRawPageExtensions } from './utils'
import browserslist from 'next/dist/compiled/browserslist'
import loadJsConfig from './load-jsconfig'
import { shouldUseReactRoot } from '../server/config'
import { getMiddlewareSourceMapPlugins } from './webpack/plugins/middleware-source-maps-plugin'

const watchOptions = Object.freeze({
aggregateTimeout: 5,
Expand Down Expand Up @@ -1272,6 +1273,10 @@ export default async function getBaseWebpackConfig(
].filter(Boolean),
},
plugins: [
...getMiddlewareSourceMapPlugins({
isConfigured: !!config.experimental.middlewareSourceMaps,
isProductionBrowserSourceMapsOn: config.productionBrowserSourceMaps,
}),
Schniz marked this conversation as resolved.
Show resolved Hide resolved
hasReactRefresh && new ReactRefreshWebpackPlugin(webpack),
// Makes sure `Buffer` and `process` are polyfilled in client and flight bundles (same behavior as webpack 4)
targetWeb &&
Expand Down
@@ -0,0 +1,47 @@
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,
* so we can avoid having the custom plugins if the browser source maps
* are emitted.
*/
export const getMiddlewareSourceMapPlugins = (params: {
isConfigured: boolean
isProductionBrowserSourceMapsOn: boolean
}) => {
if (!params.isConfigured || params.isProductionBrowserSourceMapsOn) {
return []
}

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'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to override other conditionals if you have sourcemaps enabled in a different way 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably needs

if(!module.useSourceMap) to make sure you don't disable other plugins enabling sourcemaps for certain modules.

})
})
}
}
1 change: 1 addition & 0 deletions packages/next/server/config-shared.ts
Expand Up @@ -106,6 +106,7 @@ export interface ExperimentalConfig {
urlImports?: NonNullable<webpack5.Configuration['experiments']>['buildHttp']
outputFileTracingRoot?: string
outputStandalone?: boolean
middlewareSourceMaps?: boolean
}

/**
Expand Down