Skip to content

Commit

Permalink
fix: add GlobalComponents dts (#469)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
BTBMan and antfu committed Jan 25, 2024
1 parent 135f082 commit d40f14b
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/core/ctx.ts
Expand Up @@ -114,6 +114,7 @@ ${dts}`.trim()}\n`
const multilineCommentsRE = /\/\*.*?\*\//gms
const singlelineCommentsRE = /\/\/.*$/gm
const dtsReg = /declare\s+global\s*{(.*?)[\n\r]}/s
const componentCustomPropertiesReg = /interface\s+ComponentCustomProperties\s*{(.*?)[\n\r]}/gs
function parseDTS(dts: string) {
dts = dts
.replace(multilineCommentsRE, '')
Expand All @@ -131,7 +132,7 @@ ${dts}`.trim()}\n`
const dir = dirname(file)
const originalContent = existsSync(file) ? await fs.readFile(file, 'utf-8') : ''
const originalDTS = parseDTS(originalContent)
const currentContent = await unimport.generateTypeDeclarations({
let currentContent = await unimport.generateTypeDeclarations({
resolvePath: (i) => {
if (i.from.startsWith('.') || isAbsolute(i.from)) {
const related = slash(relative(dir, i.from).replace(/\.ts(x)?$/, ''))
Expand All @@ -143,6 +144,12 @@ ${dts}`.trim()}\n`
},
})
const currentDTS = parseDTS(currentContent)!
if (options.vueTemplate) {
currentContent = currentContent.replace(
componentCustomPropertiesReg,
$1 => `interface GlobalComponents {}\n ${$1}`,
)
}
if (originalDTS) {
Object.keys(currentDTS).forEach((key) => {
originalDTS[key] = currentDTS[key]
Expand Down

0 comments on commit d40f14b

Please sign in to comment.