create project

This commit is contained in:
ismailsosic
2022-12-27 12:05:56 +01:00
parent 2a33a2d3de
commit cd2143287c
16035 changed files with 2489703 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
import type { webpack } from 'next/dist/compiled/webpack/webpack';
import type ws from 'ws';
declare class EventStream {
clients: Set<ws>;
constructor();
everyClient(fn: (client: ws) => void): void;
close(): void;
handler(client: ws): void;
publish(payload: any): void;
}
export declare class WebpackHotMiddleware {
eventStream: EventStream;
clientLatestStats: {
ts: number;
stats: webpack.Stats;
} | null;
middlewareLatestStats: {
ts: number;
stats: webpack.Stats;
} | null;
serverLatestStats: {
ts: number;
stats: webpack.Stats;
} | null;
closed: boolean;
constructor(compilers: webpack.Compiler[]);
onClientInvalid: () => void;
onClientDone: (statsResult: webpack.Stats) => void;
onServerInvalid: () => void;
onServerDone: (statsResult: webpack.Stats) => void;
onEdgeServerInvalid: () => void;
onEdgeServerDone: (statsResult: webpack.Stats) => void;
/**
* To sync we use the most recent stats but also we append middleware
* errors. This is because it is possible that middleware fails to compile
* and we still want to show the client overlay with the error while
* the error page should be rendered just fine.
*/
onHMR: (client: ws) => void;
publishStats: (action: string, statsResult: webpack.Stats) => void;
publish: (payload: any) => void;
close: () => void;
}
export {};

View File

@@ -0,0 +1,178 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _utils = require("../../build/utils");
var _nonNullable = require("../../lib/non-nullable");
function isMiddlewareStats(stats) {
for (const key of stats.compilation.entrypoints.keys()){
if ((0, _utils).isMiddlewareFilename(key)) {
return true;
}
}
return false;
}
function statsToJson(stats) {
if (!stats) return {};
return stats.toJson({
all: false,
errors: true,
hash: true,
warnings: true
});
}
class EventStream {
constructor(){
this.clients = new Set();
}
everyClient(fn) {
for (const client of this.clients){
fn(client);
}
}
close() {
this.everyClient((client)=>{
client.close();
});
this.clients.clear();
}
handler(client) {
this.clients.add(client);
client.addEventListener("close", ()=>{
this.clients.delete(client);
});
}
publish(payload) {
this.everyClient((client)=>{
client.send(JSON.stringify(payload));
});
}
}
class WebpackHotMiddleware {
constructor(compilers){
this.eventStream = new EventStream();
this.clientLatestStats = null;
this.middlewareLatestStats = null;
this.serverLatestStats = null;
this.closed = false;
compilers[0].hooks.invalid.tap("webpack-hot-middleware", this.onClientInvalid);
compilers[0].hooks.done.tap("webpack-hot-middleware", this.onClientDone);
compilers[1].hooks.invalid.tap("webpack-hot-middleware", this.onServerInvalid);
compilers[1].hooks.done.tap("webpack-hot-middleware", this.onServerDone);
compilers[2].hooks.done.tap("webpack-hot-middleware", this.onEdgeServerDone);
compilers[2].hooks.invalid.tap("webpack-hot-middleware", this.onEdgeServerInvalid);
}
onClientInvalid = ()=>{
var ref;
if (this.closed || ((ref = this.serverLatestStats) == null ? void 0 : ref.stats.hasErrors())) return;
this.eventStream.publish({
action: "building"
});
};
onClientDone = (statsResult)=>{
var ref;
this.clientLatestStats = {
ts: Date.now(),
stats: statsResult
};
if (this.closed || ((ref = this.serverLatestStats) == null ? void 0 : ref.stats.hasErrors())) return;
this.publishStats("built", statsResult);
};
onServerInvalid = ()=>{
var ref, ref1;
if (!((ref = this.serverLatestStats) == null ? void 0 : ref.stats.hasErrors())) return;
this.serverLatestStats = null;
if ((ref1 = this.clientLatestStats) == null ? void 0 : ref1.stats) {
this.publishStats("built", this.clientLatestStats.stats);
}
};
onServerDone = (statsResult)=>{
if (this.closed) return;
if (statsResult.hasErrors()) {
this.serverLatestStats = {
ts: Date.now(),
stats: statsResult
};
this.publishStats("built", statsResult);
}
};
onEdgeServerInvalid = ()=>{
var ref, ref2;
if (!((ref = this.middlewareLatestStats) == null ? void 0 : ref.stats.hasErrors())) return;
this.middlewareLatestStats = null;
if ((ref2 = this.clientLatestStats) == null ? void 0 : ref2.stats) {
this.publishStats("built", this.clientLatestStats.stats);
}
};
onEdgeServerDone = (statsResult)=>{
if (!isMiddlewareStats(statsResult)) {
this.onServerDone(statsResult);
return;
}
if (statsResult.hasErrors()) {
this.middlewareLatestStats = {
ts: Date.now(),
stats: statsResult
};
this.publishStats("built", statsResult);
}
};
/**
* To sync we use the most recent stats but also we append middleware
* errors. This is because it is possible that middleware fails to compile
* and we still want to show the client overlay with the error while
* the error page should be rendered just fine.
*/ onHMR = (client)=>{
if (this.closed) return;
this.eventStream.handler(client);
const [latestStats] = [
this.clientLatestStats,
this.serverLatestStats
].filter(_nonNullable.nonNullable).sort((statsA, statsB)=>statsB.ts - statsA.ts);
if (latestStats == null ? void 0 : latestStats.stats) {
var ref;
const stats = statsToJson(latestStats.stats);
const middlewareStats = statsToJson((ref = this.middlewareLatestStats) == null ? void 0 : ref.stats);
this.eventStream.publish({
action: "sync",
hash: stats.hash,
errors: [
...stats.errors || [],
...middlewareStats.errors || []
],
warnings: [
...stats.warnings || [],
...middlewareStats.warnings || [],
]
});
}
};
publishStats = (action, statsResult)=>{
const stats = statsResult.toJson({
all: false,
hash: true,
warnings: true,
errors: true
});
this.eventStream.publish({
action: action,
hash: stats.hash,
warnings: stats.warnings || [],
errors: stats.errors || []
});
};
publish = (payload)=>{
if (this.closed) return;
this.eventStream.publish(payload);
};
close = ()=>{
if (this.closed) return;
// Can't remove compiler plugins, so we just set a flag and noop if closed
// https://github.com/webpack/tapable/issues/32#issuecomment-350644466
this.closed = true;
this.eventStream.close();
};
}
exports.WebpackHotMiddleware = WebpackHotMiddleware;
//# sourceMappingURL=hot-middleware.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,67 @@
/// <reference types="node" />
/// <reference types="node" />
/// <reference types="node" />
import { webpack } from 'next/dist/compiled/webpack/webpack';
import type { NextConfigComplete } from '../config-shared';
import type { CustomRoutes } from '../../lib/load-custom-routes';
import { IncomingMessage, ServerResponse } from 'http';
import { UrlObject } from 'url';
import getBaseWebpackConfig from '../../build/webpack-config';
import { __ApiPreviewProps } from '../api-utils';
import { UnwrapPromise } from '../../lib/coalesced-function';
export declare function renderScriptError(res: ServerResponse, error: Error, { verbose }?: {
verbose?: boolean | undefined;
}): Promise<void>;
export default class HotReloader {
private dir;
private buildId;
private interceptors;
private pagesDir?;
private distDir;
private webpackHotMiddleware?;
private config;
hasServerComponents: boolean;
clientStats: webpack.Stats | null;
serverStats: webpack.Stats | null;
edgeServerStats: webpack.Stats | null;
private clientError;
private serverError;
private serverPrevDocumentHash;
private prevChunkNames?;
private onDemandEntries?;
private previewProps;
private watcher;
private rewrites;
private fallbackWatcher;
private hotReloaderSpan;
private pagesMapping;
private appDir?;
multiCompiler?: webpack.MultiCompiler;
activeConfigs?: Array<UnwrapPromise<ReturnType<typeof getBaseWebpackConfig>>>;
constructor(dir: string, { config, pagesDir, distDir, buildId, previewProps, rewrites, appDir, }: {
config: NextConfigComplete;
pagesDir?: string;
distDir: string;
buildId: string;
previewProps: __ApiPreviewProps;
rewrites: CustomRoutes['rewrites'];
appDir?: string;
});
run(req: IncomingMessage, res: ServerResponse, parsedUrl: UrlObject): Promise<{
finished?: true;
}>;
onHMR(req: IncomingMessage, _res: ServerResponse, head: Buffer): void;
private clean;
private getWebpackConfig;
buildFallbackError(): Promise<void>;
start(): Promise<void>;
invalidate(): void;
stop(): Promise<void>;
getCompilationErrors(page: string): Promise<any[]>;
send(action?: string | any, ...args: any[]): void;
ensurePage({ page, clientOnly, appPaths, }: {
page: string;
clientOnly: boolean;
appPaths?: string[] | null;
}): Promise<void>;
}

View File

@@ -0,0 +1,867 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.renderScriptError = renderScriptError;
exports.default = void 0;
var _webpack = require("next/dist/compiled/webpack/webpack");
var _middleware = require("next/dist/compiled/@next/react-dev-overlay/dist/middleware");
var _hotMiddleware = require("./hot-middleware");
var _path = require("path");
var _entries = require("../../build/entries");
var _output = require("../../build/output");
var Log = _interopRequireWildcard(require("../../build/output/log"));
var _webpackConfig = _interopRequireDefault(require("../../build/webpack-config"));
var _constants = require("../../lib/constants");
var _recursiveDelete = require("../../lib/recursive-delete");
var _constants1 = require("../../shared/lib/constants");
var _pathMatch = require("../../shared/lib/router/utils/path-match");
var _findPageFile = require("../lib/find-page-file");
var _onDemandEntryHandler = require("./on-demand-entry-handler");
var _denormalizePagePath = require("../../shared/lib/page-path/denormalize-page-path");
var _normalizePathSep = require("../../shared/lib/page-path/normalize-path-sep");
var _getRouteFromEntrypoint = _interopRequireDefault(require("../get-route-from-entrypoint"));
var _fileExists = require("../../lib/file-exists");
var _utils = require("../../build/utils");
var _utils1 = require("../../shared/lib/utils");
var _trace = require("../../trace");
var _isError = require("../../lib/is-error");
var _ws = _interopRequireDefault(require("next/dist/compiled/ws"));
var _fs = require("fs");
var _getPageStaticInfo = require("../../build/analysis/get-page-static-info");
class HotReloader {
clientError = null;
serverError = null;
pagesMapping = {};
constructor(dir, { config , pagesDir , distDir , buildId , previewProps , rewrites , appDir }){
this.buildId = buildId;
this.dir = dir;
this.interceptors = [];
this.pagesDir = pagesDir;
this.appDir = appDir;
this.distDir = distDir;
this.clientStats = null;
this.serverStats = null;
this.edgeServerStats = null;
this.serverPrevDocumentHash = null;
this.config = config;
this.hasServerComponents = !!this.appDir;
this.previewProps = previewProps;
this.rewrites = rewrites;
this.hotReloaderSpan = (0, _trace).trace("hot-reloader", undefined, {
version: "13.1.1"
});
// Ensure the hotReloaderSpan is flushed immediately as it's the parentSpan for all processing
// of the current `next dev` invocation.
this.hotReloaderSpan.stop();
}
async run(req, res, parsedUrl) {
// Usually CORS support is not needed for the hot-reloader (this is dev only feature)
// With when the app runs for multi-zones support behind a proxy,
// the current page is trying to access this URL via assetPrefix.
// That's when the CORS support is needed.
const { preflight } = addCorsSupport(req, res);
if (preflight) {
return {};
}
// When a request comes in that is a page bundle, e.g. /_next/static/<buildid>/pages/index.js
// we have to compile the page using on-demand-entries, this middleware will handle doing that
// by adding the page to on-demand-entries, waiting till it's done
// and then the bundle will be served like usual by the actual route in server/index.js
const handlePageBundleRequest = async (pageBundleRes, parsedPageBundleUrl)=>{
const { pathname } = parsedPageBundleUrl;
const params = matchNextPageBundleRequest(pathname);
if (!params) {
return {};
}
let decodedPagePath;
try {
decodedPagePath = `/${params.path.map((param)=>decodeURIComponent(param)).join("/")}`;
} catch (_) {
throw new _utils1.DecodeError("failed to decode param");
}
const page = (0, _denormalizePagePath).denormalizePagePath(decodedPagePath);
if (page === "/_error" || _constants1.BLOCKED_PAGES.indexOf(page) === -1) {
try {
await this.ensurePage({
page,
clientOnly: true
});
} catch (error) {
await renderScriptError(pageBundleRes, (0, _isError).getProperError(error));
return {
finished: true
};
}
const errors = await this.getCompilationErrors(page);
if (errors.length > 0) {
await renderScriptError(pageBundleRes, errors[0], {
verbose: false
});
return {
finished: true
};
}
}
return {};
};
const { finished } = await handlePageBundleRequest(res, parsedUrl);
for (const fn of this.interceptors){
await new Promise((resolve, reject)=>{
fn(req, res, (err)=>{
if (err) return reject(err);
resolve();
});
});
}
return {
finished
};
}
onHMR(req, _res, head) {
wsServer.handleUpgrade(req, req.socket, head, (client)=>{
var ref2, ref1;
(ref2 = this.webpackHotMiddleware) == null ? void 0 : ref2.onHMR(client);
(ref1 = this.onDemandEntries) == null ? void 0 : ref1.onHMR(client);
client.addEventListener("message", ({ data })=>{
data = typeof data !== "string" ? data.toString() : data;
try {
const payload = JSON.parse(data);
let traceChild;
switch(payload.event){
case "client-hmr-latency":
{
traceChild = {
name: payload.event,
startTime: BigInt(payload.startTime * 1000 * 1000),
endTime: BigInt(payload.endTime * 1000 * 1000)
};
break;
}
case "client-reload-page":
case "client-success":
{
traceChild = {
name: payload.event
};
break;
}
case "client-error":
{
traceChild = {
name: payload.event,
attrs: {
errorCount: payload.errorCount
}
};
break;
}
case "client-warning":
{
traceChild = {
name: payload.event,
attrs: {
warningCount: payload.warningCount
}
};
break;
}
case "client-removed-page":
case "client-added-page":
{
traceChild = {
name: payload.event,
attrs: {
page: payload.page || ""
}
};
break;
}
case "client-full-reload":
{
const { event , stackTrace , hadRuntimeError } = payload;
traceChild = {
name: event,
attrs: {
stackTrace: stackTrace ?? ""
}
};
if (hadRuntimeError) {
Log.warn(`Fast Refresh had to perform a full reload due to a runtime error.`);
break;
}
let fileMessage = "";
if (stackTrace) {
var ref;
const file = (ref = /Aborted because (.+) is not accepted/.exec(stackTrace)) == null ? void 0 : ref[1];
if (file) {
fileMessage = ` when ${file} changed`;
}
}
Log.warn(`Fast Refresh had to perform a full reload${fileMessage}. Read more: https://nextjs.org/docs/messages/fast-refresh-reload`);
break;
}
default:
{
break;
}
}
if (traceChild) {
this.hotReloaderSpan.manualTraceChild(traceChild.name, traceChild.startTime || process.hrtime.bigint(), traceChild.endTime || process.hrtime.bigint(), {
...traceChild.attrs,
clientId: payload.id
});
}
} catch (_) {
// invalid WebSocket message
}
});
});
}
async clean(span) {
return span.traceChild("clean").traceAsyncFn(()=>(0, _recursiveDelete).recursiveDelete((0, _path).join(this.dir, this.config.distDir), /^cache/));
}
async getWebpackConfig(span) {
const webpackConfigSpan = span.traceChild("get-webpack-config");
const pageExtensions = this.config.pageExtensions;
return webpackConfigSpan.traceAsyncFn(async ()=>{
const pagePaths = !this.pagesDir ? [] : await webpackConfigSpan.traceChild("get-page-paths").traceAsyncFn(()=>Promise.all([
(0, _findPageFile).findPageFile(this.pagesDir, "/_app", pageExtensions, false),
(0, _findPageFile).findPageFile(this.pagesDir, "/_document", pageExtensions, false),
]));
this.pagesMapping = webpackConfigSpan.traceChild("create-pages-mapping").traceFn(()=>(0, _entries).createPagesMapping({
isDev: true,
pageExtensions: this.config.pageExtensions,
pagesType: "pages",
pagePaths: pagePaths.filter((i)=>typeof i === "string"),
pagesDir: this.pagesDir
}));
const entrypoints = await webpackConfigSpan.traceChild("create-entrypoints").traceAsyncFn(()=>(0, _entries).createEntrypoints({
appDir: this.appDir,
buildId: this.buildId,
config: this.config,
envFiles: [],
isDev: true,
pages: this.pagesMapping,
pagesDir: this.pagesDir,
previewMode: this.previewProps,
rootDir: this.dir,
pageExtensions: this.config.pageExtensions
}));
const commonWebpackOptions = {
dev: true,
buildId: this.buildId,
config: this.config,
pagesDir: this.pagesDir,
rewrites: this.rewrites,
runWebpackSpan: this.hotReloaderSpan,
appDir: this.appDir
};
return webpackConfigSpan.traceChild("generate-webpack-config").traceAsyncFn(()=>Promise.all([
// order is important here
(0, _webpackConfig).default(this.dir, {
...commonWebpackOptions,
compilerType: _constants1.COMPILER_NAMES.client,
entrypoints: entrypoints.client
}),
(0, _webpackConfig).default(this.dir, {
...commonWebpackOptions,
compilerType: _constants1.COMPILER_NAMES.server,
entrypoints: entrypoints.server
}),
(0, _webpackConfig).default(this.dir, {
...commonWebpackOptions,
compilerType: _constants1.COMPILER_NAMES.edgeServer,
entrypoints: entrypoints.edgeServer
}),
]));
});
}
async buildFallbackError() {
if (this.fallbackWatcher) return;
const fallbackConfig = await (0, _webpackConfig).default(this.dir, {
runWebpackSpan: this.hotReloaderSpan,
dev: true,
compilerType: _constants1.COMPILER_NAMES.client,
config: this.config,
buildId: this.buildId,
pagesDir: this.pagesDir,
rewrites: {
beforeFiles: [],
afterFiles: [],
fallback: []
},
isDevFallback: true,
entrypoints: (await (0, _entries).createEntrypoints({
appDir: this.appDir,
buildId: this.buildId,
config: this.config,
envFiles: [],
isDev: true,
pages: {
"/_app": "next/dist/pages/_app",
"/_error": "next/dist/pages/_error"
},
pagesDir: this.pagesDir,
previewMode: this.previewProps,
rootDir: this.dir,
pageExtensions: this.config.pageExtensions
})).client
});
const fallbackCompiler = (0, _webpack).webpack(fallbackConfig);
this.fallbackWatcher = await new Promise((resolve)=>{
let bootedFallbackCompiler = false;
fallbackCompiler.watch(// @ts-ignore webpack supports an array of watchOptions when using a multiCompiler
fallbackConfig.watchOptions, // Errors are handled separately
(_err)=>{
if (!bootedFallbackCompiler) {
bootedFallbackCompiler = true;
resolve(true);
}
});
});
}
async start() {
const startSpan = this.hotReloaderSpan.traceChild("start");
startSpan.stop() // Stop immediately to create an artificial parent span
;
await this.clean(startSpan);
// Ensure distDir exists before writing package.json
await _fs.promises.mkdir(this.distDir, {
recursive: true
});
const distPackageJsonPath = (0, _path).join(this.distDir, "package.json");
// Ensure commonjs handling is used for files in the distDir (generally .next)
// Files outside of the distDir can be "type": "module"
await _fs.promises.writeFile(distPackageJsonPath, '{"type": "commonjs"}');
this.activeConfigs = await this.getWebpackConfig(startSpan);
for (const config1 of this.activeConfigs){
const defaultEntry = config1.entry;
config1.entry = async (...args)=>{
// @ts-ignore entry is always a function
const entrypoints = await defaultEntry(...args);
const isClientCompilation = config1.name === _constants1.COMPILER_NAMES.client;
const isNodeServerCompilation = config1.name === _constants1.COMPILER_NAMES.server;
const isEdgeServerCompilation = config1.name === _constants1.COMPILER_NAMES.edgeServer;
await Promise.all(Object.keys(_onDemandEntryHandler.entries).map(async (entryKey)=>{
const entryData = _onDemandEntryHandler.entries[entryKey];
const { bundlePath , dispose } = entryData;
const result = /^(client|server|edge-server)(.*)/g.exec(entryKey);
const [, key, page] = result// this match should always happen
;
if (key === _constants1.COMPILER_NAMES.client && !isClientCompilation) return;
if (key === _constants1.COMPILER_NAMES.server && !isNodeServerCompilation) return;
if (key === _constants1.COMPILER_NAMES.edgeServer && !isEdgeServerCompilation) return;
const isEntry = entryData.type === _onDemandEntryHandler.EntryTypes.ENTRY;
const isChildEntry = entryData.type === _onDemandEntryHandler.EntryTypes.CHILD_ENTRY;
// Check if the page was removed or disposed and remove it
if (isEntry) {
const pageExists = !dispose && await (0, _fileExists).fileExists(entryData.absolutePagePath);
if (!pageExists) {
delete _onDemandEntryHandler.entries[entryKey];
return;
}
}
const hasAppDir = !!this.appDir;
const isAppPath = hasAppDir && bundlePath.startsWith("app/");
const staticInfo = isEntry ? await (0, _getPageStaticInfo).getPageStaticInfo({
pageFilePath: entryData.absolutePagePath,
nextConfig: this.config,
isDev: true,
pageType: isAppPath ? "app" : "pages"
}) : {};
const isServerComponent = isAppPath && staticInfo.rsc !== _constants1.RSC_MODULE_TYPES.client;
await (0, _entries).runDependingOnPageType({
page,
pageRuntime: staticInfo.runtime,
onEdgeServer: ()=>{
// TODO-APP: verify if child entry should support.
if (!isEdgeServerCompilation || !isEntry) return;
const appDirLoader = isAppPath ? (0, _entries).getAppEntry({
name: bundlePath,
appPaths: entryData.appPaths,
pagePath: _path.posix.join(_constants.APP_DIR_ALIAS, (0, _path).relative(this.appDir, entryData.absolutePagePath).replace(/\\/g, "/")),
appDir: this.appDir,
pageExtensions: this.config.pageExtensions,
rootDir: this.dir,
isDev: true,
tsconfigPath: this.config.typescript.tsconfigPath
}).import : undefined;
_onDemandEntryHandler.entries[entryKey].status = _onDemandEntryHandler.BUILDING;
entrypoints[bundlePath] = (0, _entries).finalizeEntrypoint({
compilerType: _constants1.COMPILER_NAMES.edgeServer,
name: bundlePath,
value: (0, _entries).getEdgeServerEntry({
absolutePagePath: entryData.absolutePagePath,
rootDir: this.dir,
buildId: this.buildId,
bundlePath,
config: this.config,
isDev: true,
page,
pages: this.pagesMapping,
isServerComponent,
appDirLoader,
pagesType: isAppPath ? "app" : "pages"
}),
hasAppDir
});
},
onClient: ()=>{
if (!isClientCompilation) return;
if (isChildEntry) {
_onDemandEntryHandler.entries[entryKey].status = _onDemandEntryHandler.BUILDING;
entrypoints[bundlePath] = (0, _entries).finalizeEntrypoint({
name: bundlePath,
compilerType: _constants1.COMPILER_NAMES.client,
value: entryData.request,
hasAppDir
});
} else {
_onDemandEntryHandler.entries[entryKey].status = _onDemandEntryHandler.BUILDING;
entrypoints[bundlePath] = (0, _entries).finalizeEntrypoint({
name: bundlePath,
compilerType: _constants1.COMPILER_NAMES.client,
value: (0, _entries).getClientEntry({
absolutePagePath: entryData.absolutePagePath,
page
}),
hasAppDir
});
}
},
onServer: ()=>{
// TODO-APP: verify if child entry should support.
if (!isNodeServerCompilation || !isEntry) return;
_onDemandEntryHandler.entries[entryKey].status = _onDemandEntryHandler.BUILDING;
let relativeRequest = (0, _path).relative(config1.context, entryData.absolutePagePath);
if (!(0, _path).isAbsolute(relativeRequest) && !relativeRequest.startsWith("../")) {
relativeRequest = `./${relativeRequest}`;
}
entrypoints[bundlePath] = (0, _entries).finalizeEntrypoint({
compilerType: _constants1.COMPILER_NAMES.server,
name: bundlePath,
isServerComponent,
value: isAppPath ? (0, _entries).getAppEntry({
name: bundlePath,
appPaths: entryData.appPaths,
pagePath: _path.posix.join(_constants.APP_DIR_ALIAS, (0, _path).relative(this.appDir, entryData.absolutePagePath).replace(/\\/g, "/")),
appDir: this.appDir,
pageExtensions: this.config.pageExtensions,
rootDir: this.dir,
isDev: true,
tsconfigPath: this.config.typescript.tsconfigPath
}) : relativeRequest,
hasAppDir
});
}
});
}));
return entrypoints;
};
}
// Enable building of client compilation before server compilation in development
// @ts-ignore webpack 5
this.activeConfigs.parallelism = 1;
this.multiCompiler = (0, _webpack).webpack(this.activeConfigs);
(0, _output).watchCompilers(this.multiCompiler.compilers[0], this.multiCompiler.compilers[1], this.multiCompiler.compilers[2]);
// Watch for changes to client/server page files so we can tell when just
// the server file changes and trigger a reload for GS(S)P pages
const changedClientPages = new Set();
const changedServerPages = new Set();
const changedEdgeServerPages = new Set();
const changedServerComponentPages = new Set();
const changedCSSImportPages = new Set();
const prevClientPageHashes = new Map();
const prevServerPageHashes = new Map();
const prevEdgeServerPageHashes = new Map();
const prevCSSImportModuleHashes = new Map();
const trackPageChanges = (pageHashMap, changedItems, serverComponentChangedItems)=>{
return (stats)=>{
try {
stats.entrypoints.forEach((entry, key)=>{
if (key.startsWith("pages/") || key.startsWith("app/") || (0, _utils).isMiddlewareFilename(key)) {
// TODO this doesn't handle on demand loaded chunks
entry.chunks.forEach((chunk)=>{
if (chunk.id === key) {
const modsIterable = stats.chunkGraph.getChunkModulesIterable(chunk);
let hasCSSModuleChanges = false;
let chunksHash = new _webpack.StringXor();
let chunksHashServerLayer = new _webpack.StringXor();
modsIterable.forEach((mod)=>{
if (mod.resource && mod.resource.replace(/\\/g, "/").includes(key)) {
var ref, ref3;
// use original source to calculate hash since mod.hash
// includes the source map in development which changes
// every time for both server and client so we calculate
// the hash without the source map for the page module
const hash = require("crypto").createHash("sha256").update(mod.originalSource().buffer()).digest().toString("hex");
if (mod.layer === _constants.WEBPACK_LAYERS.server && (mod == null ? void 0 : (ref = mod.buildInfo) == null ? void 0 : (ref3 = ref.rsc) == null ? void 0 : ref3.type) !== "client") {
chunksHashServerLayer.add(hash);
}
chunksHash.add(hash);
} else {
var ref4, ref5, ref6;
// for non-pages we can use the module hash directly
const hash = stats.chunkGraph.getModuleHash(mod, chunk.runtime);
if (mod.layer === _constants.WEBPACK_LAYERS.server && (mod == null ? void 0 : (ref4 = mod.buildInfo) == null ? void 0 : (ref5 = ref4.rsc) == null ? void 0 : ref5.type) !== "client") {
chunksHashServerLayer.add(hash);
}
chunksHash.add(hash);
// Both CSS import changes from server and client
// components are tracked.
if (key.startsWith("app/") && ((ref6 = mod.resource) == null ? void 0 : ref6.endsWith(".css"))) {
const prevHash = prevCSSImportModuleHashes.get(mod.resource);
if (prevHash && prevHash !== hash) {
hasCSSModuleChanges = true;
}
prevCSSImportModuleHashes.set(mod.resource, hash);
}
}
});
const prevHash1 = pageHashMap.get(key);
const curHash = chunksHash.toString();
if (prevHash1 && prevHash1 !== curHash) {
changedItems.add(key);
}
pageHashMap.set(key, curHash);
if (serverComponentChangedItems) {
const serverKey = _constants.WEBPACK_LAYERS.server + ":" + key;
const prevServerHash = pageHashMap.get(serverKey);
const curServerHash = chunksHashServerLayer.toString();
if (prevServerHash && prevServerHash !== curServerHash) {
serverComponentChangedItems.add(key);
}
pageHashMap.set(serverKey, curServerHash);
}
if (hasCSSModuleChanges) {
changedCSSImportPages.add(key);
}
}
});
}
});
} catch (err) {
console.error(err);
}
};
};
this.multiCompiler.compilers[0].hooks.emit.tap("NextjsHotReloaderForClient", trackPageChanges(prevClientPageHashes, changedClientPages));
this.multiCompiler.compilers[1].hooks.emit.tap("NextjsHotReloaderForServer", trackPageChanges(prevServerPageHashes, changedServerPages, changedServerComponentPages));
this.multiCompiler.compilers[2].hooks.emit.tap("NextjsHotReloaderForServer", trackPageChanges(prevEdgeServerPageHashes, changedEdgeServerPages, changedServerComponentPages));
// This plugin watches for changes to _document.js and notifies the client side that it should reload the page
this.multiCompiler.compilers[1].hooks.failed.tap("NextjsHotReloaderForServer", (err)=>{
this.serverError = err;
this.serverStats = null;
});
this.multiCompiler.compilers[2].hooks.done.tap("NextjsHotReloaderForServer", (stats)=>{
this.serverError = null;
this.edgeServerStats = stats;
});
this.multiCompiler.compilers[1].hooks.done.tap("NextjsHotReloaderForServer", (stats)=>{
this.serverError = null;
this.serverStats = stats;
if (!this.pagesDir) {
return;
}
const { compilation } = stats;
// We only watch `_document` for changes on the server compilation
// the rest of the files will be triggered by the client compilation
const documentChunk = compilation.namedChunks.get("pages/_document");
// If the document chunk can't be found we do nothing
if (!documentChunk) {
console.warn("_document.js chunk not found");
return;
}
// Initial value
if (this.serverPrevDocumentHash === null) {
this.serverPrevDocumentHash = documentChunk.hash || null;
return;
}
// If _document.js didn't change we don't trigger a reload
if (documentChunk.hash === this.serverPrevDocumentHash) {
return;
}
// Notify reload to reload the page, as _document.js was changed (different hash)
this.send("reloadPage");
this.serverPrevDocumentHash = documentChunk.hash || null;
});
this.multiCompiler.hooks.done.tap("NextjsHotReloaderForServer", ()=>{
const serverOnlyChanges = (0, _utils).difference(changedServerPages, changedClientPages);
const edgeServerOnlyChanges = (0, _utils).difference(changedEdgeServerPages, changedClientPages);
const pageChanges = serverOnlyChanges.concat(edgeServerOnlyChanges).filter((key)=>key.startsWith("pages/"));
const middlewareChanges = Array.from(changedEdgeServerPages).filter((name)=>(0, _utils).isMiddlewareFilename(name));
if (middlewareChanges.length > 0) {
this.send({
event: "middlewareChanges"
});
}
if (pageChanges.length > 0) {
this.send({
event: "serverOnlyChanges",
pages: serverOnlyChanges.map((pg)=>(0, _denormalizePagePath).denormalizePagePath(pg.slice("pages".length)))
});
}
if (changedServerComponentPages.size || changedCSSImportPages.size) {
this.send({
action: "serverComponentChanges"
});
}
changedClientPages.clear();
changedServerPages.clear();
changedEdgeServerPages.clear();
changedServerComponentPages.clear();
changedCSSImportPages.clear();
});
this.multiCompiler.compilers[0].hooks.failed.tap("NextjsHotReloaderForClient", (err)=>{
this.clientError = err;
this.clientStats = null;
});
this.multiCompiler.compilers[0].hooks.done.tap("NextjsHotReloaderForClient", (stats)=>{
this.clientError = null;
this.clientStats = stats;
const { compilation } = stats;
const chunkNames = new Set([
...compilation.namedChunks.keys()
].filter((name)=>!!(0, _getRouteFromEntrypoint).default(name)));
if (this.prevChunkNames) {
// detect chunks which have to be replaced with a new template
// e.g, pages/index.js <-> pages/_error.js
const addedPages = diff(chunkNames, this.prevChunkNames);
const removedPages = diff(this.prevChunkNames, chunkNames);
if (addedPages.size > 0) {
for (const addedPage of addedPages){
const page = (0, _getRouteFromEntrypoint).default(addedPage);
this.send("addedPage", page);
}
}
if (removedPages.size > 0) {
for (const removedPage of removedPages){
const page = (0, _getRouteFromEntrypoint).default(removedPage);
this.send("removedPage", page);
}
}
}
this.prevChunkNames = chunkNames;
});
this.webpackHotMiddleware = new _hotMiddleware.WebpackHotMiddleware(this.multiCompiler.compilers);
let booted = false;
this.watcher = await new Promise((resolve)=>{
var ref;
const watcher = (ref = this.multiCompiler) == null ? void 0 : ref.watch(// @ts-ignore webpack supports an array of watchOptions when using a multiCompiler
this.activeConfigs.map((config)=>config.watchOptions), // Errors are handled separately
(_err)=>{
if (!booted) {
booted = true;
resolve(watcher);
}
});
});
this.onDemandEntries = (0, _onDemandEntryHandler).onDemandEntryHandler({
multiCompiler: this.multiCompiler,
pagesDir: this.pagesDir,
appDir: this.appDir,
rootDir: this.dir,
nextConfig: this.config,
...this.config.onDemandEntries
});
this.interceptors = [
(0, _middleware).getOverlayMiddleware({
rootDirectory: this.dir,
stats: ()=>this.clientStats,
serverStats: ()=>this.serverStats,
edgeServerStats: ()=>this.edgeServerStats
}),
];
}
invalidate() {
var ref;
return (ref = (0, _onDemandEntryHandler).getInvalidator()) == null ? void 0 : ref.invalidate();
}
async stop() {
await new Promise((resolve, reject)=>{
this.watcher.close((err)=>err ? reject(err) : resolve(true));
});
if (this.fallbackWatcher) {
await new Promise((resolve, reject)=>{
this.fallbackWatcher.close((err)=>err ? reject(err) : resolve(true));
});
}
this.multiCompiler = undefined;
}
async getCompilationErrors(page) {
var ref9, ref7, ref8;
const getErrors = ({ compilation })=>{
var ref;
const failedPages = erroredPages(compilation);
const normalizedPage = (0, _normalizePathSep).normalizePathSep(page);
// If there is an error related to the requesting page we display it instead of the first error
return ((ref = failedPages[normalizedPage]) == null ? void 0 : ref.length) > 0 ? failedPages[normalizedPage] : compilation.errors;
};
if (this.clientError || this.serverError) {
return [
this.clientError || this.serverError
];
} else if ((ref9 = this.clientStats) == null ? void 0 : ref9.hasErrors()) {
return getErrors(this.clientStats);
} else if ((ref7 = this.serverStats) == null ? void 0 : ref7.hasErrors()) {
return getErrors(this.serverStats);
} else if ((ref8 = this.edgeServerStats) == null ? void 0 : ref8.hasErrors()) {
return getErrors(this.edgeServerStats);
} else {
return [];
}
}
send(action, ...args) {
this.webpackHotMiddleware.publish(action && typeof action === "object" ? action : {
action,
data: args
});
}
async ensurePage({ page , clientOnly , appPaths }) {
var ref;
// Make sure we don't re-build or dispose prebuilt pages
if (page !== "/_error" && _constants1.BLOCKED_PAGES.indexOf(page) !== -1) {
return;
}
const error = clientOnly ? this.clientError : this.serverError || this.clientError;
if (error) {
return Promise.reject(error);
}
return (ref = this.onDemandEntries) == null ? void 0 : ref.ensurePage({
page,
clientOnly,
appPaths
});
}
}
exports.default = HotReloader;
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
function _getRequireWildcardCache() {
if (typeof WeakMap !== "function") return null;
var cache = new WeakMap();
_getRequireWildcardCache = function() {
return cache;
};
return cache;
}
function _interopRequireWildcard(obj) {
if (obj && obj.__esModule) {
return obj;
}
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
return {
default: obj
};
}
var cache = _getRequireWildcardCache();
if (cache && cache.has(obj)) {
return cache.get(obj);
}
var newObj = {};
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
for(var key in obj){
if (Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
if (desc && (desc.get || desc.set)) {
Object.defineProperty(newObj, key, desc);
} else {
newObj[key] = obj[key];
}
}
}
newObj.default = obj;
if (cache) {
cache.set(obj, newObj);
}
return newObj;
}
function diff(a, b) {
return new Set([
...a
].filter((v)=>!b.has(v)));
}
const wsServer = new _ws.default.Server({
noServer: true
});
async function renderScriptError(res, error, { verbose =true } = {}) {
// Asks CDNs and others to not to cache the errored page
res.setHeader("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate");
if (error.code === "ENOENT") {
res.statusCode = 404;
res.end("404 - Not Found");
return;
}
if (verbose) {
console.error(error.stack);
}
res.statusCode = 500;
res.end("500 - Internal Error");
}
function addCorsSupport(req, res) {
// Only rewrite CORS handling when URL matches a hot-reloader middleware
if (!req.url.startsWith("/__next")) {
return {
preflight: false
};
}
if (!req.headers.origin) {
return {
preflight: false
};
}
res.setHeader("Access-Control-Allow-Origin", req.headers.origin);
res.setHeader("Access-Control-Allow-Methods", "OPTIONS, GET");
// Based on https://github.com/primus/access-control/blob/4cf1bc0e54b086c91e6aa44fb14966fa5ef7549c/index.js#L158
if (req.headers["access-control-request-headers"]) {
res.setHeader("Access-Control-Allow-Headers", req.headers["access-control-request-headers"]);
}
if (req.method === "OPTIONS") {
res.writeHead(200);
res.end();
return {
preflight: true
};
}
return {
preflight: false
};
}
const matchNextPageBundleRequest = (0, _pathMatch).getPathMatch("/_next/static/chunks/pages/:path*.js(\\.map|)");
// Recursively look up the issuer till it ends up at the root
function findEntryModule(compilation, issuerModule) {
const issuer = compilation.moduleGraph.getIssuer(issuerModule);
if (issuer) {
return findEntryModule(compilation, issuer);
}
return issuerModule;
}
function erroredPages(compilation) {
const failedPages = {};
for (const error of compilation.errors){
if (!error.module) {
continue;
}
const entryModule = findEntryModule(compilation, error.module);
const { name } = entryModule;
if (!name) {
continue;
}
// Only pages have to be reloaded
const enhancedName = (0, _getRouteFromEntrypoint).default(name);
if (!enhancedName) {
continue;
}
if (!failedPages[enhancedName]) {
failedPages[enhancedName] = [];
}
failedPages[enhancedName].push(error);
}
return failedPages;
}
//# sourceMappingURL=hot-reloader.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,137 @@
/// <reference types="node" />
/// <reference types="node" />
import type { __ApiPreviewProps } from '../api-utils';
import type { CustomRoutes } from '../../lib/load-custom-routes';
import type { FindComponentsResult } from '../next-server';
import type { LoadComponentsReturnType } from '../load-components';
import type { Options as ServerOptions } from '../next-server';
import type { Params } from '../../shared/lib/router/utils/route-matcher';
import type { ParsedUrl } from '../../shared/lib/router/utils/parse-url';
import type { ParsedUrlQuery } from 'querystring';
import type { UrlWithParsedQuery } from 'url';
import type { BaseNextRequest, BaseNextResponse } from '../base-http';
import type { MiddlewareRoutingItem, RoutingItem } from '../base-server';
import Server from '../next-server';
import { NodeNextResponse, NodeNextRequest } from '../base-http/node';
export interface Options extends ServerOptions {
/**
* Tells of Next.js is running from the `next dev` command
*/
isNextDevCommand?: boolean;
}
export default class DevServer extends Server {
private devReady;
private setDevReady?;
private webpackWatcher?;
private hotReloader?;
private isCustomServer;
protected sortedRoutes?: string[];
private addedUpgradeListener;
private pagesDir?;
private appDir?;
private actualMiddlewareFile?;
private middleware?;
private edgeFunctions?;
private verifyingTypeScript?;
private usingTypeScript?;
private originalFetch?;
protected staticPathsWorker?: {
[key: string]: any;
} & {
loadStaticPaths: typeof import('./static-paths-worker').loadStaticPaths;
};
private getStaticPathsWorker;
constructor(options: Options);
protected getBuildId(): string;
addExportPathMapRoutes(): Promise<void>;
startWatcher(): Promise<void>;
stopWatcher(): Promise<void>;
private verifyTypeScript;
prepare(): Promise<void>;
protected close(): Promise<void>;
protected hasPage(pathname: string): Promise<boolean>;
protected _beforeCatchAllRender(req: BaseNextRequest, res: BaseNextResponse, params: Params, parsedUrl: UrlWithParsedQuery): Promise<boolean>;
private setupWebSocketHandler;
runMiddleware(params: {
request: BaseNextRequest;
response: BaseNextResponse;
parsedUrl: ParsedUrl;
parsed: UrlWithParsedQuery;
middlewareList: MiddlewareRoutingItem[];
}): Promise<import("../web/types").FetchEventResult | {
finished: boolean;
}>;
runEdgeFunction(params: {
req: BaseNextRequest;
res: BaseNextResponse;
query: ParsedUrlQuery;
params: Params | undefined;
page: string;
appPaths: string[] | null;
isAppPath: boolean;
}): Promise<import("../web/types").FetchEventResult | null>;
run(req: NodeNextRequest, res: NodeNextResponse, parsedUrl: UrlWithParsedQuery): Promise<void>;
private logErrorWithOriginalStack;
protected getCustomRoutes(): CustomRoutes;
private _devCachedPreviewProps;
protected getPreviewProps(): __ApiPreviewProps;
protected getPagesManifest(): undefined;
protected getAppPathsManifest(): undefined;
protected getMiddleware(): MiddlewareRoutingItem | undefined;
protected getEdgeFunctions(): RoutingItem[];
protected getServerComponentManifest(): undefined;
protected getServerCSSManifest(): undefined;
protected getFontLoaderManifest(): undefined;
protected hasMiddleware(): Promise<boolean>;
protected ensureMiddleware(): Promise<void>;
protected ensureEdgeFunction({ page, appPaths, }: {
page: string;
appPaths: string[] | null;
}): Promise<void>;
generateRoutes(): {
headers: import("../router").Route[];
rewrites: {
beforeFiles: import("../router").Route[];
afterFiles: import("../router").Route[];
fallback: import("../router").Route[];
};
redirects: import("../router").Route[];
catchAllRoute: import("../router").Route;
catchAllMiddleware: import("../router").Route[];
pageChecker: import("../router").PageChecker;
useFileSystemPublicRoutes: boolean;
dynamicRoutes: import("../router").DynamicRoutes | undefined;
nextConfig: import("../config-shared").NextConfig;
fsRoutes: import("../router").Route[];
};
protected generatePublicRoutes(): never[];
protected getDynamicRoutes(): never[];
_filterAmpDevelopmentScript(html: string, event: {
line: number;
col: number;
code: string;
}): boolean;
protected getStaticPaths({ pathname, originalAppPath, }: {
pathname: string;
originalAppPath?: string;
}): Promise<{
staticPaths?: string[];
fallbackMode?: false | 'static' | 'blocking';
}>;
protected ensureApiPage(pathname: string): Promise<void>;
private persistPatchedGlobals;
private restorePatchedGlobals;
protected findPageComponents({ pathname, query, params, isAppPath, appPaths, }: {
pathname: string;
query: ParsedUrlQuery;
params: Params;
isAppPath: boolean;
appPaths?: string[] | null;
}): Promise<FindComponentsResult | null>;
protected getFallbackErrorComponents(): Promise<LoadComponentsReturnType | null>;
protected setImmutableAssetCacheControl(res: BaseNextResponse): void;
private servePublic;
hasPublicFile(path: string): Promise<boolean>;
getCompilationError(page: string): Promise<any>;
protected isServeableUrl(untrustedFileUrl: string): boolean;
}

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,90 @@
import type ws from 'ws';
import type { webpack } from 'next/dist/compiled/webpack/webpack';
import type { NextConfigComplete } from '../config-shared';
import { CompilerNameValues, COMPILER_INDEXES } from '../../shared/lib/constants';
declare const COMPILER_KEYS: CompilerNameValues[];
export declare const ADDED: unique symbol;
export declare const BUILDING: unique symbol;
export declare const BUILT: unique symbol;
interface EntryType {
/**
* Tells if a page is scheduled to be disposed.
*/
dispose?: boolean;
/**
* Timestamp with the last time the page was active.
*/
lastActiveTime?: number;
/**
* Page build status.
*/
status?: typeof ADDED | typeof BUILDING | typeof BUILT;
/**
* Path to the page file relative to the dist folder with no extension.
* For example: `pages/about/index`
*/
bundlePath: string;
/**
* Webpack request to create a dependency for.
*/
request: string;
}
export declare const enum EntryTypes {
ENTRY = 0,
CHILD_ENTRY = 1
}
interface Entry extends EntryType {
type: EntryTypes.ENTRY;
/**
* The absolute page to the page file. Used for detecting if the file was removed. For example:
* `/Users/Rick/project/pages/about/index.js`
*/
absolutePagePath: string;
/**
* All parallel pages that match the same entry, for example:
* ['/parallel/@bar/nested/@a/page', '/parallel/@bar/nested/@b/page', '/parallel/@foo/nested/@a/page', '/parallel/@foo/nested/@b/page']
*/
appPaths: string[] | null;
}
interface ChildEntry extends EntryType {
type: EntryTypes.CHILD_ENTRY;
/**
* Which parent entries use this childEntry.
*/
parentEntries: Set<string>;
}
export declare const entries: {
/**
* The key composed of the compiler name and the page. For example:
* `edge-server/about`
*/
[entryName: string]: Entry | ChildEntry;
};
export declare const getInvalidator: () => Invalidator;
declare class Invalidator {
private multiCompiler;
private building;
private rebuildAgain;
constructor(multiCompiler: webpack.MultiCompiler);
shouldRebuildAll(): boolean;
invalidate(compilerKeys?: typeof COMPILER_KEYS): void;
startBuilding(compilerKey: keyof typeof COMPILER_INDEXES): void;
doneBuilding(): void;
}
export declare function onDemandEntryHandler({ maxInactiveAge, multiCompiler, nextConfig, pagesBufferLength, pagesDir, rootDir, appDir, }: {
maxInactiveAge: number;
multiCompiler: webpack.MultiCompiler;
nextConfig: NextConfigComplete;
pagesBufferLength: number;
pagesDir?: string;
rootDir: string;
appDir?: string;
}): {
ensurePage({ page, clientOnly, appPaths, }: {
page: string;
clientOnly: boolean;
appPaths?: string[] | null | undefined;
}): Promise<void>;
onHMR(client: ws): void;
};
export {};

