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
kodiakhq
merged 8 commits into
vercel:canary
from
Schniz:create-sourcemaps-for-middlewares
Feb 24, 2022
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
11d1a3b
produce source maps for middlewares
Schniz 76ecdcb
Try to enable source map work only for middleware layer
Schniz ea1dccd
runtimeModules are never in middleware layer, but maybe in middleware…
Schniz dafe3ea
Make it under an experimental flag
Schniz ca37797
Add a test for the experimental flag
Schniz 54b8c05
Lift conditionals to the webpack config
Schniz a1762ec
Merge branch 'canary' into create-sourcemaps-for-middlewares
kodiakhq[bot] a3910a8
Don't run on dev
Schniz File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
packages/next/build/webpack/plugins/middleware-source-maps-plugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
}) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
test/production/generate-middleware-source-maps/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 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 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.