Unverified Commit 47a61207 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update .env HMR handling (#39566)

parent 91d09bba
......@@ -13,6 +13,7 @@ export type LoadedEnvFiles = Array<{
let initialEnv: Env | undefined = undefined
let combinedEnv: Env | undefined = undefined
let cachedLoadedEnvFiles: LoadedEnvFiles = []
let previousLoadedEnvFiles: LoadedEnvFiles = []
type Log = {
info: (...args: any[]) => void
......@@ -49,7 +50,13 @@ export function processEnv(
result = dotenvExpand(result)
if (result.parsed) {
if (
result.parsed &&
!previousLoadedEnvFiles.some(
(item) =>
item.contents === envFile.contents && item.path === envFile.path
)
) {
log.info(`Loaded env from ${path.join(dir || '', envFile.path)}`)
}
......@@ -88,6 +95,7 @@ export function loadEnvConfig(
return { combinedEnv, loadedEnvFiles: cachedLoadedEnvFiles }
}
process.env = Object.assign({}, initialEnv)
previousLoadedEnvFiles = cachedLoadedEnvFiles
cachedLoadedEnvFiles = []
const isTest = process.env.NODE_ENV === 'test'
......
This diff is collapsed.
import type { webpack5 } from 'next/dist/compiled/webpack/webpack'
import { webpack5 } from 'next/dist/compiled/webpack/webpack'
import type { NextConfigComplete } from '../config-shared'
import type { CustomRoutes } from '../../lib/load-custom-routes'
import { getOverlayMiddleware } from 'next/dist/compiled/@next/react-dev-overlay/dist/middleware'
......@@ -48,6 +48,7 @@ import ws from 'next/dist/compiled/ws'
import { promises as fs } from 'fs'
import { getPageStaticInfo } from '../../build/analysis/get-page-static-info'
import { serverComponentRegex } from '../../build/webpack/loaders/utils'
import { UnwrapPromise } from '../../lib/coalesced-function'
const wsServer = new ws.Server({ noServer: true })
......@@ -159,8 +160,8 @@ export default class HotReloader {
private distDir: string
private webpackHotMiddleware?: WebpackHotMiddleware
private config: NextConfigComplete
private hasServerComponents: boolean
private hasReactRoot: boolean
public hasServerComponents: boolean
public hasReactRoot: boolean
public clientStats: webpack5.Stats | null
public serverStats: webpack5.Stats | null
public edgeServerStats: webpack5.Stats | null
......@@ -176,6 +177,10 @@ export default class HotReloader {
private hotReloaderSpan: Span
private pagesMapping: { [key: string]: string } = {}
private appDir?: string
public multiCompiler?: webpack5.MultiCompiler
public activeConfigs?: Array<
UnwrapPromise<ReturnType<typeof getBaseWebpackConfig>>
>
constructor(
dir: string,
......@@ -452,6 +457,7 @@ export default class HotReloader {
.traceChild('generate-webpack-config')
.traceAsyncFn(() =>
Promise.all([
// order is important here
getBaseWebpackConfig(this.dir, {
...commonWebpackOptions,
compilerType: COMPILER_NAMES.client,
......@@ -525,24 +531,23 @@ export default class HotReloader {
})
}
public async start(): Promise<void> {
public async start(initial?: boolean): Promise<void> {
const startSpan = this.hotReloaderSpan.traceChild('start')
startSpan.stop() // Stop immediately to create an artificial parent span
await this.clean(startSpan)
if (initial) {
await this.clean(startSpan)
// Ensure distDir exists before writing package.json
await fs.mkdir(this.distDir, { recursive: true })
// Ensure distDir exists before writing package.json
await fs.mkdir(this.distDir, { recursive: true })
const distPackageJsonPath = join(this.distDir, 'package.json')
// Ensure commonjs handling is used for files in the distDir (generally .next)
// Files outside of the distDir can be "type": "module"
await fs.writeFile(distPackageJsonPath, '{"type": "commonjs"}')
const configs = await this.getWebpackConfig(startSpan)
const distPackageJsonPath = join(this.distDir, 'package.json')
// Ensure commonjs handling is used for files in the distDir (generally .next)
// Files outside of the distDir can be "type": "module"
await fs.writeFile(distPackageJsonPath, '{"type": "commonjs"}')
}
this.activeConfigs = await this.getWebpackConfig(startSpan)
for (const config of configs) {
for (const config of this.activeConfigs) {
const defaultEntry = config.entry
config.entry = async (...args) => {
// @ts-ignore entry is always a function
......@@ -679,14 +684,16 @@ export default class HotReloader {
// Enable building of client compilation before server compilation in development
// @ts-ignore webpack 5
configs.parallelism = 1
this.activeConfigs.parallelism = 1
const multiCompiler = webpack(configs) as unknown as webpack5.MultiCompiler
this.multiCompiler = webpack(
this.activeConfigs
) as unknown as webpack5.MultiCompiler
watchCompilers(
multiCompiler.compilers[0],
multiCompiler.compilers[1],
multiCompiler.compilers[2]
this.multiCompiler.compilers[0],
this.multiCompiler.compilers[1],
this.multiCompiler.compilers[2]
)
// Watch for changes to client/server page files so we can tell when just
......@@ -757,21 +764,21 @@ export default class HotReloader {
}
}
multiCompiler.compilers[0].hooks.emit.tap(
this.multiCompiler.compilers[0].hooks.emit.tap(
'NextjsHotReloaderForClient',
trackPageChanges(prevClientPageHashes, changedClientPages)
)
multiCompiler.compilers[1].hooks.emit.tap(
this.multiCompiler.compilers[1].hooks.emit.tap(
'NextjsHotReloaderForServer',
trackPageChanges(prevServerPageHashes, changedServerPages)
)
multiCompiler.compilers[2].hooks.emit.tap(
this.multiCompiler.compilers[2].hooks.emit.tap(
'NextjsHotReloaderForServer',
trackPageChanges(prevEdgeServerPageHashes, changedEdgeServerPages)
)
// This plugin watches for changes to _document.js and notifies the client side that it should reload the page
multiCompiler.compilers[1].hooks.failed.tap(
this.multiCompiler.compilers[1].hooks.failed.tap(
'NextjsHotReloaderForServer',
(err: Error) => {
this.serverError = err
......@@ -779,7 +786,7 @@ export default class HotReloader {
}
)
multiCompiler.compilers[2].hooks.done.tap(
this.multiCompiler.compilers[2].hooks.done.tap(
'NextjsHotReloaderForServer',
(stats) => {
this.serverError = null
......@@ -787,7 +794,7 @@ export default class HotReloader {
}
)
multiCompiler.compilers[1].hooks.done.tap(
this.multiCompiler.compilers[1].hooks.done.tap(
'NextjsHotReloaderForServer',
(stats) => {
this.serverError = null
......@@ -820,7 +827,7 @@ export default class HotReloader {
this.serverPrevDocumentHash = documentChunk.hash || null
}
)
multiCompiler.hooks.done.tap('NextjsHotReloaderForServer', () => {
this.multiCompiler.hooks.done.tap('NextjsHotReloaderForServer', () => {
const serverOnlyChanges = difference<string>(
changedServerPages,
changedClientPages
......@@ -862,14 +869,14 @@ export default class HotReloader {
}
})
multiCompiler.compilers[0].hooks.failed.tap(
this.multiCompiler.compilers[0].hooks.failed.tap(
'NextjsHotReloaderForClient',
(err: Error) => {
this.clientError = err
this.clientStats = null
}
)
multiCompiler.compilers[0].hooks.done.tap(
this.multiCompiler.compilers[0].hooks.done.tap(
'NextjsHotReloaderForClient',
(stats) => {
this.clientError = null
......@@ -908,15 +915,15 @@ export default class HotReloader {
)
this.webpackHotMiddleware = new WebpackHotMiddleware(
multiCompiler.compilers
this.multiCompiler.compilers
)
let booted = false
this.watcher = await new Promise((resolve) => {
const watcher = multiCompiler.watch(
const watcher = this.multiCompiler?.watch(
// @ts-ignore webpack supports an array of watchOptions when using a multiCompiler
configs.map((config) => config.watchOptions!),
this.activeConfigs.map((config) => config.watchOptions!),
// Errors are handled separately
(_err: any) => {
if (!booted) {
......@@ -928,7 +935,7 @@ export default class HotReloader {
})
this.onDemandEntries = onDemandEntryHandler({
multiCompiler,
multiCompiler: this.multiCompiler,
pagesDir: this.pagesDir,
appDir: this.appDir,
rootDir: this.dir,
......@@ -950,7 +957,13 @@ export default class HotReloader {
// trigger invalidation to ensure any previous callbacks
// are handled in the on-demand-entry-handler
getInvalidator()?.invalidate()
if (!initial) {
this.invalidate()
}
}
public invalidate() {
return getInvalidator()?.invalidate()
}
public async stop(): Promise<void> {
......@@ -965,6 +978,7 @@ export default class HotReloader {
)
})
}
this.multiCompiler = undefined
}
public async getCompilationErrors(page: string) {
......
......@@ -72,6 +72,7 @@ import {
isMiddlewareFile,
NestedMiddlewareError,
} from '../../build/utils'
import { getDefineEnv } from '../../build/webpack-config'
// Load ReactDevOverlay only when needed
let ReactDevOverlayImpl: React.FunctionComponent
......@@ -391,8 +392,46 @@ export default class DevServer extends Server {
if (envChange) {
this.loadEnvConfig({ dev: true, forceReload: true })
await this.hotReloader?.stop()
await this.hotReloader?.start()
this.hotReloader?.activeConfigs?.forEach((config, idx) => {
const isClient = idx === 0
const isNodeServer = idx === 1
const isEdgeServer = idx === 2
const hasRewrites =
this.customRoutes.rewrites.afterFiles.length > 0 ||
this.customRoutes.rewrites.beforeFiles.length > 0 ||
this.customRoutes.rewrites.fallback.length > 0
config.plugins?.forEach((plugin: any) => {
// we look for the DefinePlugin definitions so we can
// update them on the active compilers
if (
plugin &&
typeof plugin.definitions === 'object' &&
plugin.definitions.__NEXT_DEFINE_ENV
) {
const newDefine = getDefineEnv({
dev: true,
config: this.nextConfig,
distDir: this.distDir,
isClient,
hasRewrites,
hasReactRoot: this.hotReloader?.hasReactRoot,
isNodeServer,
isEdgeServer,
hasServerComponents: this.hotReloader?.hasServerComponents,
})
Object.keys(plugin.definitions).forEach((key) => {
if (!(key in newDefine)) {
delete plugin.definitions[key]
}
})
Object.assign(plugin.definitions, newDefine)
}
})
})
this.hotReloader?.invalidate()
}
if (nestedMiddleware.length > 0) {
......@@ -512,7 +551,7 @@ export default class DevServer extends Server {
})
await super.prepare()
await this.addExportPathMapRoutes()
await this.hotReloader.start()
await this.hotReloader.start(true)
await this.startWatcher()
this.setDevReady!()
......
export default () => <p>{process.env.NEXT_PUBLIC_HELLO_WORLD}</p>
export default function Page() {
return <p>{process.env.NEXT_PUBLIC_HELLO_WORLD}</p>
}
......@@ -27,7 +27,7 @@ const variables = [
'NEXT_PUBLIC_HELLO_WORLD',
]
export default async (req, res) => {
export default async function handler(req, res) {
const items = {
nextConfigEnv: process.env.nextConfigEnv,
nextConfigPublicEnv: process.env.nextConfigPublicEnv,
......
export default () => <p>{process.env.NEXT_PUBLIC_TEST_DEST}</p>
export default function Page() {
return <p>{process.env.NEXT_PUBLIC_TEST_DEST}</p>
}
......@@ -44,10 +44,12 @@ export async function getStaticProps() {
}
}
export default ({ env }) => (
<>
<p>{JSON.stringify(env)}</p>
<div id="nextConfigEnv">{process.env.nextConfigEnv}</div>
<div id="nextConfigPublicEnv">{process.env.nextConfigPublicEnv}</div>
</>
)
export default function Page({ env }) {
return (
<>
<p>{JSON.stringify(env)}</p>
<div id="nextConfigEnv">{process.env.nextConfigEnv}</div>
<div id="nextConfigPublicEnv">{process.env.nextConfigPublicEnv}</div>
</>
)
}
......@@ -44,10 +44,12 @@ export async function getStaticProps() {
}
}
export default ({ env }) => (
<>
<p>{JSON.stringify(env)}</p>
<div id="nextConfigEnv">{process.env.nextConfigEnv}</div>
<div id="nextConfigPublicEnv">{process.env.nextConfigPublicEnv}</div>
</>
)
export default function Page({ env }) {
return (
<>
<p>{JSON.stringify(env)}</p>
<div id="nextConfigEnv">{process.env.nextConfigEnv}</div>
<div id="nextConfigPublicEnv">{process.env.nextConfigPublicEnv}</div>
</>
)
}
......@@ -43,10 +43,12 @@ export async function getServerSideProps() {
}
}
export default ({ env }) => (
<>
<p>{JSON.stringify(env)}</p>
<div id="nextConfigEnv">{process.env.nextConfigEnv}</div>
<div id="nextConfigPublicEnv">{process.env.nextConfigPublicEnv}</div>
</>
)
export default function Page({ env }) {
return (
<>
<p>{JSON.stringify(env)}</p>
<div id="nextConfigEnv">{process.env.nextConfigEnv}</div>
<div id="nextConfigPublicEnv">{process.env.nextConfigPublicEnv}</div>
</>
)
}
......@@ -4,6 +4,7 @@ import url from 'url'
import fs from 'fs-extra'
import { join } from 'path'
import cheerio from 'cheerio'
import webdriver from 'next-webdriver'
import {
nextBuild,
nextStart,
......@@ -170,6 +171,9 @@ describe('Env Config', () => {
const originalContents = []
beforeAll(async () => {
const outputIndex = output.length
const envFiles = (await fs.readdir(appDir)).filter((file) =>
file.startsWith('.env')
)
const envToUpdate = [
{
toAdd: 'NEW_ENV_KEY=true',
......@@ -185,12 +189,16 @@ describe('Env Config', () => {
},
]
for (const { file, toAdd } of envToUpdate) {
const content = await fs.readFile(join(appDir, file), 'utf8')
for (const file of envFiles) {
const filePath = join(appDir, file)
const content = await fs.readFile(filePath, 'utf8')
originalContents.push({ file, content })
await fs.writeFile(join(appDir, file), content + '\n' + toAdd)
}
const toUpdate = envToUpdate.find((item) => item.file === file)
if (toUpdate) {
await fs.writeFile(filePath, content + `\n${toUpdate.toAdd}`)
}
}
await check(() => {
return output.substring(outputIndex)
}, /Loaded env from/)
......@@ -203,24 +211,138 @@ describe('Env Config', () => {
runTests('dev', true)
it('should update inlined values correctly', async () => {
await renderViaHTTP(appPort, '/another-global')
const buildManifest = await fs.readJson(
join(__dirname, '../app/.next/build-manifest.json')
)
const pageFile = buildManifest.pages['/another-global'].find(
(filename) => filename.includes('pages/another-global')
)
it('should have updated runtime values after change', async () => {
let html = await fetchViaHTTP(appPort, '/').then((res) => res.text())
let renderedEnv = JSON.parse(cheerio.load(html)('p').text())
expect(renderedEnv['ENV_FILE_KEY']).toBe('env')
expect(renderedEnv['ENV_FILE_LOCAL_OVERRIDE_TEST']).toBe('localenv')
let outputIdx = output.length
const envFile = join(appDir, '.env')
const envLocalFile = join(appDir, '.env.local')
const envContent = originalContents.find(
(item) => item.file === '.env'
).content
const envLocalContent = originalContents.find(
(item) => item.file === '.env.local'
).content
try {
await fs.writeFile(
envFile,
envContent.replace(`ENV_FILE_KEY=env`, `ENV_FILE_KEY=env-updated`)
)
// we should only log we loaded new env from .env
await check(() => output.substring(outputIdx), /Loaded env from/)
expect(
[...output.substring(outputIdx).matchAll(/Loaded env from/g)].length
).toBe(1)
expect(output.substring(outputIdx)).not.toContain('.env.local')
await check(async () => {
html = await fetchViaHTTP(appPort, '/').then((res) => res.text())
renderedEnv = JSON.parse(cheerio.load(html)('p').text())
expect(renderedEnv['ENV_FILE_KEY']).toBe('env-updated')
expect(renderedEnv['ENV_FILE_LOCAL_OVERRIDE_TEST']).toBe('localenv')
return 'success'
}, 'success')
outputIdx = output.length
await fs.writeFile(
envLocalFile,
envLocalContent.replace(
`ENV_FILE_LOCAL_OVERRIDE_TEST=localenv`,
`ENV_FILE_LOCAL_OVERRIDE_TEST=localenv-updated`
)
)
// we should only log we loaded new env from .env
await check(() => output.substring(outputIdx), /Loaded env from/)
expect(
[...output.substring(outputIdx).matchAll(/Loaded env from/g)].length
).toBe(1)
expect(output.substring(outputIdx)).toContain('.env.local')
await check(async () => {
html = await fetchViaHTTP(appPort, '/').then((res) => res.text())
renderedEnv = JSON.parse(cheerio.load(html)('p').text())
expect(renderedEnv['ENV_FILE_KEY']).toBe('env-updated')
expect(renderedEnv['ENV_FILE_LOCAL_OVERRIDE_TEST']).toBe(
'localenv-updated'
)
return 'success'
}, 'success')
} finally {
await fs.writeFile(envFile, envContent)
await fs.writeFile(envLocalFile, envLocalContent)
}
})
// read client bundle contents since a server side render can
// have the value available during render but it not be injected
const bundleContent = await fs.readFile(
join(appDir, '.next', pageFile),
'utf8'
)
expect(bundleContent).toContain('again')
it('should trigger HMR correctly when NEXT_PUBLIC_ env is changed', async () => {
const envFile = join(appDir, '.env')
const envLocalFile = join(appDir, '.env.local')
const envContent = originalContents.find(
(item) => item.file === '.env'
).content
const envLocalContent = originalContents.find(
(item) => item.file === '.env.local'
).content
try {
const browser = await webdriver(appPort, '/global')
expect(await browser.elementByCss('p').text()).toBe('another')
let outputIdx = output.length
await fs.writeFile(
envFile,
envContent.replace(
`NEXT_PUBLIC_TEST_DEST=another`,
`NEXT_PUBLIC_TEST_DEST=replaced`
)
)
// we should only log we loaded new env from .env
await check(() => output.substring(outputIdx), /Loaded env from/)
expect(
[...output.substring(outputIdx).matchAll(/Loaded env from/g)].length
).toBe(1)
expect(output.substring(outputIdx)).not.toContain('.env.local')
await check(() => browser.elementByCss('p').text(), 'replaced')
outputIdx = output.length
await fs.writeFile(
envLocalFile,
envLocalContent + `\nNEXT_PUBLIC_TEST_DEST=overridden`
)
// we should only log we loaded new env from .env
await check(() => output.substring(outputIdx), /Loaded env from/)
expect(
[...output.substring(outputIdx).matchAll(/Loaded env from/g)].length
).toBe(1)
expect(output.substring(outputIdx)).toContain('.env.local')
await check(() => browser.elementByCss('p').text(), 'overridden')
outputIdx = output.length
await fs.writeFile(envLocalFile, envLocalContent)
// we should only log we loaded new env from .env
await check(() => output.substring(outputIdx), /Loaded env from/)
expect(
[...output.substring(outputIdx).matchAll(/Loaded env from/g)].length
).toBe(1)
expect(output.substring(outputIdx)).toContain('.env.local')
await check(() => browser.elementByCss('p').text(), 'replaced')
} finally {
await fs.writeFile(envFile, envContent)
await fs.writeFile(envLocalFile, envLocalContent)
}
})
})
})
......
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