unplugin / unplugin-auto-import

Auto import APIs on-demand for Vite, Webpack and Rollup
MIT License
3.29k stars 198 forks source link

[Feat Request] Is there any way to generate `auto-import.d.ts` manually? #464

Open floyd-li opened 10 months ago

floyd-li commented 10 months ago

Clear and concise description of the problem

We use unplugin-auto-import in our project and it's really powerful! the question is that when we develop locally it will auto generate auto-import.d.ts file and it cause conflict when merge code, so we add it into .gitignore and it will cause build error in CI system likeJenkins since it can not find the file like #288

Suggested solution

if there's any way to generate auto-import.d.ts file manually?

Alternative

No response

Additional context

No response

Validations

dvlden commented 10 months ago

I am looking into this just now as well. Same thing, for Jenkins pipeline. In some projects, we're using Vite SSG and the export command bound to it actually creates the auto generated files.

Not sure why that's not the case for build, but if you find out how to trigger it, let me know. I'll do the same, if I figure it out before someone else provides a feedback.

Looks like it should first build the output and then check for typescript errors. vite build && vue-tsc --noEmit

For my use-case, I guess it's better to check for type errors in PR, rather than allowing it to pass to the build step.

nathanielobrown commented 8 months ago

I'm also very interested in a way to generate this file manually for the same reasons. vite build does the job, but it's slow and I want a fast method so my type checking CI step can be speedy. Interestingly for https://github.com/unplugin/unplugin-vue-components vite optimize (which is fast) generates components.d.ts, I wonder if this project could be updated to do the same?

I've come up with a workaround for my project. I'd appreciate input from any Vite expert as to whether this is a good idea or not, but I'm instantiating the AutoImport plugin and calling the .buildStart method at module level before calling defineConfig. This has the affect that vite optimize generates auto-imports.d.ts.

I guess it might be better to separate out this hack so it's not run for every single Vite command or tool that uses the vite.config.ts file. Thoughts anyone?

Example:

import { defineConfig } from "vitest/config"
import Vue from "@vitejs/plugin-vue"
import Components from "unplugin-vue-components/vite"
import AutoImport from "unplugin-auto-import/vite"

const autoImportPlugin = AutoImport({
  imports: ["vue", "vue-router"],
  dts: "src/auto-imports.d.ts",
})

// Ensure .d.ts file is created when running `vite optimize`
autoImportPlugin.buildStart()

export default defineConfig({
  plugins: [
    Vue({
      include: [/\.vue$/],
    }),
    autoImportPlugin,
    Components({
      extensions: ["vue"],
      dts: "src/components.d.ts",
      include: [/\.vue$/, /\.vue\?vue/],
    }),
  ],
})
vitjaz commented 6 months ago

Seems like we need to commit this files... antfu comment