Skip to content

Commit

Permalink
fix: correctly get package info in pnp mode
Browse files Browse the repository at this point in the history
fix #384
  • Loading branch information
qmhc committed Sep 24, 2024
1 parent fa84e9c commit 984f487
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 48 deletions.
23 changes: 23 additions & 0 deletions examples/vue/components/GenericProps.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<script setup lang="ts" generic="T extends { label: string }">
import { computed } from 'vue'
const props = defineProps<{
item: T
}>()
const emit = defineEmits<{
select: [node: T]
}>()
const item = computed(() => props.item)
</script>

<template>
<div
class="setup"
@click="emit('select', item)"
>
{{ item.label }}
</div>
<HelloWorld></HelloWorld>
</template>
4 changes: 3 additions & 1 deletion examples/vue/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import OutsideProps from '@/components/outside-props'
import NoDirectExport from '@/components/NoDirectExport.vue'

import OutsideTsProps from '@/components/outside-ts-props'
import GenericProps from '@/components/GenericProps.vue'

import JsSetup from '../components/JsSetup.vue'
import CssVar from '../components/CssVar.vue'
Expand Down Expand Up @@ -45,7 +46,8 @@ export {
NoDirectExport,
JsSetup,
CssVar,
OutsideTsProps
OutsideTsProps,
GenericProps
}

export default DefaultImport
48 changes: 1 addition & 47 deletions src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const hasVue = !!tryGetPackageInfo('vue')

// If there has no Vue dependency, we think it's a normal TypeScript project.
// So we use the original createProgram of TypeScript.
const _createProgram = !hasVue
export const createProgram = !hasVue
? ts.createProgram
: proxyCreateProgram(ts, ts.createProgram, (ts, options) => {
const { configFilePath } = options.options
Expand All @@ -31,49 +31,3 @@ const _createProgram = !hasVue
)
return [vueLanguagePlugin]
})

export const createProgram = !hasVue
? ts.createProgram
: (options: ts.CreateProgramOptions) => {
const program = _createProgram(options)

const emit = program.emit
program.emit = (
targetSourceFile,
writeFile,
cancellationToken,
emitOnlyDtsFiles,
customTransformers
) => {
if (writeFile) {
return emit(
targetSourceFile,
(fileName, data, writeByteOrderMark, onError, sourceFiles) => {
if (fileName.endsWith('.d.ts')) {
// data = removeVolarGlobalTypes(data)
}
return writeFile(fileName, data, writeByteOrderMark, onError, sourceFiles)
},
cancellationToken,
emitOnlyDtsFiles,
customTransformers
)
}

return emit(
targetSourceFile,
writeFile,
cancellationToken,
emitOnlyDtsFiles,
customTransformers
)
}

return program
}

// const removeVolarGlobalTypesRegexp = /^[^\n]*__VLS_globalTypesStart[\w\W]*__VLS_globalTypesEnd[^\n]*\n?$/mg

// export function removeVolarGlobalTypes(dts: string) {
// return dts.replace(removeVolarGlobalTypesRegexp, '')
// }
11 changes: 11 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
sep
} from 'node:path'
import { existsSync, lstatSync, readdirSync, rmdirSync } from 'node:fs'
import { createRequire } from 'node:module'

import ts from 'typescript'
import { getPackageInfoSync, resolveModule } from 'local-pkg'
Expand Down Expand Up @@ -438,6 +439,16 @@ export function parseTsAliases(basePath: string, paths: ts.MapLike<string[]>) {
}

export function tryGetPackageInfo(name: string) {
if (process.versions.pnp) {
const targetRequire = createRequire(import.meta.url)

try {
return getPackageInfoSync(
targetRequire.resolve(`${name}/package.json`, { paths: [process.cwd()] })
)
} catch (e) {}
}

try {
return (getPackageInfoSync(name) ??
getPackageInfoSync(name, { paths: [resolveModule(name) || process.cwd()] })) as {
Expand Down

0 comments on commit 984f487

Please sign in to comment.