Skip to content

Commit

Permalink
Refactor MiddlewarePlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
javivelasco committed Apr 29, 2022
1 parent 3bc0783 commit c595a2c
Show file tree
Hide file tree
Showing 10 changed files with 380 additions and 330 deletions.
7 changes: 0 additions & 7 deletions packages/next/build/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { __ApiPreviewProps } from '../server/api-utils'
import { isTargetLikeServerless } from '../server/utils'
import { normalizePagePath } from '../server/page-path-utils'
import { normalizePathSep } from '../server/page-path-utils'
import { ssrEntries } from './webpack/plugins/middleware-plugin'
import { warn } from './output/log'
import { parse } from '../build/swc'
import { isFlightPage, withoutRSCExtensions } from './utils'
Expand Down Expand Up @@ -233,7 +232,6 @@ export function getEdgeServerEntry(opts: {
isDev: boolean
page: string
pages: { [page: string]: string }
ssrEntries: Map<string, { requireFlightManifest: boolean }>
}): ObjectValue<webpack5.EntryObject> {
if (opts.page.match(MIDDLEWARE_ROUTE)) {
const loaderParams: MiddlewareLoaderOptions = {
Expand All @@ -258,10 +256,6 @@ export function getEdgeServerEntry(opts: {
stringifiedConfig: JSON.stringify(opts.config),
}

ssrEntries.set(opts.bundlePath, {
requireFlightManifest: isFlightPage(opts.config, opts.absolutePagePath),
})

return `next-middleware-ssr-loader?${stringify(loaderParams)}!`
}

Expand Down Expand Up @@ -375,7 +369,6 @@ export async function createEntrypoints(params: CreateEntrypointsParams) {
bundlePath: clientBundlePath,
isDev: false,
page,
ssrEntries,
})
},
})
Expand Down
29 changes: 29 additions & 0 deletions packages/next/build/webpack/loaders/get-module-build-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { webpack5 } from 'next/dist/compiled/webpack/webpack'

/**
* A getter for module build info that casts to the type it should have.
* We also expose here types to make easier to use it.
*/
export function getModuleBuildInfo(webpackModule: webpack5.Module) {
return webpackModule.buildInfo as {
nextEdgeMiddleware?: EdgeMiddlewareMeta
nextEdgeSSR?: EdgeSSRMeta
nextUsedEnvVars?: Set<string>
nextWasmMiddlewareBinding?: WasmBinding
usingIndirectEval?: boolean | Set<string>
}
}

export interface EdgeMiddlewareMeta {
page: string
}

export interface EdgeSSRMeta {
isServerComponent: boolean
page: string
}

export interface WasmBinding {
filePath: string
name: string
}
5 changes: 5 additions & 0 deletions packages/next/build/webpack/loaders/next-middleware-loader.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getModuleBuildInfo } from './get-module-build-info'
import { stringifyRequest } from '../stringify-request'

export type MiddlewareLoaderOptions = {
Expand All @@ -8,6 +9,10 @@ export type MiddlewareLoaderOptions = {
export default function middlewareLoader(this: any) {
const { absolutePagePath, page }: MiddlewareLoaderOptions = this.getOptions()
const stringifiedPagePath = stringifyRequest(this, absolutePagePath)
const buildInfo = getModuleBuildInfo(this._module)
buildInfo.nextEdgeMiddleware = {
page: page.replace(/\/_middleware$/, '') || '/',
}

return `
import { adapter } from 'next/dist/server/web/adapter'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getModuleBuildInfo } from '../get-module-build-info'
import { stringifyRequest } from '../../stringify-request'

export type MiddlewareSSRLoaderQuery = {
Expand Down Expand Up @@ -27,7 +28,13 @@ export default async function middlewareSSRLoader(this: any) {
absoluteErrorPath,
isServerComponent,
stringifiedConfig,
}: MiddlewareSSRLoaderQuery = this.getOptions()
} = this.getOptions()

const buildInfo = getModuleBuildInfo(this._module)
buildInfo.nextEdgeSSR = {
isServerComponent: isServerComponent === 'true',
page: page,
}

const stringifiedPagePath = stringifyRequest(this, absolutePagePath)
const stringifiedAppPath = stringifyRequest(this, absoluteAppPath)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { getModuleBuildInfo } from './get-module-build-info'
import crypto from 'crypto'

export type WasmBinding = {
filePath: string
name: string
}

export default function MiddlewareWasmLoader(this: any, source: Buffer) {
const name = `wasm_${sha1(source)}`
const filePath = `edge-chunks/${name}.wasm`
const binding: WasmBinding = { filePath: `server/${filePath}`, name }
this._module.buildInfo.nextWasmMiddlewareBinding = binding
const buildInfo = getModuleBuildInfo(this._module)
buildInfo.nextWasmMiddlewareBinding = { filePath: `server/${filePath}`, name }
this.emitFile(`/${filePath}`, source, null)
return `module.exports = ${name};`
}
Expand Down

0 comments on commit c595a2c

Please sign in to comment.