rsnext/packages/next/build/webpack/plugins/middleware-source-maps-plugin.ts
Damien Simonin Feugas ae44779bcb
chore: narrows regexp to enable middleware source maps (#37582)
* chore: narrows regexp to enable middleware source maps

* update test

Co-authored-by: JJ Kasper <jj@jjsweb.site>
2022-06-10 20:22:03 -05:00

37 lines
1.1 KiB
TypeScript

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'
})
})
}
}