From 4540176f3c9454dde2839e2d3932cdaf5cc65a72 Mon Sep 17 00:00:00 2001 From: zhuhaow Date: Thu, 25 Aug 2022 10:32:13 +0800 Subject: [PATCH] Load function meta info from middleware manifest --- package.json | 4 +++- pnpm-lock.yaml | 8 ++++++++ src/command/build.ts | 43 ++++++++++++++++++------------------------- 3 files changed, 29 insertions(+), 26 deletions(-) diff --git a/package.json b/package.json index e53d05a..0c1fe7c 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,9 @@ "ts-node": "10.9.1", "typescript": "4.7.4", "wrangler": "2.0.27", - "xo": "0.51.0" + "xo": "0.51.0", + "@types/lodash": "^4.14.184", + "lodash": "^4.17.21" }, "peerDependencies": { "@types/react": "^17.0.2 || ^18.0.0-0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7459ccd..17b36fa 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4,6 +4,7 @@ specifiers: '@types/fs-extra': 9.0.13 '@types/jest': 28.1.7 '@types/klaw': 3.0.3 + '@types/lodash': ^4.14.184 '@types/node': 18.7.8 '@types/react': ^17.0.2 || ^18.0.0-0 '@types/react-dom': ^17.0.2 || ^18.0.0-0 @@ -14,6 +15,7 @@ specifiers: fs-extra: 10.1.0 jest: 28.1.3 klaw: 4.0.1 + lodash: ^4.17.21 next: ^12.2.5 react: ^17.0.2 || ^18.0.0-0 react-dom: ^17.0.2 || ^18.0.0-0 @@ -34,6 +36,7 @@ devDependencies: '@types/fs-extra': 9.0.13 '@types/jest': 28.1.7 '@types/klaw': 3.0.3 + '@types/lodash': 4.14.184 '@types/node': 18.7.8 add: 2.0.6 commander: 9.4.0 @@ -42,6 +45,7 @@ devDependencies: fs-extra: 10.1.0 jest: 28.1.3_famynuxl7vhnyacjiboum65kqi klaw: 4.0.1 + lodash: 4.17.21 ts-jest: 28.0.8_u4nqbvvkpmo3si3ne46plvlwbe ts-node: 10.9.1_itmtyrrie7wpjnrpwbb5uqyzwa typescript: 4.7.4 @@ -1153,6 +1157,10 @@ packages: '@types/node': 18.7.8 dev: true + /@types/lodash/4.14.184: + resolution: {integrity: sha512-RoZphVtHbxPZizt4IcILciSWiC6dcn+eZ8oX9IWEYfDMcocdd42f7NPI6fQj+6zI8y4E0L7gu2pcZKLGTRaV9Q==} + dev: true + /@types/minimist/1.2.2: resolution: {integrity: sha512-jhuKLIRrhvCPLqwPcx6INqmKeiA5EWrsCOPhrlFSrbrmU4ZMPjj5Ul/oLCMDO98XRUIwVm78xICz4EPCektzeQ==} dev: true diff --git a/src/command/build.ts b/src/command/build.ts index 5242322..bcc2e2f 100644 --- a/src/command/build.ts +++ b/src/command/build.ts @@ -11,6 +11,8 @@ import { writeFile, } from 'fs-extra'; import klaw from 'klaw'; +import {MiddlewareManifest} from 'next/dist/build/webpack/plugins/middleware-plugin'; +import {concat, uniq} from 'lodash'; export async function build(nextDir: PathLike, outputDir: PathLike) { nextDir = normalize(resolve(nextDir.toString())); @@ -89,10 +91,6 @@ async function buildId(nextDir: PathLike) { return readFile(resolve(nextDir, '.next/BUILD_ID'), 'utf8'); } -interface NftResult { - files: string[]; -} - async function getEdgeScriptList(nextDir: PathLike): Promise { const webpackRuntimePath = resolve( nextDir, @@ -105,31 +103,26 @@ async function getEdgeScriptList(nextDir: PathLike): Promise { } const result: string[] = []; - // Don't why these files are put under page instead of server/page, seems like an error. - for await (const file of klaw(resolve(nextDir, '.next/pages'))) { - if (file.path.endsWith('.nft.json')) { - const nft = (await readJson(file.path)) as NftResult; - if ( - nft.files.findIndex( - (f) => resolve(dirname(file.path), f) === webpackRuntimePath, - ) !== -1 - ) { - result.push( - ...nft.files - .filter( - (f) => - f.endsWith('.js') && - resolve(dirname(file.path), f) !== webpackRuntimePath, - ) - .map((f) => resolve(dirname(file.path), f)), - ); - } - } + const middlewareManifest = (await readJson( + resolve(nextDir, '.next/server/middleware-manifest.json'), + )) as MiddlewareManifest; + + // Since there is no more nested middleware, there should be only + // one entry here. + for (const entry of concat( + Object.values(middlewareManifest.middleware), + Object.values(middlewareManifest.functions), + )) { + result.push( + ...entry.files + .map((file) => resolve(nextDir, '.next', file)) + .filter((file) => file !== webpackRuntimePath), + ); } result.push(webpackRuntimePath); - return result; + return uniq(result); } async function buildHandler(nextDir: PathLike, outputDir: PathLike) {