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

Ensure Webpack isn't loaded in render workers #52241

Merged
merged 2 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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