-
Notifications
You must be signed in to change notification settings - Fork 46
feat: build dev server bundle using vite #201
Conversation
@pi0 I have made some updates, and finally got it running (on the first render)! I have tried to make each commit small and self-explainable, you can read them to catch up. But I also found a more critical problem which I have listed in the
AFAIK, there is no clear way to clean the cache for ESM (or do we with
The ESM externals really tricky, and I didn't find a quick solution for that. For now, could we just use
Vite has a built-in module graph with proper cache and invalidation implemented already. So I guess we don't need to do anything about that. For invalidation: const mod = server.moduleGraph.getModuleById(id)
mod && server.moduleGraph.invalidateModule(mod)
Is that possible for us to have the same alias for both mode? If you really need that, I guess we could write an inline plugin for it (but that could possibly burst the cache I guess) |
Thanks for helping on this @antfu <3 Regarding the cached version of We have 4 possible methods for clearing
Indeed that is tricky. Since for making nuxt plugins or ssr/client only logic at least, we use |
For the dependency caching, I guess it will be more straightforward to use |
@antfu Jiti uses shared CJS cache which nuxt should normally clear (if not, see #201 (comment)) but is probably adding huge perf impact of babel... :( I wish we can try mlly or bundling approaches to see the best. |
I tried 27f6545 but the is still there. Any idea on that? For the perf, maybe we could have a mode in For |
Seems a good idea to try for jiti (~> unjs/jiti#40 )👍 Still basically we are adding an unnecessary process step just for sandbox incompatibility with ESM....
We can use dynamic imports in supported Node.js also in a CJS context (in wrapper) With mlly, we can directly evaluate an ESM bundle (also from remote sources for lazy compilation in nitro). Since we use inline base64, cache is automatically bypassed. |
The problem here is that Maybe we should solve the root case instead? For forking the vue-renderer or require nitro to be used with this package? |
const __vite_ssr_exports__ = exports; | ||
const __vite_ssr_exportAll__ = __createViteSSRExportAll__(__vite_ssr_exports__) | ||
${res.code || '/* empty */'}; | ||
return module.exports; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note for @antfu: Vite transform is not handling CJS at all (Node.js and Rollup both have basic CJS-in-ESM proxy support). Had to do this because some node_modules
use CJS style exports but we also have this issue for the browser side. Maybe escalating this to add basic support to vite or refactor as a vite transform plugin?
The last change is using CJS bundle format and inlining all |
In the initial POC, we started using rollup to build server bundle in development mode (using vite.build) which is probably not the best idea since rollup is intended to be used for production builds of vite and by doing so, other than performance downsides introduces lots of unwanted behaviors from plugins in development.
Vite supports server side rendering but in order to use it, we need to use
vite.ssrLoadModule
that directly loads a module but this approach is neither compatible with nuxt2 that is using vue-bundle-renderer nor new nitro pipeline since both expect to receive a bundle code (string) for at least entry point to execute butssrLoadModule
directly gives a module instance.This PR is trying to use the same
viteServer
(instead of rollup) andtransformRequest (id, { ssr: true })
API to produce the same code from the module graph for building a compatible server bundle in development.Current issues with PR
Vite server produces ESM code (with resolved ESM externals) which is incompatible withvue-bundle-renderer
(of nuxt2) that uses a CJS wrapper to execute bundlevueComponentNormalizer
is empty (related tovite-plugin-vue2
?)TypeError: Cannot read property 'get' of undefined
Error when usingtransformRequest
(css-post plugins crashes when using transformRequest without first starting a server vitejs/vite#3798)antfu: Dependencies are cached in Node, which causingVue.use
to be called multiple times when re-requesting the pages (can't redefine '$router'
,[vue-composition-api] already installed
)Watch serverFuture improvements
viteServer
instance in development (this needs a way to set different aliases for client and server. maybe with a plugin replacingdefine
config?)