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

rename or delete functions, will also exist in DTS #548

Open
kingyang opened this issue Dec 20, 2024 · 3 comments · May be fixed by #549
Open

rename or delete functions, will also exist in DTS #548

kingyang opened this issue Dec 20, 2024 · 3 comments · May be fixed by #549

Comments

@kingyang
Copy link

The intent of the generateDTS function to read history and merge is ambiguous, and the code is very old.

version:
unplugin-auto-import:^0.19.0
vite: ^6.0.4
config:

 AutoImport({
      dirs: [
         resolve(__dirname, './src/utils/**')
      ],
      dts: './src/auto-imports.d.ts',
})

error:

  const uid66: typeof import('./utils/html')['uid66']
  const uid6611: typeof import('./utils/html')['uid6611']
  const uid661122: typeof import('./utils/html')['uid661122']

generateDTS:

async function generateDTS(file: string) {
await importsPromise
const dir = dirname(file)
const originalContent = existsSync(file) ? await fs.readFile(file, 'utf-8') : ''
const originalDTS = parseDTS(originalContent)
let currentContent = await unimport.generateTypeDeclarations({
resolvePath: (i) => {
if (i.from.startsWith('.') || isAbsolute(i.from)) {
const related = slash(relative(dir, i.from).replace(/\.ts(x)?$/, ''))
return !related.startsWith('.')
? `./${related}`
: related
}
return i.from
},
})
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]
})
const dtsList = Object.keys(originalDTS).sort().map(k => ` ${k}: ${originalDTS[k]}`)
return currentContent.replace(dtsReg, () => `declare global {\n${dtsList.join('\n')}\n}`)
}
return currentContent

@noootwo
Copy link
Contributor

noootwo commented Dec 24, 2024

Same as #433 , and the historical reasons in #341 .

@antfu If there is a need for an option to control the DTS generation mode between 'overwrite' and 'additional'.

@antfu
Copy link
Member

antfu commented Dec 24, 2024

If there is a need for an option to control the DTS generation mode between 'overwrite' and 'additional'.

I am up to it! PR welcome :)

@noootwo
Copy link
Contributor

noootwo commented Dec 24, 2024

Ok, I'll make a PR later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants