File size: 5,980 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
import type { webpack } from 'next/dist/compiled/webpack/webpack'
import type { MiddlewareConfig } from '../../../analysis/get-page-static-info'
import { stringify } from 'querystring'
import {
type ModuleBuildInfo,
getModuleBuildInfo,
} from '../get-module-build-info'
import { RouteKind } from '../../../../server/route-kind'
import { normalizePagePath } from '../../../../shared/lib/page-path/normalize-page-path'
import { decodeFromBase64, encodeToBase64 } from '../utils'
import { isInstrumentationHookFile } from '../../../utils'
import { loadEntrypoint } from '../../../load-entrypoint'
import type { MappedPages } from '../../../build-context'
type RouteLoaderOptionsPagesAPIInput = {
kind: RouteKind.PAGES_API
page: string
preferredRegion: string | string[] | undefined
absolutePagePath: string
middlewareConfig: MiddlewareConfig
}
type RouteLoaderOptionsPagesInput = {
kind: RouteKind.PAGES
page: string
pages: MappedPages
preferredRegion: string | string[] | undefined
absolutePagePath: string
middlewareConfig: MiddlewareConfig
}
type RouteLoaderOptionsInput =
| RouteLoaderOptionsPagesInput
| RouteLoaderOptionsPagesAPIInput
type RouteLoaderPagesAPIOptions = {
kind: RouteKind.PAGES_API
/**
* The page name for this particular route.
*/
page: string
/**
* The preferred region for this route.
*/
preferredRegion: string | string[] | undefined
/**
* The absolute path to the userland page file.
*/
absolutePagePath: string
/**
* The middleware config for this route.
*/
middlewareConfigBase64: string
}
type RouteLoaderPagesOptions = {
kind: RouteKind.PAGES
/**
* The page name for this particular route.
*/
page: string
/**
* The preferred region for this route.
*/
preferredRegion: string | string[] | undefined
/**
* The absolute path to the userland page file.
*/
absolutePagePath: string
/**
* The absolute paths to the app path file.
*/
absoluteAppPath: string
/**
* The absolute paths to the document path file.
*/
absoluteDocumentPath: string
/**
* The middleware config for this route.
*/
middlewareConfigBase64: string
}
/**
* The options for the route loader.
*/
type RouteLoaderOptions = RouteLoaderPagesOptions | RouteLoaderPagesAPIOptions
/**
* Returns the loader entry for a given page.
*
* @param options the options to create the loader entry
* @returns the encoded loader entry
*/
export function getRouteLoaderEntry(options: RouteLoaderOptionsInput): string {
switch (options.kind) {
case RouteKind.PAGES: {
const query: RouteLoaderPagesOptions = {
kind: options.kind,
page: options.page,
preferredRegion: options.preferredRegion,
absolutePagePath: options.absolutePagePath,
// These are the path references to the internal components that may be
// overridden by userland components.
absoluteAppPath: options.pages['/_app'],
absoluteDocumentPath: options.pages['/_document'],
middlewareConfigBase64: encodeToBase64(options.middlewareConfig),
}
return `next-route-loader?${stringify(query)}!`
}
case RouteKind.PAGES_API: {
const query: RouteLoaderPagesAPIOptions = {
kind: options.kind,
page: options.page,
preferredRegion: options.preferredRegion,
absolutePagePath: options.absolutePagePath,
middlewareConfigBase64: encodeToBase64(options.middlewareConfig),
}
return `next-route-loader?${stringify(query)}!`
}
default: {
throw new Error('Invariant: Unexpected route kind')
}
}
}
const loadPages = async (
{
page,
absolutePagePath,
absoluteDocumentPath,
absoluteAppPath,
preferredRegion,
middlewareConfigBase64,
}: RouteLoaderPagesOptions,
buildInfo: ModuleBuildInfo
) => {
const middlewareConfig: MiddlewareConfig = decodeFromBase64(
middlewareConfigBase64
)
// Attach build info to the module.
buildInfo.route = {
page,
absolutePagePath,
preferredRegion,
middlewareConfig,
}
let file = await loadEntrypoint('pages', {
VAR_USERLAND: absolutePagePath,
VAR_MODULE_DOCUMENT: absoluteDocumentPath,
VAR_MODULE_APP: absoluteAppPath,
VAR_DEFINITION_PAGE: normalizePagePath(page),
VAR_DEFINITION_PATHNAME: page,
})
if (isInstrumentationHookFile(page)) {
// When we're building the instrumentation page (only when the
// instrumentation file conflicts with a page also labeled
// /instrumentation) hoist the `register` method.
file += '\nexport const register = hoist(userland, "register")'
}
return file
}
const loadPagesAPI = async (
{
page,
absolutePagePath,
preferredRegion,
middlewareConfigBase64,
}: RouteLoaderPagesAPIOptions,
buildInfo: ModuleBuildInfo
) => {
const middlewareConfig: MiddlewareConfig = decodeFromBase64(
middlewareConfigBase64
)
// Attach build info to the module.
buildInfo.route = {
page,
absolutePagePath,
preferredRegion,
middlewareConfig,
}
return await loadEntrypoint('pages-api', {
VAR_USERLAND: absolutePagePath,
VAR_DEFINITION_PAGE: normalizePagePath(page),
VAR_DEFINITION_PATHNAME: page,
})
}
/**
* Handles the `next-route-loader` options.
* @returns the loader definition function
*/
const loader: webpack.LoaderDefinitionFunction<RouteLoaderOptions> =
async function () {
if (!this._module) {
throw new Error('Invariant: expected this to reference a module')
}
const buildInfo = getModuleBuildInfo(this._module)
const opts = this.getOptions()
switch (opts.kind) {
case RouteKind.PAGES: {
return await loadPages(opts, buildInfo)
}
case RouteKind.PAGES_API: {
return await loadPagesAPI(opts, buildInfo)
}
default: {
throw new Error('Invariant: Unexpected route kind')
}
}
}
export default loader
|