unplugin-auto-import icon indicating copy to clipboard operation
unplugin-auto-import copied to clipboard

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

Open floyd-li opened this issue 1 year ago • 3 comments
trafficstars

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

  • [X] Follow our Code of Conduct
  • [X] Read the Contributing Guide.
  • [X] Check that there isn't already an issue that request the same feature to avoid creating a duplicate.

floyd-li avatar Dec 28 '23 07:12 floyd-li

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.

nonwip avatar Jan 04 '24 09:01 nonwip

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/],
    }),
  ],
})

nathanielobrown avatar Mar 15 '24 17:03 nathanielobrown

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

vitjaz avatar May 13 '24 21:05 vitjaz