View File

@@ -0,0 +1,492 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.onDemandEntryHandler = onDemandEntryHandler;
exports.getInvalidator = exports.entries = exports.EntryTypes = exports.BUILT = exports.BUILDING = exports.ADDED = void 0;
var _debug = _interopRequireDefault(require("next/dist/compiled/debug"));
var _events = require("events");
var _findPageFile = require("../lib/find-page-file");
var _entries = require("../../build/entries");
var _path = require("path");
var _normalizePathSep = require("../../shared/lib/page-path/normalize-path-sep");
var _normalizePagePath = require("../../shared/lib/page-path/normalize-page-path");
var _ensureLeadingSlash = require("../../shared/lib/page-path/ensure-leading-slash");
var _removePagePathTail = require("../../shared/lib/page-path/remove-page-path-tail");
var _output = require("../../build/output");
var _getRouteFromEntrypoint = _interopRequireDefault(require("../get-route-from-entrypoint"));
var _getPageStaticInfo = require("../../build/analysis/get-page-static-info");
var _utils = require("../../build/utils");
var _utils1 = require("../../shared/lib/utils");
var _constants = require("../../shared/lib/constants");
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
const debug = (0, _debug).default("next:on-demand-entry-handler");
/**
* Returns object keys with type inferred from the object key
*/ const keys = Object.keys;
const COMPILER_KEYS = keys(_constants.COMPILER_INDEXES);
function treePathToEntrypoint(segmentPath, parentPath) {
const [parallelRouteKey, segment] = segmentPath;
// TODO-APP: modify this path to cover parallelRouteKey convention
const path = (parentPath ? parentPath + "/" : "") + (parallelRouteKey !== "children" && !segment.startsWith("@") ? parallelRouteKey + "/" : "") + (segment === "" ? "page" : segment);
// Last segment
if (segmentPath.length === 2) {
return path;
}
const childSegmentPath = segmentPath.slice(2);
return treePathToEntrypoint(childSegmentPath, path);
}
function convertDynamicParamTypeToSyntax(dynamicParamTypeShort, param) {
switch(dynamicParamTypeShort){
case "c":
return `[...${param}]`;
case "oc":
return `[[...${param}]]`;
case "d":
return `[${param}]`;
default:
throw new Error("Unknown dynamic param type");
}
}
function getEntrypointsFromTree(tree, isFirst, parentPath = []) {
const [segment, parallelRoutes] = tree;
const currentSegment = Array.isArray(segment) ? convertDynamicParamTypeToSyntax(segment[2], segment[0]) : segment;
const currentPath = [
...parentPath,
currentSegment
];
if (!isFirst && currentSegment === "") {
// TODO get rid of '' at the start of tree
return [
treePathToEntrypoint(currentPath.slice(1))
];
}
return Object.keys(parallelRoutes).reduce((paths, key)=>{
const childTree = parallelRoutes[key];
const childPages = getEntrypointsFromTree(childTree, false, [
...currentPath,
key,
]);
return [
...paths,
...childPages
];
}, []);
}
const ADDED = Symbol("added");
exports.ADDED = ADDED;
const BUILDING = Symbol("building");
exports.BUILDING = BUILDING;
const BUILT = Symbol("built");
exports.BUILT = BUILT;
var EntryTypes;
exports.EntryTypes = EntryTypes;
(function(EntryTypes) {
EntryTypes[EntryTypes["ENTRY"] = 0] = "ENTRY";
EntryTypes[EntryTypes["CHILD_ENTRY"] = 1] = "CHILD_ENTRY";
})(EntryTypes || (exports.EntryTypes = EntryTypes = {}));
const entries = {};
exports.entries = entries;
let invalidator;
const getInvalidator = ()=>invalidator;
exports.getInvalidator = getInvalidator;
const doneCallbacks = new _events.EventEmitter();
const lastClientAccessPages = [
""
];
const lastServerAccessPagesForAppDir = [
""
];
// Make sure only one invalidation happens at a time
// Otherwise, webpack hash gets changed and it'll force the client to reload.
class Invalidator {
building = new Set();
rebuildAgain = new Set();
constructor(multiCompiler){
this.multiCompiler = multiCompiler;
}
shouldRebuildAll() {
return this.rebuildAgain.size > 0;
}
invalidate(compilerKeys = COMPILER_KEYS) {
for (const key of compilerKeys){
var ref;
// If there's a current build is processing, we won't abort it by invalidating.
// (If aborted, it'll cause a client side hard reload)
// But let it to invalidate just after the completion.
// So, it can re-build the queued pages at once.
if (this.building.has(key)) {
this.rebuildAgain.add(key);
continue;
}
(ref = this.multiCompiler.compilers[_constants.COMPILER_INDEXES[key]].watching) == null ? void 0 : ref.invalidate();
this.building.add(key);
}
}
startBuilding(compilerKey) {
this.building.add(compilerKey);
}
doneBuilding() {
const rebuild = [];
for (const key of COMPILER_KEYS){
this.building.delete(key);
if (this.rebuildAgain.has(key)) {
rebuild.push(key);
this.rebuildAgain.delete(key);
}
}
this.invalidate(rebuild);
}
}
function disposeInactiveEntries(maxInactiveAge) {
Object.keys(entries).forEach((entryKey)=>{
const entryData = entries[entryKey];
const { lastActiveTime , status , dispose } = entryData;
// TODO-APP: implement disposing of CHILD_ENTRY
if (entryData.type === 1) {
return;
}
if (dispose) // Skip pages already scheduled for disposing
return;
// This means this entry is currently building or just added
// We don't need to dispose those entries.
if (status !== BUILT) return;
// We should not build the last accessed page even we didn't get any pings
// Sometimes, it's possible our XHR ping to wait before completing other requests.
// In that case, we should not dispose the current viewing page
if (lastClientAccessPages.includes(entryKey) || lastServerAccessPagesForAppDir.includes(entryKey)) return;
if (lastActiveTime && Date.now() - lastActiveTime > maxInactiveAge) {
entries[entryKey].dispose = true;
}
});
}
// Normalize both app paths and page paths
function tryToNormalizePagePath(page) {
try {
return (0, _normalizePagePath).normalizePagePath(page);
} catch (err) {
console.error(err);
throw new _utils1.PageNotFoundError(page);
}
}
/**
* Attempts to find a page file path from the given pages absolute directory,
* a page and allowed extensions. If the page can't be found it will throw an
* error. It defaults the `/_error` page to Next.js internal error page.
*
* @param rootDir Absolute path to the project root.
* @param pagesDir Absolute path to the pages folder with trailing `/pages`.
* @param normalizedPagePath The page normalized (it will be denormalized).
* @param pageExtensions Array of page extensions.
*/ async function findPagePathData(rootDir, page, extensions, pagesDir, appDir) {
const normalizedPagePath = tryToNormalizePagePath(page);
let pagePath = null;
if ((0, _utils).isMiddlewareFile(normalizedPagePath)) {
pagePath = await (0, _findPageFile).findPageFile(rootDir, normalizedPagePath, extensions, false);
if (!pagePath) {
throw new _utils1.PageNotFoundError(normalizedPagePath);
}
const pageUrl = (0, _ensureLeadingSlash).ensureLeadingSlash((0, _removePagePathTail).removePagePathTail((0, _normalizePathSep).normalizePathSep(pagePath), {
extensions
}));
return {
absolutePagePath: (0, _path).join(rootDir, pagePath),
bundlePath: normalizedPagePath.slice(1),
page: _path.posix.normalize(pageUrl)
};
}
// Check appDir first falling back to pagesDir
if (appDir) {
pagePath = await (0, _findPageFile).findPageFile(appDir, normalizedPagePath, extensions, true);
if (pagePath) {
const pageUrl = (0, _ensureLeadingSlash).ensureLeadingSlash((0, _removePagePathTail).removePagePathTail((0, _normalizePathSep).normalizePathSep(pagePath), {
keepIndex: true,
extensions
}));
return {
absolutePagePath: (0, _path).join(appDir, pagePath),
bundlePath: _path.posix.join("app", (0, _normalizePagePath).normalizePagePath(pageUrl)),
page: _path.posix.normalize(pageUrl)
};
}
}
if (!pagePath && pagesDir) {
pagePath = await (0, _findPageFile).findPageFile(pagesDir, normalizedPagePath, extensions, false);
}
if (pagePath !== null && pagesDir) {
const pageUrl = (0, _ensureLeadingSlash).ensureLeadingSlash((0, _removePagePathTail).removePagePathTail((0, _normalizePathSep).normalizePathSep(pagePath), {
extensions
}));
return {
absolutePagePath: (0, _path).join(pagesDir, pagePath),
bundlePath: _path.posix.join("pages", (0, _normalizePagePath).normalizePagePath(pageUrl)),
page: _path.posix.normalize(pageUrl)
};
}
if (page === "/_error") {
return {
absolutePagePath: require.resolve("next/dist/pages/_error"),
bundlePath: page,
page: (0, _normalizePathSep).normalizePathSep(page)
};
} else {
throw new _utils1.PageNotFoundError(normalizedPagePath);
}
}
function onDemandEntryHandler({ maxInactiveAge , multiCompiler , nextConfig , pagesBufferLength , pagesDir , rootDir , appDir }) {
invalidator = new Invalidator(multiCompiler);
const startBuilding = (compilation)=>{
const compilationName = compilation.name;
invalidator.startBuilding(compilationName);
};
for (const compiler of multiCompiler.compilers){
compiler.hooks.make.tap("NextJsOnDemandEntries", startBuilding);
}
function getPagePathsFromEntrypoints(type, entrypoints, root) {
const pagePaths = [];
for (const entrypoint of entrypoints.values()){
const page = (0, _getRouteFromEntrypoint).default(entrypoint.name, root);
if (page) {
pagePaths.push(`${type}${page}`);
} else if (root && entrypoint.name === "root" || (0, _utils).isMiddlewareFilename(entrypoint.name)) {
pagePaths.push(`${type}/${entrypoint.name}`);
}
}
return pagePaths;
}
multiCompiler.hooks.done.tap("NextJsOnDemandEntries", (multiStats)=>{
if (invalidator.shouldRebuildAll()) {
return invalidator.doneBuilding();
}
const [clientStats, serverStats, edgeServerStats] = multiStats.stats;
const root = !!appDir;
const pagePaths = [
...getPagePathsFromEntrypoints(_constants.COMPILER_NAMES.client, clientStats.compilation.entrypoints, root),
...getPagePathsFromEntrypoints(_constants.COMPILER_NAMES.server, serverStats.compilation.entrypoints, root),
...edgeServerStats ? getPagePathsFromEntrypoints(_constants.COMPILER_NAMES.edgeServer, edgeServerStats.compilation.entrypoints, root) : [],
];
for (const page of pagePaths){
const entry = entries[page];
if (!entry) {
continue;
}
if (entry.status !== BUILDING) {
continue;
}
entry.status = BUILT;
doneCallbacks.emit(page);
}
invalidator.doneBuilding();
});
const pingIntervalTime = Math.max(1000, Math.min(5000, maxInactiveAge));
setInterval(function() {
disposeInactiveEntries(maxInactiveAge);
}, pingIntervalTime + 1000).unref();
function handleAppDirPing(tree) {
const pages = getEntrypointsFromTree(tree, true);
let toSend = {
invalid: true
};
for (const page of pages){
for (const compilerType of [
_constants.COMPILER_NAMES.client,
_constants.COMPILER_NAMES.server,
_constants.COMPILER_NAMES.edgeServer,
]){
const pageKey = `${compilerType}/${page}`;
const entryInfo = entries[pageKey];
// If there's no entry, it may have been invalidated and needs to be re-built.
if (!entryInfo) {
continue;
}
// We don't need to maintain active state of anything other than BUILT entries
if (entryInfo.status !== BUILT) continue;
// If there's an entryInfo
if (!lastServerAccessPagesForAppDir.includes(pageKey)) {
lastServerAccessPagesForAppDir.unshift(pageKey);
// Maintain the buffer max length
// TODO: verify that the current pageKey is not at the end of the array as multiple entrypoints can exist
if (lastServerAccessPagesForAppDir.length > pagesBufferLength) {
lastServerAccessPagesForAppDir.pop();
}
}
entryInfo.lastActiveTime = Date.now();
entryInfo.dispose = false;
toSend = {
success: true
};
}
}
return toSend;
}
function handlePing(pg) {
const page = (0, _normalizePathSep).normalizePathSep(pg);
let toSend = {
invalid: true
};
for (const compilerType of [
_constants.COMPILER_NAMES.client,
_constants.COMPILER_NAMES.server,
_constants.COMPILER_NAMES.edgeServer,
]){
const pageKey = `${compilerType}${page}`;
const entryInfo = entries[pageKey];
// If there's no entry, it may have been invalidated and needs to be re-built.
if (!entryInfo) {
// if (page !== lastEntry) client pings, but there's no entry for page
if (compilerType === _constants.COMPILER_NAMES.client) {
return {
invalid: true
};
}
continue;
}
// 404 is an on demand entry but when a new page is added we have to refresh the page
toSend = page === "/_error" ? {
invalid: true
} : {
success: true
};
// We don't need to maintain active state of anything other than BUILT entries
if (entryInfo.status !== BUILT) continue;
// If there's an entryInfo
if (!lastClientAccessPages.includes(pageKey)) {
lastClientAccessPages.unshift(pageKey);
// Maintain the buffer max length
if (lastClientAccessPages.length > pagesBufferLength) {
lastClientAccessPages.pop();
}
}
entryInfo.lastActiveTime = Date.now();
entryInfo.dispose = false;
}
return toSend;
}
return {
async ensurePage ({ page , clientOnly , appPaths =null }) {
const stalledTime = 60;
const stalledEnsureTimeout = setTimeout(()=>{
debug(`Ensuring ${page} has taken longer than ${stalledTime}s, if this continues to stall this may be a bug`);
}, stalledTime * 1000);
try {
const pagePathData = await findPagePathData(rootDir, page, nextConfig.pageExtensions, pagesDir, appDir);
const isInsideAppDir = !!appDir && pagePathData.absolutePagePath.startsWith(appDir);
const addEntry = (compilerType)=>{
const entryKey = `${compilerType}${pagePathData.page}`;
if (entries[entryKey]) {
entries[entryKey].dispose = false;
entries[entryKey].lastActiveTime = Date.now();
if (entries[entryKey].status === BUILT) {
return {
entryKey,
newEntry: false,
shouldInvalidate: false
};
}
return {
entryKey,
newEntry: false,
shouldInvalidate: true
};
}
entries[entryKey] = {
type: 0,
appPaths,
absolutePagePath: pagePathData.absolutePagePath,
request: pagePathData.absolutePagePath,
bundlePath: pagePathData.bundlePath,
dispose: false,
lastActiveTime: Date.now(),
status: ADDED
};
return {
entryKey: entryKey,
newEntry: true,
shouldInvalidate: true
};
};
const staticInfo = await (0, _getPageStaticInfo).getPageStaticInfo({
pageFilePath: pagePathData.absolutePagePath,
nextConfig,
isDev: true,
pageType: isInsideAppDir ? "app" : "pages"
});
const added = new Map();
const isServerComponent = isInsideAppDir && staticInfo.rsc !== _constants.RSC_MODULE_TYPES.client;
await (0, _entries).runDependingOnPageType({
page: pagePathData.page,
pageRuntime: staticInfo.runtime,
onClient: ()=>{
// Skip adding the client entry for app / Server Components.
if (isServerComponent || isInsideAppDir) {
return;
}
added.set(_constants.COMPILER_NAMES.client, addEntry(_constants.COMPILER_NAMES.client));
},
onServer: ()=>{
added.set(_constants.COMPILER_NAMES.server, addEntry(_constants.COMPILER_NAMES.server));
const edgeServerEntry = `${_constants.COMPILER_NAMES.edgeServer}${pagePathData.page}`;
if (entries[edgeServerEntry]) {
// Runtime switched from edge to server
delete entries[edgeServerEntry];
}
},
onEdgeServer: ()=>{
added.set(_constants.COMPILER_NAMES.edgeServer, addEntry(_constants.COMPILER_NAMES.edgeServer));
const serverEntry = `${_constants.COMPILER_NAMES.server}${pagePathData.page}`;
if (entries[serverEntry]) {
// Runtime switched from server to edge
delete entries[serverEntry];
}
}
});
const addedValues = [
...added.values()
];
const entriesThatShouldBeInvalidated = addedValues.filter((entry)=>entry.shouldInvalidate);
const hasNewEntry = addedValues.some((entry)=>entry.newEntry);
if (hasNewEntry) {
(0, _output).reportTrigger(!clientOnly && hasNewEntry ? `${pagePathData.page} (client and server)` : pagePathData.page);
}
if (entriesThatShouldBeInvalidated.length > 0) {
const invalidatePromises = entriesThatShouldBeInvalidated.map(({ entryKey })=>{
return new Promise((resolve, reject)=>{
doneCallbacks.once(entryKey, (err)=>{
if (err) {
return reject(err);
}
resolve();
});
});
});
invalidator.invalidate([
...added.keys()
]);
await Promise.all(invalidatePromises);
}
} finally{
clearTimeout(stalledEnsureTimeout);
}
},
onHMR (client) {
client.addEventListener("message", ({ data })=>{
try {
const parsedData = JSON.parse(typeof data !== "string" ? data.toString() : data);
if (parsedData.event === "ping") {
const result = parsedData.appDirRoute ? handleAppDirPing(parsedData.tree) : handlePing(parsedData.page);
client.send(JSON.stringify({
...result,
[parsedData.appDirRoute ? "action" : "event"]: "pong"
}));
}
} catch (_) {}
});
}
};
}
//# sourceMappingURL=on-demand-entry-handler.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,19 @@
import type { NextConfigComplete } from '../config-shared';
import '../node-polyfill-fetch';
declare type RuntimeConfig = any;
export declare function loadStaticPaths({ distDir, pathname, config, httpAgentOptions, enableUndici, locales, defaultLocale, isAppPath, originalAppPath, }: {
distDir: string;
pathname: string;
config: RuntimeConfig;
httpAgentOptions: NextConfigComplete['httpAgentOptions'];
enableUndici: NextConfigComplete['enableUndici'];
locales?: string[];
defaultLocale?: string;
isAppPath?: boolean;
originalAppPath?: string;
}): Promise<{
paths?: string[];
encodedPaths?: string[];
fallback?: boolean | 'blocking';
}>;
export {};

