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
Changes from 6 commits
11d1a3b
76ecdcb
ea1dccd
dafe3ea
ca37797
54b8c05
a1762ec
a3910a8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
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' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably needs
|
||
}) | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
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) | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This still applies in development where eval sourcemaps are already enabled. Is that intended?