Skip to content

Commit

Permalink
chore: narrows regexp to enable middleware source maps
Browse files Browse the repository at this point in the history
  • Loading branch information
feugy committed Jun 9, 2022
1 parent 0c986cd commit 3c385e6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 44 deletions.
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
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

0 comments on commit 3c385e6

Please sign in to comment.