Unverified Commit 21f4c9f4 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Development: Move all TypeScript related work in watcher together (#83912)

## What?

Work in progress. Checking if tests fail.

Moving these `.d.ts` file writes and TS checks together so that they can
be moved to not block in a follow-up PR.
parent 4d047b28
...@@ -406,7 +406,7 @@ export default abstract class Server< ...@@ -406,7 +406,7 @@ export default abstract class Server<
protected abstract loadEnvConfig(params: { protected abstract loadEnvConfig(params: {
dev: boolean dev: boolean
forceReload?: boolean forceReload: boolean
}): void }): void
// TODO-APP: (wyattjoh): Make protected again. Used for turbopack in route-resolver.ts right now. // TODO-APP: (wyattjoh): Make protected again. Used for turbopack in route-resolver.ts right now.
...@@ -448,7 +448,7 @@ export default abstract class Server< ...@@ -448,7 +448,7 @@ export default abstract class Server<
this.dir = path.resolve(/* turbopackIgnore: true */ dir) this.dir = path.resolve(/* turbopackIgnore: true */ dir)
this.quiet = quiet this.quiet = quiet
this.loadEnvConfig({ dev }) this.loadEnvConfig({ dev, forceReload: false })
// TODO: should conf be normalized to prevent missing // TODO: should conf be normalized to prevent missing
// values from causing issues as this can be user provided // values from causing issues as this can be user provided
......
...@@ -40,8 +40,7 @@ export {}` ...@@ -40,8 +40,7 @@ export {}`
try { try {
// we expect the types directory to already exist // we expect the types directory to already exist
const envDtsPath = join(distDir, 'types', 'env.d.ts') const envDtsPath = join(distDir, 'types', 'env.d.ts')
// do not await, this is not essential for further process await writeFile(envDtsPath, definitionStr, 'utf-8')
writeFile(envDtsPath, definitionStr, 'utf-8')
} catch (e) { } catch (e) {
console.error('Failed to write env.d.ts:', e) console.error('Failed to write env.d.ts:', e)
} }
......
...@@ -353,6 +353,7 @@ async function startWatcher( ...@@ -353,6 +353,7 @@ async function startWatcher(
const validatorFilePath = path.join(distDir, 'types', 'validator.ts') const validatorFilePath = path.join(distDir, 'types', 'validator.ts')
wp.on('aggregated', async () => { wp.on('aggregated', async () => {
let writeEnvDefinitions = false
let typescriptStatusFromLastAggregation = enabledTypeScript let typescriptStatusFromLastAggregation = enabledTypeScript
let middlewareMatchers: MiddlewareMatcher[] | undefined let middlewareMatchers: MiddlewareMatcher[] | undefined
const routedPages: string[] = [] const routedPages: string[] = []
...@@ -726,70 +727,14 @@ async function startWatcher( ...@@ -726,70 +727,14 @@ async function startWatcher(
} }
} }
// Using === false to make the check clearer.
if (typescriptStatusFromLastAggregation === false && enabledTypeScript) {
// we tolerate the error here as this is best effort
// and the manual install command will be shown
await verifyTypeScript(opts)
.then(() => {
tsconfigChange = true
})
.catch(() => {})
}
if (envChange || tsconfigChange) { if (envChange || tsconfigChange) {
if (envChange) { if (envChange) {
const loadEnvConfig = ( writeEnvDefinitions = true
require('@next/env') as typeof import('@next/env')
).loadEnvConfig
const { loadedEnvFiles } = loadEnvConfig(
dir,
process.env.NODE_ENV === 'development',
Log,
true,
(envFilePath) => {
Log.info(`Reload env: ${envFilePath}`)
}
)
if (enabledTypeScript && nextConfig.experimental?.typedEnv) {
// do not await, this is not essential for further process
createEnvDefinitions({
distDir,
loadedEnvFiles: [
...loadedEnvFiles,
{
path: nextConfig.configFileName,
env: nextConfig.env,
contents: '',
},
],
})
}
await propagateServerField(opts, 'loadEnvConfig', [ await propagateServerField(opts, 'loadEnvConfig', [
{ dev: true, forceReload: true, silent: true }, { dev: true, forceReload: true },
]) ])
} }
let tsconfigResult:
| UnwrapPromise<
ReturnType<typeof import('../../../build/load-jsconfig').default>
>
| undefined
// This is not relevant for Turbopack because tsconfig/jsconfig is handled internally.
if (!hotReloader.turbopackProject) {
if (tsconfigChange) {
try {
const loadJsConfig = (
require('../../../build/load-jsconfig') as typeof import('../../../build/load-jsconfig')
).default
tsconfigResult = await loadJsConfig(dir, nextConfig)
} catch (_) {
/* do we want to log if there are syntax errors in tsconfig while editing? */
}
}
}
if (hotReloader.turbopackProject) { if (hotReloader.turbopackProject) {
const hasRewrites = const hasRewrites =
...@@ -819,6 +764,24 @@ async function startWatcher( ...@@ -819,6 +764,24 @@ async function startWatcher(
rootPath, rootPath,
projectPath: normalizePath(path.relative(rootPath, dir)), projectPath: normalizePath(path.relative(rootPath, dir)),
}) })
} else {
let tsconfigResult:
| UnwrapPromise<
ReturnType<
typeof import('../../../build/load-jsconfig').default
>
>
| undefined
// This is not relevant for Turbopack because tsconfig/jsconfig is handled internally.
if (tsconfigChange) {
try {
const loadJsConfig = (
require('../../../build/load-jsconfig') as typeof import('../../../build/load-jsconfig')
).default
tsconfigResult = await loadJsConfig(dir, nextConfig)
} catch (_) {
/* do we want to log if there are syntax errors in tsconfig while editing? */
}
} }
hotReloader.activeWebpackConfigs?.forEach((config, idx) => { hotReloader.activeWebpackConfigs?.forEach((config, idx) => {
...@@ -843,7 +806,8 @@ async function startWatcher( ...@@ -843,7 +806,8 @@ async function startWatcher(
if (resolvedBaseUrl) { if (resolvedBaseUrl) {
if ( if (
resolvedBaseUrl.baseUrl !== currentResolvedBaseUrl?.baseUrl resolvedBaseUrl.baseUrl !==
currentResolvedBaseUrl?.baseUrl
) { ) {
// remove old baseUrl and add new one // remove old baseUrl and add new one
if (resolvedUrlIndex && resolvedUrlIndex > -1) { if (resolvedUrlIndex && resolvedUrlIndex > -1) {
...@@ -905,6 +869,7 @@ async function startWatcher( ...@@ -905,6 +869,7 @@ async function startWatcher(
}) })
} }
}) })
}
await hotReloader.invalidate({ await hotReloader.invalidate({
reloadAfterInvalidation: envChange, reloadAfterInvalidation: envChange,
}) })
...@@ -1091,6 +1056,43 @@ async function startWatcher( ...@@ -1091,6 +1056,43 @@ async function startWatcher(
prevSortedRoutes = sortedRoutes prevSortedRoutes = sortedRoutes
if (enabledTypeScript) { if (enabledTypeScript) {
// Using === false to make the check clearer.
if (typescriptStatusFromLastAggregation === false) {
// we tolerate the error here as this is best effort
// and the manual install command will be shown
await verifyTypeScript(opts)
.then(() => {
tsconfigChange = true
})
.catch(() => {})
}
if (writeEnvDefinitions && nextConfig.experimental?.typedEnv) {
// TODO: The call to propagateServerField 'loadEnvConfig' causes the env to be loaded twice on env file changes.
const loadEnvConfig = (
require('@next/env') as typeof import('@next/env')
).loadEnvConfig
const { loadedEnvFiles } = loadEnvConfig(
dir,
process.env.NODE_ENV === 'development',
// Silent as it's the second time `loadEnvConfig` is called in this pass.
undefined,
true
)
await createEnvDefinitions({
distDir,
loadedEnvFiles: [
...loadedEnvFiles,
{
path: nextConfig.configFileName,
env: nextConfig.env,
contents: '',
},
],
})
}
const routeTypesManifest = await createRouteTypesManifest({ const routeTypesManifest = await createRouteTypesManifest({
dir, dir,
pageRoutes, pageRoutes,
......
...@@ -434,17 +434,20 @@ export default class NextNodeServer extends BaseServer< ...@@ -434,17 +434,20 @@ export default class NextNodeServer extends BaseServer<
protected loadEnvConfig({ protected loadEnvConfig({
dev, dev,
forceReload, forceReload,
silent,
}: { }: {
dev: boolean dev: boolean
forceReload?: boolean forceReload: boolean
silent?: boolean
}) { }) {
loadEnvConfig( loadEnvConfig(
this.dir, this.dir,
dev, dev,
silent ? { info: () => {}, error: () => {} } : Log, Log,
forceReload,
forceReload forceReload
? (envFilePath) => {
Log.info(`Reload env: ${envFilePath}`)
}
: undefined
) )
} }
......
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