Skip to content

Commit

Permalink
fix(app-vite): make #q-app TS aliases work on npm/yarn/bun workspaces (
Browse files Browse the repository at this point in the history
…fix: quasarframework#17648)

it also improves handling for any other aliases to npm packages
  • Loading branch information
yusufkandemir committed Nov 19, 2024
1 parent f916273 commit 29c2dcf
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions app-vite/lib/types-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ import { isAbsolute, join, relative } from 'node:path'
import { statSync } from 'node:fs'
import fse from 'fs-extra'

import { cliPkg } from './utils/cli-runtime.js'
import { cliPkg, resolveToCliDir } from './utils/cli-runtime.js'
import { isModeInstalled } from './modes/modes-utils.js'
import { getPackagePath } from './utils/get-package-path.js'

const qAppPaths = (() => {
const exportsRE = /^\./
const dTsRE = /\.d\.ts$/

const { name: cliPackageName } = cliPkg
const localMap = {}

for (const key in cliPkg.exports) {
const localMapKey = key.replace(exportsRE, '#q-app')
const value = cliPkg.exports[ key ]
if (Object(value) === value) {
if (value.types) {
localMap[ localMapKey ] = value.types.replace(exportsRE, cliPackageName + '/')
localMap[ localMapKey ] = resolveToCliDir(value.types)
}
}
else if (typeof value === 'string') {
if (dTsRE.test(value)) {
localMap[ localMapKey ] = value.replace(exportsRE, cliPackageName + '/')
localMap[ localMapKey ] = resolveToCliDir(value)
}
}
}
Expand Down Expand Up @@ -65,11 +65,16 @@ export function generateTypes (quasarConf) {
function generateTsConfig (quasarConf, fsUtils) {
const { appPaths, mode } = quasarConf.ctx

/** Returns the path relative to the tsconfig.json file, in POSIX format */
const toTsPath = _path => {
const relativePath = relative(
fsUtils.tsConfigDir,
isAbsolute(_path) === false ? join('node_modules', _path) : _path
).replaceAll('\\', '/')
// Folder aliases are defined as absolute paths.
// So, the rest, e.g. `'some-pkg': 'another-pkg'`, is not absolute and must be resolved as a package.
const path = isAbsolute(_path) === false
// Try to resolve the package path first, it's crucial to some monorepo setups like npm/yarn/bun workspaces
? (getPackagePath(_path, appPaths.appDir) ?? join('node_modules', _path))
: _path

const relativePath = relative(fsUtils.tsConfigDir, path).replaceAll('\\', '/')

if (relativePath.length === 0) return '.'
if (relativePath.startsWith('./') === false) return ('./' + relativePath)
Expand Down Expand Up @@ -150,8 +155,8 @@ function generateTsConfig (quasarConf, fsUtils) {
paths
},
exclude: [
'./../dist',
'./*/*.js',
'./../dist',
'./../node_modules',
'./../src-capacitor',
'./../src-cordova',
Expand Down

0 comments on commit 29c2dcf

Please sign in to comment.