From 6a849f5ac8c0083a4103c5f0fdd359ced0ab391f Mon Sep 17 00:00:00 2001 From: Sadegh Barati Date: Sun, 10 Nov 2024 21:01:57 +0330 Subject: [PATCH 1/3] refactor: use `get-tsconfig` instead of `tsconfig-paths` update ora, remove unsed semver --- packages/cli/package.json | 5 +- packages/cli/src/utils/get-config.ts | 67 +++++++++++++----------- packages/cli/src/utils/resolve-import.ts | 19 +++---- pnpm-lock.yaml | 53 ++++++------------- 4 files changed, 63 insertions(+), 81 deletions(-) diff --git a/packages/cli/package.json b/packages/cli/package.json index 98b347e21..0bf564bf9 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -57,18 +57,17 @@ "consola": "^3.2.3", "diff": "^7.0.0", "fs-extra": "^11.2.0", + "get-tsconfig": "^4.8.1", "https-proxy-agent": "^7.0.5", "lodash-es": "^4.17.21", "magic-string": "^0.30.12", "nypm": "^0.3.12", "ofetch": "^1.4.1", - "ora": "^8.1.0", + "ora": "^8.1.1", "pathe": "^1.1.2", "pkg-types": "^1.2.1", "prompts": "^2.4.2", "radix-vue": "catalog:", - "semver": "^7.6.3", - "tsconfig-paths": "^4.2.0", "vue-metamorph": "^3.2.0", "zod": "^3.23.8" }, diff --git a/packages/cli/src/utils/get-config.ts b/packages/cli/src/utils/get-config.ts index 7902e7795..a6974ca42 100644 --- a/packages/cli/src/utils/get-config.ts +++ b/packages/cli/src/utils/get-config.ts @@ -1,9 +1,8 @@ -import type { ConfigLoaderResult } from 'tsconfig-paths' -import { existsSync } from 'node:fs' import { resolveImport } from '@/src/utils/resolve-import' import { loadConfig as c12LoadConfig } from 'c12' +import { colors } from 'consola/utils' +import { getTsconfig } from 'get-tsconfig' import path from 'pathe' -import { loadConfig } from 'tsconfig-paths' import { z } from 'zod' export const DEFAULT_STYLE = 'default' @@ -67,52 +66,58 @@ export async function getConfig(cwd: string) { } export async function resolveConfigPaths(cwd: string, config: RawConfig) { - let tsConfig: ConfigLoaderResult | undefined - let tsConfigPath = path.resolve( - cwd, - config.tsConfigPath, - ) - - if (config.typescript) { - // Read tsconfig.json. - tsConfig = loadConfig(tsConfigPath) - // 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 ('paths' in tsConfig && Object.keys(tsConfig.paths).length === 0) { - tsConfigPath = path.resolve(cwd, './tsconfig.app.json') - if (existsSync(tsConfigPath)) - tsConfig = loadConfig(tsConfigPath) - } - } - else { - tsConfigPath = config.tsConfigPath.includes('tsconfig.json') ? path.resolve(cwd, './jsconfig.json') : path.resolve(cwd, config.tsConfigPath) - tsConfig = loadConfig(tsConfigPath) - } - if (tsConfig.resultType === 'failed') { + const tsconfigType = config.typescript ? 'tsconfig.json' : 'jsconfig.json' + const pathAliases = getTSConfig(cwd, tsconfigType) + + if (pathAliases === null) { throw new Error( - `Failed to load ${tsConfigPath}. ${tsConfig.message ?? ''}`.trim(), + `Missing ${colors.cyan('paths')} field in your ${colors.cyan(tsconfigType)} for path aliases. See: ${colors.underline('https//')}`, ) } + const utilsPath = resolveImport(config.aliases.utils, pathAliases) + const componentsPath = resolveImport(config.aliases.components, pathAliases) + const aliasError = (type: string, alias: string) => + new Error( + `Invalid import alias found: (${colors.cyan(`"${type}": "${alias}"`)}) in ${colors.cyan('components.json')}. + - Import aliases ${colors.underline('must use')} existing path aliases defined in your ${colors.cyan(tsconfigType)}.`, + ) + + if (utilsPath === undefined) + throw aliasError('utils', config.aliases.utils) + if (componentsPath === undefined) + throw aliasError('components', config.aliases.components) + return configSchema.parse({ ...config, resolvedPaths: { tailwindConfig: path.resolve(cwd, config.tailwind.config), tailwindCss: path.resolve(cwd, config.tailwind.css), - utils: resolveImport(config.aliases.utils, tsConfig), - components: resolveImport(config.aliases.components, tsConfig), + utils: resolveImport(config.aliases.utils, pathAliases), + components: resolveImport(config.aliases.components, pathAliases), ui: config.aliases.ui - ? resolveImport(config.aliases.ui, tsConfig) - : resolveImport(config.aliases.components, tsConfig), + ? resolveImport(config.aliases.ui, pathAliases) + : resolveImport(config.aliases.components, pathAliases), }, }) } +export function getTSConfig(cwd: string, tsconfigName: 'tsconfig.json' | 'jsconfig.json') { + const parsedConfig = getTsconfig(path.resolve(cwd, 'package.json'), tsconfigName) + if (parsedConfig === null) { + throw new Error( + `Failed to find ${colors.cyan(tsconfigName)}`, + ) + } + + return parsedConfig +} + export async function getRawConfig(cwd: string): Promise { try { const configResult = await c12LoadConfig({ name: 'components', - configFile: 'components.json', + configFile: 'components', cwd, }) diff --git a/packages/cli/src/utils/resolve-import.ts b/packages/cli/src/utils/resolve-import.ts index 76ddd9e93..eb33b9bd8 100644 --- a/packages/cli/src/utils/resolve-import.ts +++ b/packages/cli/src/utils/resolve-import.ts @@ -1,13 +1,10 @@ -import { type ConfigLoaderSuccessResult, createMatchPath } from 'tsconfig-paths' +import { createPathsMatcher, type TsConfigResult } from 'get-tsconfig' -export function resolveImport( - importPath: string, - config: Pick, -) { - return createMatchPath(config.absoluteBaseUrl, config.paths)( - importPath, - undefined, - () => true, - ['.ts', '.tsx', '.vue'], - ) +export function resolveImport(importPath: string, config: TsConfigResult) { + const matcher = createPathsMatcher(config) + if (matcher === null) { + return + } + const paths = matcher(importPath) + return paths[0] } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 65c6f4794..145ff4238 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -31,7 +31,7 @@ importers: devDependencies: '@antfu/eslint-config': specifier: ^3.6.2 - version: 3.8.0(@typescript-eslint/utils@8.10.0(eslint@9.13.0(jiti@2.3.3))(typescript@5.6.3))(@vue/compiler-sfc@3.5.12)(eslint@9.13.0(jiti@2.3.3))(typescript@5.6.3)(vitest@2.1.3(@types/node@22.7.7)(@vitest/ui@2.1.3)(stylus@0.57.0)(terser@5.36.0)) + version: 3.8.0(@typescript-eslint/utils@8.10.0(eslint@9.13.0(jiti@2.3.3))(typescript@5.6.3))(@vue/compiler-sfc@3.5.12)(eslint@9.13.0(jiti@2.3.3))(typescript@5.6.3)(vitest@2.1.3) '@commitlint/cli': specifier: ^19.5.0 version: 19.5.0(@types/node@22.7.7)(typescript@5.6.3) @@ -270,6 +270,9 @@ importers: fs-extra: specifier: ^11.2.0 version: 11.2.0 + get-tsconfig: + specifier: ^4.8.1 + version: 4.8.1 https-proxy-agent: specifier: ^7.0.5 version: 7.0.5 @@ -286,8 +289,8 @@ importers: specifier: ^1.4.1 version: 1.4.1 ora: - specifier: ^8.1.0 - version: 8.1.0 + specifier: ^8.1.1 + version: 8.1.1 pathe: specifier: ^1.1.2 version: 1.1.2 @@ -300,12 +303,6 @@ importers: radix-vue: specifier: 'catalog:' version: 1.9.8(vue@3.5.12(typescript@5.6.3)) - semver: - specifier: ^7.6.3 - version: 7.6.3 - tsconfig-paths: - specifier: ^4.2.0 - version: 4.2.0 vitest: specifier: '*' version: 2.1.3(@types/node@22.7.7)(@vitest/ui@2.1.3)(stylus@0.57.0)(terser@5.36.0) @@ -367,7 +364,7 @@ importers: version: 3.13.2(rollup@4.24.0) '@nuxt/test-utils': specifier: ^3.14.0 - version: 3.14.4(@vitest/ui@2.1.3(vitest@2.1.3))(h3@1.13.0)(magicast@0.3.5)(nitropack@2.9.7(encoding@0.1.13)(magicast@0.3.5))(rollup@4.24.0)(vite@5.4.9(@types/node@20.16.13)(stylus@0.57.0)(terser@5.36.0))(vitest@2.1.3(@types/node@20.16.13)(@vitest/ui@2.1.3)(stylus@0.57.0)(terser@5.36.0))(vue-router@4.4.5(vue@3.5.12(typescript@5.6.3)))(vue@3.5.12(typescript@5.6.3)) + version: 3.14.4(@vitest/ui@2.1.3)(h3@1.13.0)(magicast@0.3.5)(nitropack@2.9.7(encoding@0.1.13)(magicast@0.3.5))(rollup@4.24.0)(vite@5.4.9(@types/node@20.16.13)(stylus@0.57.0)(terser@5.36.0))(vitest@2.1.3)(vue-router@4.4.5(vue@3.5.12(typescript@5.6.3)))(vue@3.5.12(typescript@5.6.3)) '@nuxtjs/color-mode': specifier: ^3.5.1 version: 3.5.1(magicast@0.3.5)(rollup@4.24.0) @@ -5831,8 +5828,8 @@ packages: resolution: {integrity: sha512-iMK1DOQxzzh2MBlVsU42G80mnrvUhqsMh74phHtDlrcTZPK0pH6o7l7DRshK+0YsxDyEuaOkziVdvM3T0QTzpw==} engines: {node: '>=4'} - ora@8.1.0: - resolution: {integrity: sha512-GQEkNkH/GHOhPFXcqZs3IDahXEQcQxsSjEkK4KvEEST4t7eNzoMjxTzef+EZ+JluDEV+Raoi3WQ2CflnRdSVnQ==} + ora@8.1.1: + resolution: {integrity: sha512-YWielGi1XzG1UTvOaCFaNgEnuhZVMSHYkW/FQ7UX8O26PtlpdM84c0f7wLPlkvx2RfiQmnzd61d/MGxmpQeJPw==} engines: {node: '>=18'} os-homedir@1.0.2: @@ -6899,10 +6896,6 @@ packages: resolution: {integrity: sha512-iq6eVVI64nQQTRYq2KtEg2d2uU7LElhTJwsH4YzIHZshxlgZms/wIc4VoDQTlG/IvVIrBKG06CrZnp0qv7hkcQ==} engines: {node: '>=12'} - strip-bom@3.0.0: - resolution: {integrity: sha512-vavAMRXOgBVNF6nyEEmL3DBK19iRpDcoIwW+swQ+CbGiu7lju6t+JklA1MHweoWtadgt4ISVUsXLyDq34ddcwA==} - engines: {node: '>=4'} - strip-eof@1.0.0: resolution: {integrity: sha512-7FCwGGmx8mD5xQd3RPUvnSpUXHM3BWuzjtpD4TXsfcZ9EL4azvVVUscFYwD9nx8Kh+uCBC00XBtAykoMHwTh8Q==} engines: {node: '>=0.10.0'} @@ -7188,10 +7181,6 @@ packages: typescript: optional: true - tsconfig-paths@4.2.0: - resolution: {integrity: sha512-NoZ4roiN7LnbKn9QqE1amc9DJfzvZXxF4xDavcOWt1BPkdx+m+0gJuPM+S0vCe7zTJMYUP0R8pO2XMr+Y8oLIg==} - engines: {node: '>=6'} - tslib@1.14.1: resolution: {integrity: sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==} @@ -8015,7 +8004,7 @@ snapshots: '@jridgewell/gen-mapping': 0.3.5 '@jridgewell/trace-mapping': 0.3.25 - '@antfu/eslint-config@3.8.0(@typescript-eslint/utils@8.10.0(eslint@9.13.0(jiti@2.3.3))(typescript@5.6.3))(@vue/compiler-sfc@3.5.12)(eslint@9.13.0(jiti@2.3.3))(typescript@5.6.3)(vitest@2.1.3(@types/node@22.7.7)(@vitest/ui@2.1.3)(stylus@0.57.0)(terser@5.36.0))': + '@antfu/eslint-config@3.8.0(@typescript-eslint/utils@8.10.0(eslint@9.13.0(jiti@2.3.3))(typescript@5.6.3))(@vue/compiler-sfc@3.5.12)(eslint@9.13.0(jiti@2.3.3))(typescript@5.6.3)(vitest@2.1.3)': dependencies: '@antfu/install-pkg': 0.4.1 '@clack/prompts': 0.7.0 @@ -8024,7 +8013,7 @@ snapshots: '@stylistic/eslint-plugin': 2.9.0(eslint@9.13.0(jiti@2.3.3))(typescript@5.6.3) '@typescript-eslint/eslint-plugin': 8.10.0(@typescript-eslint/parser@8.10.0(eslint@9.13.0(jiti@2.3.3))(typescript@5.6.3))(eslint@9.13.0(jiti@2.3.3))(typescript@5.6.3) '@typescript-eslint/parser': 8.10.0(eslint@9.13.0(jiti@2.3.3))(typescript@5.6.3) - '@vitest/eslint-plugin': 1.1.7(@typescript-eslint/utils@8.10.0(eslint@9.13.0(jiti@2.3.3))(typescript@5.6.3))(eslint@9.13.0(jiti@2.3.3))(typescript@5.6.3)(vitest@2.1.3(@types/node@22.7.7)(@vitest/ui@2.1.3)(stylus@0.57.0)(terser@5.36.0)) + '@vitest/eslint-plugin': 1.1.7(@typescript-eslint/utils@8.10.0(eslint@9.13.0(jiti@2.3.3))(typescript@5.6.3))(eslint@9.13.0(jiti@2.3.3))(typescript@5.6.3)(vitest@2.1.3) eslint: 9.13.0(jiti@2.3.3) eslint-config-flat-gitignore: 0.3.0(eslint@9.13.0(jiti@2.3.3)) eslint-flat-config-utils: 0.4.0 @@ -9348,7 +9337,7 @@ snapshots: - supports-color - webpack-sources - '@nuxt/test-utils@3.14.4(@vitest/ui@2.1.3(vitest@2.1.3))(h3@1.13.0)(magicast@0.3.5)(nitropack@2.9.7(encoding@0.1.13)(magicast@0.3.5))(rollup@4.24.0)(vite@5.4.9(@types/node@20.16.13)(stylus@0.57.0)(terser@5.36.0))(vitest@2.1.3(@types/node@20.16.13)(@vitest/ui@2.1.3)(stylus@0.57.0)(terser@5.36.0))(vue-router@4.4.5(vue@3.5.12(typescript@5.6.3)))(vue@3.5.12(typescript@5.6.3))': + '@nuxt/test-utils@3.14.4(@vitest/ui@2.1.3)(h3@1.13.0)(magicast@0.3.5)(nitropack@2.9.7(encoding@0.1.13)(magicast@0.3.5))(rollup@4.24.0)(vite@5.4.9(@types/node@20.16.13)(stylus@0.57.0)(terser@5.36.0))(vitest@2.1.3)(vue-router@4.4.5(vue@3.5.12(typescript@5.6.3)))(vue@3.5.12(typescript@5.6.3))': dependencies: '@nuxt/kit': 3.13.2(magicast@0.3.5)(rollup@4.24.0) '@nuxt/schema': 3.13.2(rollup@4.24.0) @@ -9375,7 +9364,7 @@ snapshots: unenv: 1.10.0 unplugin: 1.14.1 vite: 5.4.9(@types/node@20.16.13)(stylus@0.57.0)(terser@5.36.0) - vitest-environment-nuxt: 1.0.1(@vitest/ui@2.1.3(vitest@2.1.3))(h3@1.13.0)(magicast@0.3.5)(nitropack@2.9.7(encoding@0.1.13)(magicast@0.3.5))(rollup@4.24.0)(vite@5.4.9(@types/node@20.16.13)(stylus@0.57.0)(terser@5.36.0))(vitest@2.1.3(@types/node@20.16.13)(@vitest/ui@2.1.3)(stylus@0.57.0)(terser@5.36.0))(vue-router@4.4.5(vue@3.5.12(typescript@5.6.3)))(vue@3.5.12(typescript@5.6.3)) + vitest-environment-nuxt: 1.0.1(@vitest/ui@2.1.3)(h3@1.13.0)(magicast@0.3.5)(nitropack@2.9.7(encoding@0.1.13)(magicast@0.3.5))(rollup@4.24.0)(vite@5.4.9(@types/node@20.16.13)(stylus@0.57.0)(terser@5.36.0))(vitest@2.1.3)(vue-router@4.4.5(vue@3.5.12(typescript@5.6.3)))(vue@3.5.12(typescript@5.6.3)) vue: 3.5.12(typescript@5.6.3) vue-router: 4.4.5(vue@3.5.12(typescript@5.6.3)) optionalDependencies: @@ -10308,7 +10297,7 @@ snapshots: vite: 5.4.9(@types/node@22.7.7)(stylus@0.57.0)(terser@5.36.0) vue: 3.5.12(typescript@5.6.3) - '@vitest/eslint-plugin@1.1.7(@typescript-eslint/utils@8.10.0(eslint@9.13.0(jiti@2.3.3))(typescript@5.6.3))(eslint@9.13.0(jiti@2.3.3))(typescript@5.6.3)(vitest@2.1.3(@types/node@22.7.7)(@vitest/ui@2.1.3)(stylus@0.57.0)(terser@5.36.0))': + '@vitest/eslint-plugin@1.1.7(@typescript-eslint/utils@8.10.0(eslint@9.13.0(jiti@2.3.3))(typescript@5.6.3))(eslint@9.13.0(jiti@2.3.3))(typescript@5.6.3)(vitest@2.1.3)': dependencies: '@typescript-eslint/utils': 8.10.0(eslint@9.13.0(jiti@2.3.3))(typescript@5.6.3) eslint: 9.13.0(jiti@2.3.3) @@ -14389,7 +14378,7 @@ snapshots: cli-spinners: 1.3.1 log-symbols: 2.2.0 - ora@8.1.0: + ora@8.1.1: dependencies: chalk: 5.3.0 cli-cursor: 5.0.0 @@ -15470,8 +15459,6 @@ snapshots: dependencies: ansi-regex: 6.1.0 - strip-bom@3.0.0: {} - strip-eof@1.0.0: {} strip-final-newline@2.0.0: {} @@ -15779,12 +15766,6 @@ snapshots: optionalDependencies: typescript: 5.6.3 - tsconfig-paths@4.2.0: - dependencies: - json5: 2.2.3 - minimist: 1.2.8 - strip-bom: 3.0.0 - tslib@1.14.1: {} tslib@2.8.0: {} @@ -16341,9 +16322,9 @@ snapshots: - typescript - universal-cookie - vitest-environment-nuxt@1.0.1(@vitest/ui@2.1.3(vitest@2.1.3))(h3@1.13.0)(magicast@0.3.5)(nitropack@2.9.7(encoding@0.1.13)(magicast@0.3.5))(rollup@4.24.0)(vite@5.4.9(@types/node@20.16.13)(stylus@0.57.0)(terser@5.36.0))(vitest@2.1.3(@types/node@20.16.13)(@vitest/ui@2.1.3)(stylus@0.57.0)(terser@5.36.0))(vue-router@4.4.5(vue@3.5.12(typescript@5.6.3)))(vue@3.5.12(typescript@5.6.3)): + vitest-environment-nuxt@1.0.1(@vitest/ui@2.1.3)(h3@1.13.0)(magicast@0.3.5)(nitropack@2.9.7(encoding@0.1.13)(magicast@0.3.5))(rollup@4.24.0)(vite@5.4.9(@types/node@20.16.13)(stylus@0.57.0)(terser@5.36.0))(vitest@2.1.3)(vue-router@4.4.5(vue@3.5.12(typescript@5.6.3)))(vue@3.5.12(typescript@5.6.3)): dependencies: - '@nuxt/test-utils': 3.14.4(@vitest/ui@2.1.3(vitest@2.1.3))(h3@1.13.0)(magicast@0.3.5)(nitropack@2.9.7(encoding@0.1.13)(magicast@0.3.5))(rollup@4.24.0)(vite@5.4.9(@types/node@20.16.13)(stylus@0.57.0)(terser@5.36.0))(vitest@2.1.3(@types/node@20.16.13)(@vitest/ui@2.1.3)(stylus@0.57.0)(terser@5.36.0))(vue-router@4.4.5(vue@3.5.12(typescript@5.6.3)))(vue@3.5.12(typescript@5.6.3)) + '@nuxt/test-utils': 3.14.4(@vitest/ui@2.1.3)(h3@1.13.0)(magicast@0.3.5)(nitropack@2.9.7(encoding@0.1.13)(magicast@0.3.5))(rollup@4.24.0)(vite@5.4.9(@types/node@20.16.13)(stylus@0.57.0)(terser@5.36.0))(vitest@2.1.3)(vue-router@4.4.5(vue@3.5.12(typescript@5.6.3)))(vue@3.5.12(typescript@5.6.3)) transitivePeerDependencies: - '@cucumber/cucumber' - '@jest/globals' From 7a13dd73aaff78f5b4d179ff1ecec5a9cb7b852e Mon Sep 17 00:00:00 2001 From: Sadegh Barati Date: Sun, 10 Nov 2024 22:08:15 +0330 Subject: [PATCH 2/3] refactor: use `.nuxt/tsconfig.json` initial path even for js projects --- packages/cli/src/commands/init.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/src/commands/init.ts b/packages/cli/src/commands/init.ts index 96e0f0284..5204a7f14 100644 --- a/packages/cli/src/commands/init.ts +++ b/packages/cli/src/commands/init.ts @@ -139,7 +139,7 @@ export async function promptForConfig( message: (prev, values) => `Where is your ${highlight(values.typescript ? 'tsconfig.json' : 'jsconfig.json')} file?`, initial: (prev, values) => { const prefix = values.framework === 'nuxt' ? '.nuxt/' : './' - const path = values.typescript ? 'tsconfig.json' : 'jsconfig.json' + const path = values.framework === 'nuxt' ? 'tsconfig.json' : values.typescript ? 'tsconfig.json' : 'jsconfig.json' return prefix + path }, }, From 1b5301895141ad667a9a25bdb2909d5ce64cd1d5 Mon Sep 17 00:00:00 2001 From: Sadegh Barati Date: Thu, 14 Nov 2024 22:27:17 +0330 Subject: [PATCH 3/3] refactor: simplify initial prompt message for tsconfig path --- packages/cli/src/commands/init.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/cli/src/commands/init.ts b/packages/cli/src/commands/init.ts index 5204a7f14..d48a28876 100644 --- a/packages/cli/src/commands/init.ts +++ b/packages/cli/src/commands/init.ts @@ -138,9 +138,8 @@ export async function promptForConfig( name: 'tsConfigPath', message: (prev, values) => `Where is your ${highlight(values.typescript ? 'tsconfig.json' : 'jsconfig.json')} file?`, initial: (prev, values) => { - const prefix = values.framework === 'nuxt' ? '.nuxt/' : './' - const path = values.framework === 'nuxt' ? 'tsconfig.json' : values.typescript ? 'tsconfig.json' : 'jsconfig.json' - return prefix + path + const path = values.typescript ? './tsconfig.json' : './jsconfig.json' + return path }, }, {