Skip to content

Commit

Permalink
chore: extract edge-app-route loader template
Browse files Browse the repository at this point in the history
  • Loading branch information
ForsakenHarmony committed Oct 4, 2023
1 parent f8308c9 commit ad8601d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/next/src/build/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ export function getEdgeServerEntry(opts: {
layer: WEBPACK_LAYERS.reactServerComponents,
}
}

if (isMiddlewareFile(opts.page)) {
const loaderParams: MiddlewareLoaderOptions = {
absolutePagePath: opts.absolutePagePath,
Expand Down
10 changes: 8 additions & 2 deletions packages/next/src/build/load-entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@ const TEMPLATES_ESM_FOLDER = path.normalize(
* handle replacement values that are related to imports.
*
* @param entrypoint the entrypoint to load
* @param replacements the replacements to perform
* @param replacements string replacements to perform
* @param injections code injections to perform
* @returns the loaded file with the replacements
*/

export async function loadEntrypoint(
entrypoint: 'pages' | 'pages-api' | 'app-page' | 'app-route',
entrypoint:
| 'pages'
| 'pages-api'
| 'app-page'
| 'app-route'
| 'edge-app-route',
replacements: Record<`VAR_${string}`, string>,
injections?: Record<string, string>
): Promise<string> {
Expand Down
9 changes: 9 additions & 0 deletions packages/next/src/build/templates/edge-app-route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { EdgeRouteModuleWrapper } from '../../server/web/edge-route-module-wrapper'

// Import the userland code.
// @ts-expect-error - replaced by webpack/turbopack loader
import * as module from 'VAR_USERLAND'

export const ComponentMod = module

export default EdgeRouteModuleWrapper.wrap(module.routeModule)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { NextConfig } from '../../../../server/config-shared'
import { webpack } from 'next/dist/compiled/webpack/webpack'
import { WEBPACK_RESOURCE_QUERIES } from '../../../../lib/constants'
import { MiddlewareConfig } from '../../../analysis/get-page-static-info'
import { loadEntrypoint } from '../../../load-entrypoint'

export type EdgeAppRouteLoaderQuery = {
absolutePagePath: string
Expand All @@ -15,7 +16,7 @@ export type EdgeAppRouteLoaderQuery = {
}

const EdgeAppRouteLoader: webpack.LoaderDefinitionFunction<EdgeAppRouteLoaderQuery> =
function (this) {
async function (this) {
const {
page,
absolutePagePath,
Expand Down Expand Up @@ -52,13 +53,9 @@ const EdgeAppRouteLoader: webpack.LoaderDefinitionFunction<EdgeAppRouteLoaderQue
stringifiedPagePath.length - 1
)}?${WEBPACK_RESOURCE_QUERIES.edgeSSREntry}`

return `
import { EdgeRouteModuleWrapper } from 'next/dist/esm/server/web/edge-route-module-wrapper'
import * as module from ${JSON.stringify(modulePath)}
export const ComponentMod = module
export default EdgeRouteModuleWrapper.wrap(module.routeModule)`
return await loadEntrypoint('edge-app-route', {
VAR_USERLAND: modulePath,
})
}

export default EdgeAppRouteLoader

0 comments on commit ad8601d

Please sign in to comment.