Skip to content

Commit

Permalink
Ensure Webpack isn't loaded in render workers (#52241)
Browse files Browse the repository at this point in the history
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">
  • Loading branch information
shuding committed Jul 8, 2023
1 parent 6627903 commit dd8134c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NextTypesPlugin } from './next-types-plugin'
import { NextTypesPlugin } from '.'

describe('next-types-plugin', () => {
it('should generate correct base import path', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
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'
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'

Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// TODO: Eliminate this singleton in the future.
export const devPageFiles = new Set<string>()
5 changes: 4 additions & 1 deletion packages/next/src/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
13 changes: 9 additions & 4 deletions packages/next/src/server/dev/next-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -911,6 +915,7 @@ export default class DevServer extends Server {
telemetry,
})
}

await super.prepareImpl()
await this.addExportPathMapRoutes()
await this.hotReloader?.start()
Expand Down

0 comments on commit dd8134c

Please sign in to comment.