Skip to content
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

Gate alternate bundler behind canary only #76634

Merged
merged 3 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/next-plugin-rspack/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# @next/plugin-rspack
# @next/plugin-rspack (EXPERIMENTAL)

> [!WARNING]
> This package is currently experimental and actively developed and supported in Next.j's `canary` branch. To use this, you must be using a published canary build of Next.js.

This plugin allows you to use [Rspack](https://rspack.dev) in place of webpack with Next.js.

Expand Down
3 changes: 2 additions & 1 deletion packages/next/errors.json
Original file line number Diff line number Diff line change
Expand Up @@ -649,5 +649,6 @@
"648": "@rspack/plugin-react-refresh is not available. Please make sure `@next/plugin-rspack` is correctly installed.",
"649": "Cache handlers not initialized",
"650": "experimental.nodeMiddleware",
"651": "Unexpected module type %s"
"651": "Unexpected module type %s",
"652": "Rspack support is only available in Next.js canary."
}
28 changes: 8 additions & 20 deletions packages/next/src/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { dset } from '../shared/lib/dset'
import { normalizeZodErrors } from '../shared/lib/zod'
import { HTML_LIMITED_BOT_UA_RE_STRING } from '../shared/lib/router/utils/is-bot'
import { findDir } from '../lib/find-pages-dir'
import { CanaryOnlyError, isStableBuild } from '../shared/lib/canary-only'

export { normalizeConfig } from './config-shared'
export type { DomainLocale, NextConfig } from './config-shared'
Expand Down Expand Up @@ -244,20 +245,18 @@ function assignDefaults(
)
}

if (
!process.env.__NEXT_VERSION?.includes('canary') &&
!process.env.__NEXT_TEST_MODE &&
!process.env.NEXT_PRIVATE_LOCAL_DEV
) {
if (isStableBuild()) {
// Prevents usage of certain experimental features outside of canary
if (result.experimental?.ppr) {
throw new CanaryOnlyError('experimental.ppr')
throw new CanaryOnlyError({ feature: 'experimental.ppr' })
} else if (result.experimental?.dynamicIO) {
throw new CanaryOnlyError('experimental.dynamicIO')
throw new CanaryOnlyError({ feature: 'experimental.dynamicIO' })
} else if (result.experimental?.turbo?.unstablePersistentCaching) {
throw new CanaryOnlyError('experimental.turbo.unstablePersistentCaching')
throw new CanaryOnlyError({
feature: 'experimental.turbo.unstablePersistentCaching',
})
} else if (result.experimental?.nodeMiddleware) {
throw new CanaryOnlyError('experimental.nodeMiddleware')
throw new CanaryOnlyError({ feature: 'experimental.nodeMiddleware' })
}
}

Expand Down Expand Up @@ -1370,14 +1369,3 @@ export function getConfiguredExperimentalFeatures(
}
return configuredExperimentalFeatures
}

class CanaryOnlyError extends Error {
constructor(feature: string) {
super(
`The experimental feature "${feature}" can only be enabled when using the latest canary version of Next.js.`
)
// This error is meant to interrupt the server start/build process
// but the stack trace isn't meaningful, as it points to internal code.
this.stack = undefined
}
}
23 changes: 23 additions & 0 deletions packages/next/src/shared/lib/canary-only.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export function isStableBuild() {
return (
!process.env.__NEXT_VERSION?.includes('canary') &&
!process.env.__NEXT_TEST_MODE &&
!process.env.NEXT_PRIVATE_LOCAL_DEV
)
}

export class CanaryOnlyError extends Error {
constructor(arg: { feature: string } | string) {
if (typeof arg === 'object' && 'feature' in arg) {
super(
`The experimental feature "${arg.feature}" can only be enabled when using the latest canary version of Next.js.`
)
} else {
super(arg)
}

// This error is meant to interrupt the server start/build process
// but the stack trace isn't meaningful, as it points to internal code.
this.stack = undefined
}
}
12 changes: 12 additions & 0 deletions packages/next/src/shared/lib/get-rspack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { CanaryOnlyError, isStableBuild } from './canary-only'

export function getRspackCore() {
gateCanary()
try {
// eslint-disable-next-line import/no-extraneous-dependencies
return require('@rspack/core')
Expand All @@ -14,6 +17,7 @@ export function getRspackCore() {
}

export function getRspackReactRefresh() {
gateCanary()
try {
// eslint-disable-next-line import/no-extraneous-dependencies
return require('@rspack/plugin-react-refresh')
Expand All @@ -27,3 +31,11 @@ export function getRspackReactRefresh() {
throw e
}
}

function gateCanary() {
if (isStableBuild()) {
throw new CanaryOnlyError(
'Rspack support is only available in Next.js canary.'
)
}
}
Loading