forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This exports the named exports from the user's page route file and exports a new `routeModule` stub. This stub will be the new rendering mechanism used for pages within `pages/` and will serve to allow us to deprecate large chunks of the server code to instead live within the pages entrypoints. TLDR; - Route Modules now accept a `definition` so that they have their full route definition - Exports are now hoisted from the userland module by name to prevent name collisions - A new `PagesRouteModule` stub was added that will in the future contain all the pages rendering logic --------- Co-authored-by: Tim Neutkens <tim@timneutkens.nl>
- Loading branch information
Showing
18 changed files
with
355 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 0 additions & 64 deletions
64
packages/next/src/build/webpack/loaders/next-route-loader.ts
This file was deleted.
Oops, something went wrong.
28 changes: 28 additions & 0 deletions
28
packages/next/src/build/webpack/loaders/next-route-loader/helpers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Hoists a name from a module or promised module. | ||
* | ||
* @param module the module to hoist the name from | ||
* @param name the name to hoist | ||
* @returns the value on the module (or promised module) | ||
*/ | ||
export function hoist(module: any, name: string) { | ||
// If the name is available in the module, return it. | ||
if (name in module) { | ||
return module[name] | ||
} | ||
|
||
// If a property called `then` exists, assume it's a promise and | ||
// return a promise that resolves to the name. | ||
if ('then' in module && typeof module.then === 'function') { | ||
return module.then((mod: any) => hoist(mod, name)) | ||
} | ||
|
||
// If we're trying to hoise the default export, and the module is a function, | ||
// return the module itself. | ||
if (typeof module === 'function' && name === 'default') { | ||
return module | ||
} | ||
|
||
// Otherwise, return undefined. | ||
return undefined | ||
} |
104 changes: 104 additions & 0 deletions
104
packages/next/src/build/webpack/loaders/next-route-loader/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import type { webpack } from 'next/dist/compiled/webpack/webpack' | ||
|
||
import { stringify } from 'querystring' | ||
import { getModuleBuildInfo } from '../get-module-build-info' | ||
import { PagesRouteModuleOptions } from '../../../../server/future/route-modules/pages/module' | ||
import { RouteKind } from '../../../../server/future/route-kind' | ||
import { normalizePagePath } from '../../../../shared/lib/page-path/normalize-page-path' | ||
|
||
/** | ||
* The options for the route loader. | ||
*/ | ||
type RouteLoaderOptions = { | ||
/** | ||
* 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 | ||
} | ||
|
||
/** | ||
* Returns the loader entry for a given page. | ||
* | ||
* @param query the options to create the loader entry | ||
* @returns the encoded loader entry | ||
*/ | ||
export function getRouteLoaderEntry(query: RouteLoaderOptions): string { | ||
return `next-route-loader?${stringify(query)}!` | ||
} | ||
|
||
/** | ||
* Handles the `next-route-loader` options. | ||
* @returns the loader definition function | ||
*/ | ||
const loader: webpack.LoaderDefinitionFunction<RouteLoaderOptions> = | ||
function () { | ||
const { page, preferredRegion, absolutePagePath } = this.getOptions() | ||
|
||
// Ensure we only run this loader for as a module. | ||
if (!this._module) { | ||
throw new Error('Invariant: expected this to reference a module') | ||
} | ||
|
||
// Attach build info to the module. | ||
const buildInfo = getModuleBuildInfo(this._module) | ||
buildInfo.route = { | ||
page, | ||
absolutePagePath, | ||
preferredRegion, | ||
} | ||
|
||
const options: Omit<PagesRouteModuleOptions, 'userland'> = { | ||
definition: { | ||
kind: RouteKind.PAGES, | ||
page: normalizePagePath(page), | ||
pathname: page, | ||
// The following aren't used in production. | ||
bundlePath: '', | ||
filename: '', | ||
}, | ||
} | ||
|
||
return ` | ||
// Next.js Route Loader | ||
import RouteModule from "next/dist/server/future/route-modules/pages/module" | ||
import { hoist } from "next/dist/build/webpack/loaders/next-route-loader/helpers" | ||
// Import the userland code. | ||
import * as userland from ${JSON.stringify(absolutePagePath)} | ||
// Re-export the component (should be the default export). | ||
export default hoist(userland, "default") | ||
// Re-export methods. | ||
export const getStaticProps = hoist(userland, "getStaticProps") | ||
export const getStaticPaths = hoist(userland, "getStaticPaths") | ||
export const getServerSideProps = hoist(userland, "getServerSideProps") | ||
export const config = hoist(userland, "config") | ||
export const reportWebVitals = hoist(userland, "reportWebVitals") | ||
// Re-export legacy methods. | ||
export const unstable_getStaticProps = hoist(userland, "unstable_getStaticProps") | ||
export const unstable_getStaticPaths = hoist(userland, "unstable_getStaticPaths") | ||
export const unstable_getStaticParams = hoist(userland, "unstable_getStaticParams") | ||
export const unstable_getServerProps = hoist(userland, "unstable_getServerProps") | ||
export const unstable_getServerSideProps = hoist(userland, "unstable_getServerSideProps") | ||
// Create and export the route module that will be consumed. | ||
const options = ${JSON.stringify(options)} | ||
const routeModule = new RouteModule({ ...options, userland }) | ||
export { routeModule } | ||
` | ||
} | ||
|
||
export default loader |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.