Skip to content

Commit

Permalink
Load function meta info from middleware manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuhaow committed Aug 25, 2022
1 parent 8c2155f commit 4540176
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 18 additions & 25 deletions src/command/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down Expand Up @@ -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<string[]> {
const webpackRuntimePath = resolve(
nextDir,
Expand All @@ -105,31 +103,26 @@ async function getEdgeScriptList(nextDir: PathLike): Promise<string[]> {
}

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) {
Expand Down

0 comments on commit 4540176

Please sign in to comment.