Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add GlobalComponents dts #469

Merged
merged 6 commits into from
Jan 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/core/ctx.ts
Original file line number Diff line number Diff line change
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