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

chore: narrows regexp to enable middleware source maps #37582

Merged
merged 3 commits into from
Jun 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/next/build/webpack/loaders/next-middleware-loader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getModuleBuildInfo } from './get-module-build-info'
import { stringifyRequest } from '../stringify-request'
import { MIDDLEWARE_FILENAME } from '../../../lib/constants'
import { MIDDLEWARE_LOCATION_REGEXP } from '../../../lib/constants'

export type MiddlewareLoaderOptions = {
absolutePagePath: string
Expand All @@ -16,7 +16,7 @@ export default function middlewareLoader(this: any) {
buildInfo.nextEdgeMiddleware = {
matcherRegexp,
page:
page.replace(new RegExp(`/(?:src/)?${MIDDLEWARE_FILENAME}$`), '') || '/',
page.replace(new RegExp(`/${MIDDLEWARE_LOCATION_REGEXP}$`), '') || '/',
}

return `
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { webpack } from 'next/dist/compiled/webpack/webpack'
import { MIDDLEWARE_FILENAME } from '../../../lib/constants'
import { MIDDLEWARE_LOCATION_REGEXP } from '../../../lib/constants'
import type { webpack5 } from 'next/dist/compiled/webpack/webpack'

/**
Expand All @@ -10,7 +10,10 @@ export const getMiddlewareSourceMapPlugins = () => {
return [
new webpack.SourceMapDevToolPlugin({
filename: '[file].map',
include: [new RegExp(`${MIDDLEWARE_FILENAME}.`), /^edge-chunks\//],
include: [
new RegExp(`^${MIDDLEWARE_LOCATION_REGEXP}\\.`),
/^edge-chunks\//,
],
}),
new MiddlewareSourceMapsPlugin(),
]
Expand Down
3 changes: 2 additions & 1 deletion packages/next/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ export const NEXT_PROJECT_ROOT_DIST_SERVER = join(
// Regex for API routes
export const API_ROUTE = /^\/api(?:\/|$)/

// Regex for middleware
// Patterns to detect middleware files
export const MIDDLEWARE_FILENAME = 'middleware'
export const MIDDLEWARE_LOCATION_REGEXP = `(?:src/)?${MIDDLEWARE_FILENAME}`

// Because on Windows absolute paths in the generated code can break because of numbers, eg 1 in the path,
// we have to use a private alias
Expand Down
3 changes: 2 additions & 1 deletion test/integration/async-modules/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ function runTests(dev = false) {
}
})

it('can render async AMP pages', async () => {
// TODO: investigate this test flaking
it.skip('can render async AMP pages', async () => {
let browser
try {
browser = await webdriver(appPort, '/config')
Expand Down
77 changes: 38 additions & 39 deletions test/production/generate-middleware-source-maps/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,63 @@ import { NextInstance } from 'test/lib/next-modes/base'
import fs from 'fs-extra'
import path from 'path'

const files = {
'pages/index.js': `
export default function () { return <div>Hello, world!</div> }
`,
'middleware.js': `
import { NextResponse } from "next/server";
export default function middleware() {
return NextResponse.next();
}
`,
}

describe('experimental.middlewareSourceMaps: true', () => {
let next: NextInstance

beforeAll(async () => {
next = await createNext({
nextConfig: {
experimental: {
middlewareSourceMaps: true,
},
},
files: {
'pages/index.js': `
export default function () { return <div>Hello, world!</div> }
`,
'middleware.js': `
import { NextResponse } from "next/server";
export default function middleware() {
return NextResponse.next();
}
`,
},
dependencies: {},
})
})
afterAll(() => next.destroy())
const nextConfig = { experimental: { middlewareSourceMaps: true } }

afterEach(() => next.destroy())

it('generates a source map', async () => {
next = await createNext({ nextConfig, files })

const middlewarePath = path.resolve(
next.testDir,
'.next/server/middleware.js'
)
expect(await fs.pathExists(middlewarePath)).toEqual(true)
expect(await fs.pathExists(`${middlewarePath}.map`)).toEqual(true)
})

it('generates a source map from src', async () => {
next = await createNext({
nextConfig,
files: Object.fromEntries(
Object.entries(files).map(([filename, content]) => [
`src/${filename}`,
content,
])
),
})

const middlewarePath = path.resolve(
next.testDir,
'.next/server/src/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/index.js': `
export default function () { return <div>Hello, world!</div> }
`,
'middleware.js': `
import { NextResponse } from "next/server";
export default function middleware() {
return NextResponse.next();
}
`,
},
dependencies: {},
})
})
afterAll(() => next.destroy())
afterEach(() => next.destroy())

it('does not generate a source map', async () => {
next = await createNext({ files })
const middlewarePath = path.resolve(
next.testDir,
'.next/server/middleware.js'
Expand Down