samonxian / vite-plugin-build

Vite library mode plugin,which support transfom the whole folder and emit dceclaration files. Vite 库模式插件,支持整个文件夹的转换和声明文件生成。(支持 vanilla、react、vue3 和 svelte)
60 stars 6 forks source link

Add support for adding own created d.ts file in the build #8

Closed mddanish00 closed 1 year ago

mddanish00 commented 1 year ago

emitDeclaration feature is wonderful but sometimes I need to write code in JavaScript and declare type manually using d.ts files. I tried to use ignoreInput and ignore d.ts files. (I used this array instead, ['**\/*.spec.*', '**\/*.test.*'])

But that will also process d.ts. file which I really don't need so, I restore ignoreInput to normal.

I tried to use the Vite feature like copying files from the public folder to the build folder but it will also ignore d.ts from the folder. I tried to use the Vite copy plugin but it is also the same...

Is there any way to add a manually created d.ts file (excluding vite.d.ts I don't know its function) into the build without processing it?

samonxian commented 1 year ago

You want to add custom d.ts files to the build folder?

mddanish00 commented 1 year ago

Yes. It is possible?

samonxian commented 1 year ago

In theory,using copy plugin after build plugin should work.

export default defineConfig(() => {
  return {
    plugins: [buildPlugin(), copyPlugin()],
  };
});
mddanish00 commented 1 year ago

Sorry for the late reply. I tried that before in the exact order. It will not work at all if I put in src. So, I put the file in a different folder. It will come out with another error. I think Path does not exist or something. I think your plugin will probably create lib and es folder, but at the time of the copy, it still does not exist.

samonxian commented 1 year ago

@Miracutor the vite-plugin-static-copy also work, dest is relative to output dir dist, the code below will copy the projectRoot/src/a/b/*.d.ts to the folder projectRoot/lib

   viteStaticCopy({
      targets: [
        {
          src: 'projectRoot/src/a/b/*.d.ts',
          dest: '../lib',
        },
      ],
    }),
mddanish00 commented 1 year ago

I already refactored my project to not use that d.ts, so I no longer needed this. But for the sake of any future dev looking to do this, I tested with @samonxian suggestion with vite-plugin-static-copy. It works with some addition.

viteStaticCopy({
      targets: [
        {
          src: 'projectRoot/src/a/b/*.d.ts',
          dest: '../lib',
          transform: null,
        },
      ],
    }),

Without the transform: null, I get Error: EBUSY: resource busy or locked, copyfile. Maybe because I also enable emitDeclaration? Anyway, this approach work.👍