View File

@@ -0,0 +1,62 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.loadStaticPaths = loadStaticPaths;
require("../node-polyfill-fetch");
var _utils = require("../../build/utils");
var _loadComponents = require("../load-components");
var _config = require("../config");
var _requireHook = require("../../build/webpack/require-hook");
(0, _requireHook).loadRequireHook();
if (process.env.NEXT_PREBUNDLED_REACT) {
(0, _requireHook).overrideBuiltInReactPackages();
}
let workerWasUsed = false;
// expose AsyncLocalStorage on globalThis for react usage
const { AsyncLocalStorage } = require("async_hooks");
globalThis.AsyncLocalStorage = AsyncLocalStorage;
async function loadStaticPaths({ distDir , pathname , config , httpAgentOptions , enableUndici , locales , defaultLocale , isAppPath , originalAppPath }) {
// we only want to use each worker once to prevent any invalid
// caches
if (workerWasUsed) {
process.exit(1);
}
// update work memory runtime-config
require("../../shared/lib/runtime-config").setConfig(config);
(0, _config).setHttpClientAndAgentOptions({
httpAgentOptions,
experimental: {
enableUndici
}
});
const components = await (0, _loadComponents).loadComponents({
distDir,
pathname: originalAppPath || pathname,
hasServerComponents: false,
isAppPath: !!isAppPath
});
if (!components.getStaticPaths && !isAppPath) {
// we shouldn't get to this point since the worker should
// only be called for SSG pages with getStaticPaths
throw new Error(`Invariant: failed to load page with getStaticPaths for ${pathname}`);
}
workerWasUsed = true;
if (isAppPath) {
const generateParams = await (0, _utils).collectGenerateParams(components.ComponentMod.tree);
return (0, _utils).buildAppStaticPaths({
page: pathname,
generateParams,
configFileName: config.configFileName
});
}
return (0, _utils).buildStaticPaths({
page: pathname,
getStaticPaths: components.getStaticPaths,
configFileName: config.configFileName,
locales,
defaultLocale
});
}
//# sourceMappingURL=static-paths-worker.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../server/dev/static-paths-worker.ts"],"names":["loadStaticPaths","loadRequireHook","process","env","NEXT_PREBUNDLED_REACT","overrideBuiltInReactPackages","workerWasUsed","AsyncLocalStorage","require","globalThis","distDir","pathname","config","httpAgentOptions","enableUndici","locales","defaultLocale","isAppPath","originalAppPath","exit","setConfig","setHttpClientAndAgentOptions","experimental","components","loadComponents","hasServerComponents","getStaticPaths","Error","generateParams","collectGenerateParams","ComponentMod","tree","buildAppStaticPaths","page","configFileName","buildStaticPaths"],"mappings":"AAAA;;;;QA+BsBA,eAAe,GAAfA,eAAe;QA7B9B,wBAAwB;AAKxB,IAAA,MAAmB,WAAnB,mBAAmB,CAAA;AACK,IAAA,eAAoB,WAApB,oBAAoB,CAAA;AACN,IAAA,OAAW,WAAX,WAAW,CAAA;AAIjD,IAAA,YAAkC,WAAlC,kCAAkC,CAAA;AAIzCC,CAAAA,GAAAA,YAAe,AAAE,CAAA,gBAAF,EAAE;AACjB,IAAIC,OAAO,CAACC,GAAG,CAACC,qBAAqB,EAAE;IACrCC,CAAAA,GAAAA,YAA4B,AAAE,CAAA,6BAAF,EAAE;CAC/B;AAED,IAAIC,aAAa,GAAG,KAAK;AAEzB,yDAAyD;AACzD,MAAM,EAAEC,iBAAiB,CAAA,EAAE,GAAGC,OAAO,CAAC,aAAa,CAAC,AACnD;AAAA,AAACC,UAAU,CAASF,iBAAiB,GAAGA,iBAAiB;AAKnD,eAAeP,eAAe,CAAC,EACpCU,OAAO,CAAA,EACPC,QAAQ,CAAA,EACRC,MAAM,CAAA,EACNC,gBAAgB,CAAA,EAChBC,YAAY,CAAA,EACZC,OAAO,CAAA,EACPC,aAAa,CAAA,EACbC,SAAS,CAAA,EACTC,eAAe,CAAA,EAWhB,EAIE;IACD,8DAA8D;IAC9D,SAAS;IACT,IAAIZ,aAAa,EAAE;QACjBJ,OAAO,CAACiB,IAAI,CAAC,CAAC,CAAC;KAChB;IAED,oCAAoC;IACpCX,OAAO,CAAC,iCAAiC,CAAC,CAACY,SAAS,CAACR,MAAM,CAAC;IAC5DS,CAAAA,GAAAA,OAA4B,AAG1B,CAAA,6BAH0B,CAAC;QAC3BR,gBAAgB;QAChBS,YAAY,EAAE;YAAER,YAAY;SAAE;KAC/B,CAAC;IAEF,MAAMS,UAAU,GAAG,MAAMC,CAAAA,GAAAA,eAAc,AAKrC,CAAA,eALqC,CAAC;QACtCd,OAAO;QACPC,QAAQ,EAAEO,eAAe,IAAIP,QAAQ;QACrCc,mBAAmB,EAAE,KAAK;QAC1BR,SAAS,EAAE,CAAC,CAACA,SAAS;KACvB,CAAC;IAEF,IAAI,CAACM,UAAU,CAACG,cAAc,IAAI,CAACT,SAAS,EAAE;QAC5C,yDAAyD;QACzD,mDAAmD;QACnD,MAAM,IAAIU,KAAK,CACb,CAAC,uDAAuD,EAAEhB,QAAQ,CAAC,CAAC,CACrE,CAAA;KACF;IACDL,aAAa,GAAG,IAAI;IAEpB,IAAIW,SAAS,EAAE;QACb,MAAMW,cAAc,GAAG,MAAMC,CAAAA,GAAAA,MAAqB,AAEjD,CAAA,sBAFiD,CAChDN,UAAU,CAACO,YAAY,CAACC,IAAI,CAC7B;QACD,OAAOC,CAAAA,GAAAA,MAAmB,AAIxB,CAAA,oBAJwB,CAAC;YACzBC,IAAI,EAAEtB,QAAQ;YACdiB,cAAc;YACdM,cAAc,EAAEtB,MAAM,CAACsB,cAAc;SACtC,CAAC,CAAA;KACH;IAED,OAAOC,CAAAA,GAAAA,MAAgB,AAMrB,CAAA,iBANqB,CAAC;QACtBF,IAAI,EAAEtB,QAAQ;QACde,cAAc,EAAEH,UAAU,CAACG,cAAc;QACzCQ,cAAc,EAAEtB,MAAM,CAACsB,cAAc;QACrCnB,OAAO;QACPC,aAAa;KACd,CAAC,CAAA;CACH"}