Skip to content

Commit

Permalink
load only meta information of tsconfig for validation
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Apr 16, 2021
1 parent 9da7eba commit 5e267c8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ export default async function getBaseWebpackConfig(
// jsconfig is a subset of tsconfig
if (useTypeScript) {
const ts = (await import(typeScriptPath!)) as typeof import('typescript')
const tsConfig = await getTypeScriptConfiguration(ts, tsConfigPath)
const tsConfig = await getTypeScriptConfiguration(ts, tsConfigPath, true)
jsConfig = { compilerOptions: tsConfig.options }
}

Expand Down Expand Up @@ -1480,7 +1480,7 @@ export default async function getBaseWebpackConfig(
)
}
} else if (!config.future.strictPostcssConfiguration) {
await __overrideCssConfiguration(dir, !dev, webpackConfig)
__overrideCssConfiguration(dir, !dev, webpackConfig)
}

// Inject missing React Refresh loaders so that development mode is fast:
Expand Down
19 changes: 17 additions & 2 deletions packages/next/lib/typescript/getTypeScriptConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { FatalTypeScriptError } from './FatalTypeScriptError'

export async function getTypeScriptConfiguration(
ts: typeof import('typescript'),
tsConfigPath: string
tsConfigPath: string,
metaOnly?: boolean
): Promise<import('typescript').ParsedCommandLine> {
try {
const formatDiagnosticsHost: import('typescript').FormatDiagnosticsHost = {
Expand All @@ -21,8 +22,18 @@ export async function getTypeScriptConfiguration(
)
}

let configToParse: any = config
if (metaOnly) {
configToParse = {
...configToParse,
include: [],
exclude: [],
files: undefined,
}
}

const result = ts.parseJsonConfigFileContent(
config,
configToParse,
ts.sys,
path.dirname(tsConfigPath)
)
Expand All @@ -41,6 +52,10 @@ export async function getTypeScriptConfiguration(
)
}

if (metaOnly) {
result.raw = config
}

return result
} catch (err) {
if (err?.name === 'SyntaxError') {
Expand Down
2 changes: 1 addition & 1 deletion packages/next/lib/typescript/writeConfigurationDefaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export async function writeConfigurationDefaults(
const {
options: tsOptions,
raw: rawConfig,
} = await getTypeScriptConfiguration(ts, tsConfigPath)
} = await getTypeScriptConfiguration(ts, tsConfigPath, true)

const userTsConfigContent = await fs.readFile(tsConfigPath, {
encoding: 'utf8',
Expand Down

0 comments on commit 5e267c8

Please sign in to comment.