Skip to content

Commit

Permalink
Built-in app loader plugin for additional bundler
Browse files Browse the repository at this point in the history
Ported from https://github.com/vercel/next.js/tree/wbinnssmith/try-ci-test, originally by @hardfist.

Co-authored-by: hardfist <yangjianzju@gmail.com>
Co-authored-by: Tim Neutkens <tim@timneutkens.nl>
  • Loading branch information
3 people committed Feb 19, 2025
1 parent a70eccf commit 443fc11
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
13 changes: 11 additions & 2 deletions packages/next/src/build/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import * as Log from './output/log'
import type { LoadedEnvFiles } from '@next/env'
import type { AppLoaderOptions } from './webpack/loaders/next-app-loader'

import { posix, join, dirname, extname } from 'path'
import { posix, join, dirname, extname, normalize } from 'path'
import { stringify } from 'querystring'
import fs from 'fs'
import {
Expand Down Expand Up @@ -474,9 +474,18 @@ export function getInstrumentationEntry(opts: {
}
}

export function getAppLoader() {
return process.env.BUILTIN_APP_LOADER
? `builtin:next-app-loader`
: 'next-app-loader'
}

export function getAppEntry(opts: Readonly<AppLoaderOptions>) {
if (process.env.NEXT_RSPACK && process.env.BUILTIN_APP_LOADER) {
;(opts as any).projectRoot = normalize(join(__dirname, '../../..'))
}
return {
import: `next-app-loader?${stringify(opts)}!`,
import: `${getAppLoader()}?${stringify(opts)}!`,
layer: WEBPACK_LAYERS.reactServerComponents,
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import type { webpack } from 'next/dist/compiled/webpack/webpack'
import { relative } from 'path'
import { SimpleWebpackError } from './simpleWebpackError'
import { getAppLoader } from '../../../entries'

export function getNextAppLoaderError(
err: Error,
module: any,
compiler: webpack.Compiler
): SimpleWebpackError | false {
try {
if (!module.loaders[0].loader.includes('next-app-loader')) {
if (!module.loaders[0].loader.includes(getAppLoader())) {
return false
}

Expand Down
5 changes: 3 additions & 2 deletions packages/next/src/build/webpack/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
ModuleGraph,
} from 'webpack'
import type { ModuleGraphConnection } from 'webpack'
import { getAppLoader } from '../entries'

export function traverseModules(
compilation: Compilation,
Expand Down Expand Up @@ -61,7 +62,7 @@ export function forEachEntryModule(
if (
!request.startsWith('next-edge-ssr-loader?') &&
!request.startsWith('next-edge-app-route-loader?') &&
!request.startsWith('next-app-loader?')
!request.startsWith(`${getAppLoader()}?`)
)
continue

Expand All @@ -74,7 +75,7 @@ export function forEachEntryModule(
) {
entryModule.dependencies.forEach((dependency) => {
const modRequest: string | undefined = (dependency as any).request
if (modRequest?.includes('next-app-loader')) {
if (modRequest?.includes(getAppLoader())) {
entryModule = compilation.moduleGraph.getResolvedModule(dependency)
}
})
Expand Down

0 comments on commit 443fc11

Please sign in to comment.