|
| 1 | +import { cp, mkdir, writeFile } from 'node:fs/promises' |
| 2 | +import { join, relative } from 'node:path/posix' |
| 3 | + |
| 4 | +import { glob } from 'fast-glob' |
| 5 | + |
| 6 | +import { |
| 7 | + DISPLAY_NAME_PAGES_AND_APP, |
| 8 | + GENERATOR, |
| 9 | + NETLIFY_FRAMEWORKS_API_FUNCTIONS, |
| 10 | + PLUGIN_DIR, |
| 11 | +} from './constants.js' |
| 12 | +import type { FrameworksAPIConfig, OnBuildCompleteContext } from './types.js' |
| 13 | + |
| 14 | +const PAGES_AND_APP_FUNCTION_INTERNAL_NAME = 'next_pages_and_app' |
| 15 | + |
| 16 | +const RUNTIME_DIR = '.netlify' |
| 17 | + |
| 18 | +const PAGES_AND_APP_FUNCTION_DIR = join( |
| 19 | + NETLIFY_FRAMEWORKS_API_FUNCTIONS, |
| 20 | + PAGES_AND_APP_FUNCTION_INTERNAL_NAME, |
| 21 | +) |
| 22 | + |
| 23 | +export async function onBuildComplete( |
| 24 | + ctx: OnBuildCompleteContext, |
| 25 | + frameworksAPIConfigArg: FrameworksAPIConfig, |
| 26 | +) { |
| 27 | + const frameworksAPIConfig: FrameworksAPIConfig = frameworksAPIConfigArg ?? {} |
| 28 | + |
| 29 | + const requiredFiles = new Set<string>() |
| 30 | + const pathnameToEntry: Record<string, string> = {} |
| 31 | + |
| 32 | + for (const outputs of [ |
| 33 | + ctx.outputs.pages, |
| 34 | + ctx.outputs.pagesApi, |
| 35 | + ctx.outputs.appPages, |
| 36 | + ctx.outputs.appRoutes, |
| 37 | + ]) { |
| 38 | + if (outputs) { |
| 39 | + for (const output of outputs) { |
| 40 | + if (output.runtime === 'edge') { |
| 41 | + // TODO: figure something out here |
| 42 | + continue |
| 43 | + } |
| 44 | + for (const asset of Object.values(output.assets)) { |
| 45 | + requiredFiles.add(asset) |
| 46 | + } |
| 47 | + |
| 48 | + requiredFiles.add(output.filePath) |
| 49 | + pathnameToEntry[output.pathname] = relative(ctx.repoRoot, output.filePath) |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + await mkdir(PAGES_AND_APP_FUNCTION_DIR, { recursive: true }) |
| 55 | + |
| 56 | + for (const filePath of requiredFiles) { |
| 57 | + await cp(filePath, join(PAGES_AND_APP_FUNCTION_DIR, relative(ctx.repoRoot, filePath)), { |
| 58 | + recursive: true, |
| 59 | + }) |
| 60 | + } |
| 61 | + |
| 62 | + // copy needed runtime files |
| 63 | + |
| 64 | + await copyRuntime(join(PAGES_AND_APP_FUNCTION_DIR, RUNTIME_DIR)) |
| 65 | + |
| 66 | + // generate needed runtime files |
| 67 | + const entrypoint = /* javascript */ ` |
| 68 | + import { AsyncLocalStorage } from 'node:async_hooks' |
| 69 | + import { createRequire } from 'node:module' |
| 70 | + import { runNextHandler } from './${RUNTIME_DIR}/dist/adapter/adapter-handler.js' |
| 71 | +
|
| 72 | + globalThis.AsyncLocalStorage = AsyncLocalStorage |
| 73 | +
|
| 74 | + const require = createRequire(import.meta.url) |
| 75 | +
|
| 76 | + const pathnameToEntry = ${JSON.stringify(pathnameToEntry, null, 2)} |
| 77 | +
|
| 78 | + export default async function handler(request, context) { |
| 79 | + const url = new URL(request.url) |
| 80 | +
|
| 81 | + const entry = pathnameToEntry[url.pathname] |
| 82 | + if (!entry) { |
| 83 | + return new Response('Not Found', { status: 404 }) |
| 84 | + } |
| 85 | +
|
| 86 | + const nextHandler = require('./' + entry).handler |
| 87 | +
|
| 88 | + return runNextHandler(request, context, nextHandler) |
| 89 | + } |
| 90 | +
|
| 91 | + export const config = { |
| 92 | + |
| 93 | + path: ${JSON.stringify(Object.keys(pathnameToEntry), null, 2)}, |
| 94 | + } |
| 95 | + ` |
| 96 | + await writeFile( |
| 97 | + join(PAGES_AND_APP_FUNCTION_DIR, `${PAGES_AND_APP_FUNCTION_INTERNAL_NAME}.mjs`), |
| 98 | + entrypoint, |
| 99 | + ) |
| 100 | + |
| 101 | + // configuration |
| 102 | + // TODO: ideally allow to set `includedFilesBasePath` via frameworks api config |
| 103 | + // frameworksAPIConfig.functions ??= { '*': {} } |
| 104 | + // frameworksAPIConfig.functions[PAGES_AND_APP_FUNCTION_INTERNAL_NAME] = { |
| 105 | + // node_bundler: 'none', |
| 106 | + // included_files: ['**'], |
| 107 | + // // we can't define includedFilesBasePath via Frameworks API |
| 108 | + // // included_files_base_path: PAGES_AND_APP_FUNCTION_DIR, |
| 109 | + // } |
| 110 | + |
| 111 | + // not using frameworks api because ... it doesn't allow to set includedFilesBasePath |
| 112 | + await writeFile( |
| 113 | + join(PAGES_AND_APP_FUNCTION_DIR, `${PAGES_AND_APP_FUNCTION_INTERNAL_NAME}.json`), |
| 114 | + JSON.stringify( |
| 115 | + { |
| 116 | + config: { |
| 117 | + name: DISPLAY_NAME_PAGES_AND_APP, |
| 118 | + generator: GENERATOR, |
| 119 | + node_bundler: 'none', |
| 120 | + included_files: ['**'], |
| 121 | + includedFilesBasePath: PAGES_AND_APP_FUNCTION_DIR, |
| 122 | + }, |
| 123 | + }, |
| 124 | + null, |
| 125 | + 2, |
| 126 | + ), |
| 127 | + ) |
| 128 | + |
| 129 | + return frameworksAPIConfig |
| 130 | +} |
| 131 | + |
| 132 | +const copyRuntime = async (handlerDirectory: string): Promise<void> => { |
| 133 | + const files = await glob('dist/**/*', { |
| 134 | + cwd: PLUGIN_DIR, |
| 135 | + ignore: ['**/*.test.ts'], |
| 136 | + dot: true, |
| 137 | + }) |
| 138 | + await Promise.all( |
| 139 | + files.map((path) => |
| 140 | + cp(join(PLUGIN_DIR, path), join(handlerDirectory, path), { recursive: true }), |
| 141 | + ), |
| 142 | + ) |
| 143 | + // We need to create a package.json file with type: module to make sure that the runtime modules |
| 144 | + // are handled correctly as ESM modules |
| 145 | + await writeFile(join(handlerDirectory, 'package.json'), JSON.stringify({ type: 'module' })) |
| 146 | +} |
0 commit comments