Unverified Commit ef89890f authored by Shu Ding's avatar Shu Ding Committed by GitHub

Ensure Webpack isn't loaded in render workers (#52241)

There's no need to require the HotLoader module as well as Webpack in render workers, as we are not running compilation there. This reduces 10 MB memory in total and some time from `require`ing Webpack.

Before:

<img width="1639" alt="CleanShot 2023-07-05 at 11 33 39@2x" src="https://github.com/vercel/next.js/assets/3676859/48f31051-7a27-4cf6-94aa-1b2ddf313f20">

After:

<img width="1639" alt="CleanShot 2023-07-05 at 12 02 04@2x" src="https://github.com/vercel/next.js/assets/3676859/4c717d75-c0cd-458f-a6c9-6f967ae6df90">
parent baf3d2fa
import { NextTypesPlugin } from './next-types-plugin'
import { NextTypesPlugin } from '.'
describe('next-types-plugin', () => {
it('should generate correct base import path', () => {
......
import type { Rewrite, Redirect } from '../../../lib/load-custom-routes'
import type { Rewrite, Redirect } from '../../../../lib/load-custom-routes'
import type { Token } from 'next/dist/compiled/path-to-regexp'
import fs from 'fs/promises'
......@@ -6,14 +6,15 @@ import { webpack, sources } from 'next/dist/compiled/webpack/webpack'
import { parse } from 'next/dist/compiled/path-to-regexp'
import path from 'path'
import { WEBPACK_LAYERS } from '../../../lib/constants'
import { denormalizePagePath } from '../../../shared/lib/page-path/denormalize-page-path'
import { ensureLeadingSlash } from '../../../shared/lib/page-path/ensure-leading-slash'
import { normalizePathSep } from '../../../shared/lib/page-path/normalize-path-sep'
import { HTTP_METHODS } from '../../../server/web/http'
import { isDynamicRoute } from '../../../shared/lib/router/utils'
import { normalizeAppPath } from '../../../shared/lib/router/utils/app-paths'
import { getPageFromPath } from '../../entries'
import { WEBPACK_LAYERS } from '../../../../lib/constants'
import { denormalizePagePath } from '../../../../shared/lib/page-path/denormalize-page-path'
import { ensureLeadingSlash } from '../../../../shared/lib/page-path/ensure-leading-slash'
import { normalizePathSep } from '../../../../shared/lib/page-path/normalize-path-sep'
import { HTTP_METHODS } from '../../../../server/web/http'
import { isDynamicRoute } from '../../../../shared/lib/router/utils'
import { normalizeAppPath } from '../../../../shared/lib/router/utils/app-paths'
import { getPageFromPath } from '../../../entries'
import { devPageFiles } from './shared'
const PLUGIN_NAME = 'NextTypesPlugin'
......@@ -188,8 +189,6 @@ async function collectNamedSlots(layoutPath: string) {
return slots
}
export const devPageFiles = new Set<string>()
// By exposing the static route types separately as string literals,
// editors can provide autocompletion for them. However it's currently not
// possible to provide the same experience for dynamic routes.
......
// TODO: Eliminate this singleton in the future.
export const devPageFiles = new Set<string>()
......@@ -747,7 +747,10 @@ export default async function loadConfig(
: Log
await loadEnvConfig(dir, phase === PHASE_DEVELOPMENT_SERVER, curLog)
loadWebpackHook()
if (!process.env.__NEXT_PRIVATE_RENDER_WORKER) {
loadWebpackHook()
}
let configFileName = 'next.config.js'
......
......@@ -12,6 +12,7 @@ import type { MiddlewareRoutingItem } from '../base-server'
import type { MiddlewareMatcher } from '../../build/analysis/get-page-static-info'
import type { FunctionComponent } from 'react'
import type { RouteMatch } from '../future/route-matches/route-match'
import type { default as THotReloader } from './hot-reloader'
import fs from 'fs'
import { Worker } from 'next/dist/compiled/jest-worker'
......@@ -49,7 +50,6 @@ import { removePathPrefix } from '../../shared/lib/router/utils/remove-path-pref
import { eventCliSession } from '../../telemetry/events'
import { Telemetry } from '../../telemetry/storage'
import { setGlobal } from '../../trace'
import HotReloader from './hot-reloader'
import { createValidFileMatcher, findPageFile } from '../lib/find-page-file'
import { getNodeOptionsWithoutInspect } from '../lib/utils'
import {
......@@ -79,10 +79,9 @@ import {
isMiddlewareFile,
NestedMiddlewareError,
} from '../../build/utils'
import { getDefineEnv } from '../../build/webpack-config'
import loadJsConfig from '../../build/load-jsconfig'
import { formatServerError } from '../../lib/format-server-error'
import { devPageFiles } from '../../build/webpack/plugins/next-types-plugin'
import { devPageFiles } from '../../build/webpack/plugins/next-types-plugin/shared'
import {
DevRouteMatcherManager,
RouteEnsurer,
......@@ -126,7 +125,7 @@ export default class DevServer extends Server {
private devReady: Promise<void>
private setDevReady?: Function
private webpackWatcher?: any | null
private hotReloader?: HotReloader
private hotReloader?: THotReloader
private isCustomServer: boolean
protected sortedRoutes?: string[]
private addedUpgradeListener = false
......@@ -723,6 +722,8 @@ export default class DevServer extends Server {
typeof plugin.definitions === 'object' &&
plugin.definitions.__NEXT_DEFINE_ENV
) {
const { getDefineEnv } =
require('../../build/webpack-config') as typeof import('../../build/webpack-config')
const newDefine = getDefineEnv({
dev: true,
config: this.nextConfig,
......@@ -900,6 +901,9 @@ export default class DevServer extends Server {
// router worker does not start webpack compilers
if (!this.isRenderWorker) {
const { default: HotReloader } =
require('./hot-reloader') as typeof import('./hot-reloader')
this.hotReloader = new HotReloader(this.dir, {
pagesDir: this.pagesDir,
distDir: this.distDir,
......@@ -911,6 +915,7 @@ export default class DevServer extends Server {
telemetry,
})
}
await super.prepareImpl()
await this.addExportPathMapRoutes()
await this.hotReloader?.start()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment