-
Notifications
You must be signed in to change notification settings - Fork 79
Initial working version with the Adapters API #961
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,162 @@ | ||
| /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
| import fs from "node:fs"; | ||
| import { createRequire } from "node:module"; | ||
| import path from "node:path"; | ||
|
|
||
| import { compileCache } from "@opennextjs/aws/build/compileCache.js"; | ||
| import { compileOpenNextConfig } from "@opennextjs/aws/build/compileConfig.js"; | ||
| import { compileTagCacheProvider } from "@opennextjs/aws/build/compileTagCacheProvider.js"; | ||
| import { createCacheAssets, createStaticAssets } from "@opennextjs/aws/build/createAssets.js"; | ||
| import { createMiddleware } from "@opennextjs/aws/build/createMiddleware.js"; | ||
| import * as buildHelper from "@opennextjs/aws/build/helper.js"; | ||
| import { addDebugFile } from "@opennextjs/aws/debug.js"; | ||
| import type { ContentUpdater } from "@opennextjs/aws/plugins/content-updater.js"; | ||
| import { inlineRouteHandler } from "@opennextjs/aws/plugins/inlineRouteHandlers.js"; | ||
| import type { NextConfig } from "@opennextjs/aws/types/next-types.js"; | ||
|
|
||
| import { bundleServer } from "./build/bundle-server.js"; | ||
| import { compileEnvFiles } from "./build/open-next/compile-env-files.js"; | ||
| import { compileImages } from "./build/open-next/compile-images.js"; | ||
| import { compileInit } from "./build/open-next/compile-init.js"; | ||
| import { compileSkewProtection } from "./build/open-next/compile-skew-protection.js"; | ||
| import { compileDurableObjects } from "./build/open-next/compileDurableObjects.js"; | ||
| import { createServerBundle } from "./build/open-next/createServerBundle.js"; | ||
| import { inlineLoadManifest } from "./build/patches/plugins/load-manifest.js"; | ||
|
|
||
| export type NextAdapterOutputs = { | ||
| pages: any[]; | ||
| pagesApi: any[]; | ||
| appPages: any[]; | ||
| appRoutes: any[]; | ||
| }; | ||
|
|
||
| export type BuildCompleteCtx = { | ||
| routes: any; | ||
| outputs: NextAdapterOutputs; | ||
| projectDir: string; | ||
| repoRoot: string; | ||
| distDir: string; | ||
| config: NextConfig; | ||
| nextVersion: string; | ||
| }; | ||
|
|
||
| type NextAdapter = { | ||
| name: string; | ||
| modifyConfig: (config: NextConfig, { phase }: { phase: string }) => Promise<NextConfig>; | ||
| onBuildComplete: (ctx: BuildCompleteCtx) => Promise<void>; | ||
| }; //TODO: use the one provided by Next | ||
|
|
||
| let buildOpts: buildHelper.BuildOptions; | ||
|
|
||
| export default { | ||
| name: "OpenNext", | ||
|
|
||
| async modifyConfig(nextConfig) { | ||
| // We have to precompile the cache here, probably compile OpenNext config as well | ||
| const { config, buildDir } = await compileOpenNextConfig("open-next.config.ts", { | ||
| // TODO(vicb): do we need edge compile | ||
| compileEdge: true, | ||
| }); | ||
|
|
||
| const require = createRequire(import.meta.url); | ||
| const openNextDistDir = path.dirname(require.resolve("@opennextjs/aws/index.js")); | ||
|
|
||
| buildOpts = buildHelper.normalizeOptions(config, openNextDistDir, buildDir); | ||
|
|
||
| buildHelper.initOutputDir(buildOpts); | ||
|
|
||
| const cache = compileCache(buildOpts); | ||
|
|
||
| // We then have to copy the cache files to the .next dir so that they are available at runtime | ||
| // TODO: use a better path, this one is temporary just to make it work | ||
| const tempCachePath = `${buildOpts.outputDir}/server-functions/default/.open-next/.build`; | ||
| fs.mkdirSync(tempCachePath, { recursive: true }); | ||
| fs.copyFileSync(cache.cache, path.join(tempCachePath, "cache.cjs")); | ||
| fs.copyFileSync(cache.composableCache, path.join(tempCachePath, "composable-cache.cjs")); | ||
|
|
||
| //TODO: We should check the version of Next here, below 16 we'd throw or show a warning | ||
| return { | ||
| ...nextConfig, | ||
| cacheHandler: cache.cache, //TODO: compute that here, | ||
| cacheMaxMemorySize: 0, | ||
| experimental: { | ||
| ...nextConfig.experimental, | ||
| trustHostHeader: true, | ||
| cacheHandlers: { | ||
| default: cache.composableCache, | ||
| }, | ||
| }, | ||
| }; | ||
| }, | ||
|
|
||
| async onBuildComplete(ctx: BuildCompleteCtx) { | ||
| console.log("OpenNext build will start now"); | ||
|
|
||
| const configPath = path.join(buildOpts.appBuildOutputPath, ".open-next/.build/open-next.config.edge.mjs"); | ||
| if (!fs.existsSync(configPath)) { | ||
| throw new Error("Could not find compiled Open Next config, did you run the build command?"); | ||
| } | ||
| const openNextConfig = await import(configPath).then((mod) => mod.default); | ||
|
|
||
| // TODO(vicb): save outputs | ||
| addDebugFile(buildOpts, "outputs.json", ctx); | ||
|
|
||
| // Cloudflare specific | ||
| compileEnvFiles(buildOpts); | ||
| /* TODO(vicb): pass the wrangler config*/ | ||
| await compileInit(buildOpts, {} as any); | ||
| await compileImages(buildOpts); | ||
| await compileSkewProtection(buildOpts, openNextConfig); | ||
|
|
||
| // Compile middleware | ||
| // TODO(vicb): `forceOnlyBuildOnce` is cloudflare specific | ||
| await createMiddleware(buildOpts, { forceOnlyBuildOnce: true }); | ||
| console.log("Middleware created"); | ||
|
|
||
| createStaticAssets(buildOpts); | ||
| console.log("Static assets created"); | ||
|
|
||
| if (buildOpts.config.dangerous?.disableIncrementalCache !== true) { | ||
| const { useTagCache } = createCacheAssets(buildOpts); | ||
| console.log("Cache assets created"); | ||
| if (useTagCache) { | ||
| await compileTagCacheProvider(buildOpts); | ||
| console.log("Tag cache provider compiled"); | ||
| } | ||
| } | ||
|
|
||
| await createServerBundle( | ||
| buildOpts, | ||
| { | ||
| additionalPlugins: getAdditionalPluginsFactory(buildOpts, ctx), | ||
| }, | ||
| ctx | ||
| ); | ||
|
|
||
| await compileDurableObjects(buildOpts); | ||
|
|
||
| // TODO(vicb): pass minify `projectOpts` | ||
| await bundleServer(buildOpts, { minify: false } as any); | ||
|
|
||
| console.log("OpenNext build complete."); | ||
|
|
||
| // TODO(vicb): not needed on cloudflare | ||
| // console.log("Server bundle created"); | ||
| // await createRevalidationBundle(buildOpts); | ||
| // console.log("Revalidation bundle created"); | ||
| // await createImageOptimizationBundle(buildOpts); | ||
| // console.log("Image optimization bundle created"); | ||
| // await createWarmerBundle(buildOpts); | ||
| // console.log("Warmer bundle created"); | ||
| // await generateOutput(buildOpts); | ||
| // console.log("Output generated"); | ||
| }, | ||
| } satisfies NextAdapter; | ||
|
|
||
| function getAdditionalPluginsFactory(buildOpts: buildHelper.BuildOptions, ctx: BuildCompleteCtx) { | ||
| return (updater: ContentUpdater) => [ | ||
| inlineRouteHandler(updater, ctx.outputs), | ||
| //externalChunksPlugin(outputs), | ||
| inlineLoadManifest(updater, buildOpts), | ||
| ]; | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.