fix(cli): shouldn't load tsconfig when typescript disabled (#43)

This commit is contained in:
Dunqing 2023-09-11 13:45:39 +08:00 committed by GitHub
parent 686ac00cab
commit a4dc8ab971
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
import path from 'node:path' import path from 'node:path'
import { existsSync } from 'node:fs' import { existsSync } from 'node:fs'
import { cosmiconfig } from 'cosmiconfig' import { cosmiconfig } from 'cosmiconfig'
import type { ConfigLoaderResult } from 'tsconfig-paths'
import { loadConfig } from 'tsconfig-paths' import { loadConfig } from 'tsconfig-paths'
import * as z from 'zod' import * as z from 'zod'
import { resolveImport } from '@/src/utils/resolve-import' import { resolveImport } from '@/src/utils/resolve-import'
@ -62,24 +63,27 @@ export async function getConfig(cwd: string) {
} }
export async function resolveConfigPaths(cwd: string, config: RawConfig) { export async function resolveConfigPaths(cwd: string, config: RawConfig) {
const TSCONFIG_PATH = config.framework === 'nuxt' ? '.nuxt/tsconfig.json' : './tsconfig.json' let tsConfig: ConfigLoaderResult | undefined
if (config.typescript) {
const TSCONFIG_PATH = config.framework === 'nuxt' ? '.nuxt/tsconfig.json' : './tsconfig.json'
// Read tsconfig.json. // Read tsconfig.json.
const tsconfigPath = path.resolve(cwd, TSCONFIG_PATH) const tsconfigPath = path.resolve(cwd, TSCONFIG_PATH)
let tsConfig = loadConfig(tsconfigPath) tsConfig = loadConfig(tsconfigPath)
// In new Vue project, tsconfig has references to tsconfig.app.json, which is causing the path not resolving correctly // In new Vue project, tsconfig has references to tsconfig.app.json, which is causing the path not resolving correctly
// If no paths were found, try to load tsconfig.app.json. // If no paths were found, try to load tsconfig.app.json.
if ('paths' in tsConfig && Object.keys(tsConfig.paths).length === 0) { if ('paths' in tsConfig && Object.keys(tsConfig.paths).length === 0) {
const FALLBACK_TSCONFIG_PATH = path.resolve(cwd, './tsconfig.app.json') const FALLBACK_TSCONFIG_PATH = path.resolve(cwd, './tsconfig.app.json')
if (existsSync(FALLBACK_TSCONFIG_PATH)) if (existsSync(FALLBACK_TSCONFIG_PATH))
tsConfig = loadConfig(FALLBACK_TSCONFIG_PATH) tsConfig = loadConfig(FALLBACK_TSCONFIG_PATH)
} }
if (tsConfig.resultType === 'failed') { if (tsConfig.resultType === 'failed') {
throw new Error( throw new Error(
`Failed to load tsconfig.json. ${tsConfig.message ?? ''}`.trim(), `Failed to load tsconfig.json. ${tsConfig.message ?? ''}`.trim(),
) )
}
} }
return configSchema.parse({ return configSchema.parse({
@ -87,8 +91,8 @@ export async function resolveConfigPaths(cwd: string, config: RawConfig) {
resolvedPaths: { resolvedPaths: {
tailwindConfig: path.resolve(cwd, config.tailwind.config), tailwindConfig: path.resolve(cwd, config.tailwind.config),
tailwindCss: path.resolve(cwd, config.tailwind.css), tailwindCss: path.resolve(cwd, config.tailwind.css),
utils: await resolveImport(config.aliases.utils, tsConfig), utils: tsConfig ? await resolveImport(config.aliases.utils, tsConfig) : config.aliases.utils,
components: await resolveImport(config.aliases.components, tsConfig), components: tsConfig ? await resolveImport(config.aliases.components, tsConfig) : config.aliases.components,
}, },
}) })
} }