vitejs / vite

Next generation frontend tooling. It's fast!
http://vite.dev
MIT License
68k stars 6.12k forks source link

Difficulty in generating multiple different codes from single .vue/.md file #1513

Closed sunziping2016 closed 3 years ago

sunziping2016 commented 3 years ago

Is your feature request related to a problem? Please describe. I am now implementing a blog generator with Vite, which is similar to VitePress. I want to generate two Vue components from single Markdown file. One is the excerpt, the other is the full content. There may exist two solutions for that:

  1. Two ids with different base filename: the problem is that one of the id will lose hot updates, as the filename is a virtual one. If I could set the file dependencies for a virtual code, it would be solved.
  2. Two ids with same filename but different query strings: the problem is that @vitejs/plugin-vue seems not well prepared for this situation. I did some dirty hacks (e.g. replacing _sfc_main.__hmrId), and I am still trying. If @vitejs/plugin-vue can generate different __hmrId and handleHotUpdate can deal with multiple mainModule and descriptorCache can store muliple cache, it would be fine.

Describe the solution you'd like The two bold text in the above description is the solution I'd like.

Describe alternatives you've considered I am tring to do the dirty hacks. And I will really be pleased if there already exists some solution that I've missed. Sorry for bother you.

yyx990803 commented 3 years ago

This is too vague without actual code. If you can provide a minimal demo that will help.

sunziping2016 commented 3 years ago

In markdown.ts, I try to return different code based on query string. Thus, the exportExcerpt and the exportContent may refer to the same file, which will cause @vitejs/plugin-vue treat them as one item. To overcome this, I wrapped @vitejs/plugin-vue in vueWrapper.ts and change the id passed to the inner @vitejs/plugin-vue. But I think this solution may be too dirty. Is there any better solution?

yyx990803 commented 3 years ago

Hmm, this is like virtual module inception... I think the only way is to use actually different file names. e.g. instead of foo.md?pageData, use foo.pageData.md instead. This is essentially what you are doing in the wrapper, but if you generate these filenames directly and parse for them instead of queries in your markdown loader, you can avoid hacking the vue plugin.

sunziping2016 commented 3 years ago

Hmm, this is like virtual module inception... I think the only way is to use actually different file names. e.g. instead of foo.md?pageData, use foo.pageData.md instead. This is essentially what you are doing in the wrapper, but if you generate these filenames directly and parse for them instead of queries in your markdown loader, you can avoid hacking the vue plugin.

So is it possible to hot reload a virtual module when the file changed? It seems moduleGraph.ts doesn't provide API for doing this.

yyx990803 commented 3 years ago

Oh yeah, using different filenames means they won't be linked to the original file for HMR. I guess there's no clean way around this then.