Skip to content

Commit

Permalink
refactor: uses the appropriate webpack hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
feugy committed Aug 23, 2022
1 parent 6adc2f8 commit 73be5ce
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 118 deletions.
2 changes: 0 additions & 2 deletions packages/next/build/webpack/loaders/get-module-build-info.ts
@@ -1,5 +1,4 @@
import { webpack } from 'next/dist/compiled/webpack/webpack'
import type { MiddlewareConfig } from '../../analysis/get-page-static-info'

/**
* A getter for module build info that casts to the type it should have.
Expand All @@ -16,7 +15,6 @@ export function getModuleBuildInfo(webpackModule: webpack.Module) {
usingIndirectEval?: boolean | Set<string>
route?: RouteMeta
importLocByPath?: Map<string, any>
edgeFunctionConfig?: Partial<MiddlewareConfig>
rootDir?: string
}
}
Expand Down
@@ -1,25 +1,20 @@
import { getModuleBuildInfo } from './get-module-build-info'
import { stringifyRequest } from '../stringify-request'
import { loadEdgeFunctionConfigFromFile } from './utils'

export type EdgeFunctionLoaderOptions = {
absolutePagePath: string
page: string
rootDir: string
}

export default async function middlewareLoader(this: any) {
export default function middlewareLoader(this: any) {
const { absolutePagePath, page, rootDir }: EdgeFunctionLoaderOptions =
this.getOptions()
const stringifiedPagePath = stringifyRequest(this, absolutePagePath)
const buildInfo = getModuleBuildInfo(this._module)
buildInfo.nextEdgeApiFunction = {
page: page || '/',
}
buildInfo.edgeFunctionConfig = await loadEdgeFunctionConfigFromFile(
absolutePagePath,
this.getResolve()
)
buildInfo.rootDir = rootDir

return `
Expand Down
@@ -1,7 +1,6 @@
import { getModuleBuildInfo } from './get-module-build-info'
import { stringifyRequest } from '../stringify-request'
import { MIDDLEWARE_LOCATION_REGEXP } from '../../../lib/constants'
import { loadEdgeFunctionConfigFromFile } from './utils'

export type MiddlewareLoaderOptions = {
absolutePagePath: string
Expand All @@ -10,7 +9,7 @@ export type MiddlewareLoaderOptions = {
rootDir: string
}

export default async function middlewareLoader(this: any) {
export default function middlewareLoader(this: any) {
const {
absolutePagePath,
page,
Expand All @@ -28,10 +27,6 @@ export default async function middlewareLoader(this: any) {
page:
page.replace(new RegExp(`/${MIDDLEWARE_LOCATION_REGEXP}$`), '') || '/',
}
buildInfo.edgeFunctionConfig = await loadEdgeFunctionConfigFromFile(
absolutePagePath,
this.getResolve()
)
buildInfo.rootDir = rootDir

return `
Expand Down

0 comments on commit 73be5ce

Please sign in to comment.