-
Notifications
You must be signed in to change notification settings - Fork 6.5k
refactor: split platform-specific code into platform-vercel and platform-cloudflare packages #8838
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
base: main
Are you sure you want to change the base?
Changes from all commits
40d90f4
a5ab910
8c900fd
eed7c1d
9a23e44
a349766
fdcd140
43c26ec
39b6baf
0a3b89c
8ae0372
b39010b
dda6268
d3f4233
092b640
6a73da9
d0d3a18
a0cec9d
130867d
3e24abf
57b7c38
c261b2c
aa7cfdd
24ef7e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { default } from '@platform/analytics'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1 @@ | ||
| export async function register() { | ||
| if (!('Cloudflare' in globalThis)) { | ||
| // Note: we don't need to set up the Vercel OTEL if the application is running on Cloudflare | ||
| const { registerOTel } = await import('@vercel/otel'); | ||
| registerOTel({ serviceName: 'nodejs-org' }); | ||
| } | ||
| } | ||
| export { register } from '@platform/instrumentation'; | ||
|
ovflowd marked this conversation as resolved.
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,30 @@ | ||
| 'use strict'; | ||
|
|
||
| import createNextIntlPlugin from 'next-intl/plugin'; | ||
|
|
||
| import { OPEN_NEXT_CLOUDFLARE } from './next.constants.cloudflare.mjs'; | ||
| import { BASE_PATH, ENABLE_STATIC_EXPORT } from './next.constants.mjs'; | ||
| import { getImagesConfig } from './next.image.config.mjs'; | ||
| import { DEPLOY_TARGET, PLATFORM_ALIAS } from './next.platform.constants.mjs'; | ||
| import { redirects, rewrites } from './next.rewrites.mjs'; | ||
|
|
||
| const getDeploymentId = async () => { | ||
| if (OPEN_NEXT_CLOUDFLARE) { | ||
| // If we're building for the Cloudflare deployment we want to set | ||
| // an appropriate deploymentId (needed for skew protection) | ||
| const openNextAdapter = await import('@opennextjs/cloudflare'); | ||
| /** | ||
| * Loaded by Node directly (Next.js doesn't bundle `next.config.mjs`), so | ||
| * we resolve the active platform via a dynamic import keyed on | ||
| * `DEPLOY_TARGET` rather than a `@platform/*` alias (those only resolve | ||
| * inside Turbopack/webpack). | ||
| * | ||
| * @type {{ default: import('./next.platform.config.d.ts').PlatformConfig }} | ||
| */ | ||
| const { default: platform } = await import(`${PLATFORM_ALIAS}/next.config.mjs`); | ||
|
|
||
| return openNextAdapter.getDeploymentId(); | ||
| } | ||
| const platformImages = await platform.images?.(); | ||
| const platformNextConfig = await platform.nextConfig?.(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These don't need to be functions set this this way. If we just pass |
||
|
|
||
| return undefined; | ||
| }; | ||
| // Turbopack's `resolveAlias` requires explicit `*` wildcards; webpack's | ||
| // `resolve.alias` matches the bare prefix and the `/*` form is invalid, | ||
| // so the two bundlers need different shapes for the same mapping. | ||
| const turbopackPlatformAliases = { '@platform/*': `${PLATFORM_ALIAS}/*` }; | ||
| const webpackPlatformAliases = { '@platform': PLATFORM_ALIAS }; | ||
|
Comment on lines
+23
to
+27
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a source for why we can't use node aliases, so it's good to know for the future? |
||
|
|
||
| /** @type {import('next').NextConfig} */ | ||
| const nextConfig = { | ||
|
|
@@ -27,9 +35,9 @@ const nextConfig = { | |
| // We allow the BASE_PATH to be overridden in case that the Website | ||
| // is being built on a subdirectory (e.g. /nodejs-website) | ||
| basePath: BASE_PATH, | ||
| // Vercel/Next.js Image Optimization Settings | ||
| images: getImagesConfig(), | ||
| images: getImagesConfig(platformImages), | ||
| serverExternalPackages: ['twoslash'], | ||
| transpilePackages: [PLATFORM_ALIAS], | ||
| outputFileTracingIncludes: { | ||
| // Twoslash needs TypeScript declarations to function, and, by default, Next.js | ||
| // strips them for brevity. Therefore, they must be explicitly included. | ||
|
|
@@ -81,8 +89,22 @@ const nextConfig = { | |
| // Faster Development Servers with Turbopack | ||
| turbopackFileSystemCacheForDev: true, | ||
| }, | ||
| deploymentId: await getDeploymentId(), | ||
| // Provide Turbopack Aliases for Platform Resolution | ||
| turbopack: { resolveAlias: turbopackPlatformAliases }, | ||
| // Provide Webpack Aliases for Platform Resolution. | ||
| webpack: ({ resolve, ...config }) => ({ | ||
| ...config, | ||
| resolve: { | ||
| ...resolve, | ||
| alias: { ...resolve.alias, ...webpackPlatformAliases }, | ||
| conditionNames: resolve.conditionNames | ||
| .concat(DEPLOY_TARGET) | ||
| .filter(Boolean), | ||
| }, | ||
| }), | ||
| ...platformNextConfig, | ||
| }; | ||
|
|
||
| const withNextIntl = createNextIntlPlugin('./i18n.tsx'); | ||
|
|
||
| export default withNextIntl(nextConfig); | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import type { HighlighterOptions } from '@node-core/rehype-shiki'; | ||
| import type { NextConfig } from 'next'; | ||
|
|
||
| type PlatformMdxConfig = Pick<HighlighterOptions, 'wasm' | 'twoslash'>; | ||
| type PlatformNextConfig = Pick<NextConfig, 'deploymentId' | 'env'>; | ||
|
|
||
| /** | ||
| * Shared platform-config contract consumed by `apps/site/next.config.mjs` | ||
| * and implemented by each `@node-core/platform-<target>` package. | ||
| * | ||
| * `nextConfig` and `images` are async thunks so that platform modules | ||
| * that depend on Node-only tooling (e.g. `@opennextjs/cloudflare`, | ||
| * `require.resolve`) can keep those imports out of the module's | ||
| * top-level. Webpack bundles the top level of this module into the | ||
| * server output; deferring heavy work into function bodies keeps the | ||
| * worker runtime free of build-only code. | ||
| */ | ||
| export type PlatformConfig = { | ||
| images?: () => Promise<NextConfig['images']>; | ||
| mdx?: PlatformMdxConfig; | ||
| nextConfig?: () => Promise<PlatformNextConfig>; | ||
| }; | ||
|
|
||
| declare const config: PlatformConfig; | ||
|
|
||
| export default config; | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| 'use strict'; | ||
|
|
||
| /** | ||
| * Identifies the deployment platform the site is being built for. | ||
| * | ||
| * Set by the deployment wrapper at build time: `vercel.json`'s `build.env` | ||
| * sets `vercel`, `open-next.config.ts`'s `buildCommand` sets `cloudflare`. | ||
| * Unset for standalone builds (local dev, static export). | ||
| * | ||
| * The `NEXT_PUBLIC_` prefix makes Next.js inline the value at build time, | ||
| * enabling dead-code elimination of platform-specific branches. | ||
| * | ||
| * @type {'vercel' | 'cloudflare' | 'default'} | ||
| */ | ||
| export const DEPLOY_TARGET = process.env.NEXT_PUBLIC_DEPLOY_TARGET ?? 'default'; | ||
|
|
||
| /** | ||
| * The alias for the platform. | ||
| * | ||
| * @type {string} | ||
| */ | ||
| export const PLATFORM_ALIAS = `@node-core/platform-${DEPLOY_TARGET}`; |
Uh oh!
There was an error while loading. Please reload this page.