posva / unplugin-vue-router

Next Generation file based typed routing for Vue Router
https://uvr.esm.is
MIT License
1.69k stars 82 forks source link

[Feature Request]: Auto generate dts file dir #515

Open tmg0 opened 1 month ago

tmg0 commented 1 month ago

This is a really awesome package, I have been using it in my vite project with default options, it works great. But there are some options that confuse me.

options.dts support to use a string as a path of target dts file path, I would love to adding support for auto generate dir if does not exist. And maybe we can use fs-extra ensure the path in context ? or impl a ensureDir function manully

(I have created a demo for this

if (dts) {
  const content = generateDTS()
  if (lastDTS !== content) {
    await fse.ensureDir(dirname(dts))
    await fs.writeFile(dts, content, 'utf-8')
    logger.timeLog('writeConfigFiles', 'wrote dts file')
    lastDTS = content
  }
}

If needed, I would be happy to provide a PR for this issue.

Thank you for your consideration.

posva commented 1 month ago

Thanks! I’m glad you like it! It should be simple to add but what folder to you want to write to that doesn’t exist? if implemented, I would prefer to reuse any existing dep

tmg0 commented 1 month ago

Thank you for your reply!

I'm trying to add all dts files to a single folder like .types, and here is my config file:

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueRouter from 'unplugin-vue-router/vite'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vueRouter({ dts: '.types/typed-router.d.ts' }),
    vue()
  ],
})

But when I run the build script, it seems there are some errors: ENOENT: no such file or directory. It seems necessary to manually create the parent directory.

So I'm considering if it's possible to implement ensuring the folder exists in this plugin.

Maybe use native node fs module is a better way? like: await fs.mkdir(dirname(dts), { recursive: true })

posva commented 1 month ago

Using the native module sounds good