-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Node ESM loader #17
Merged
Merged
Node ESM loader #17
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 tasks
cyco130
force-pushed
the
feat/esm-loader
branch
3 times, most recently
from
December 21, 2022 14:30
8a17526
to
3b1fd25
Compare
cyco130
force-pushed
the
feat/esm-loader
branch
from
February 26, 2023 23:25
3b1fd25
to
1e9c8dd
Compare
cyco130
force-pushed
the
feat/esm-loader
branch
4 times, most recently
from
February 27, 2023 09:09
88990ac
to
3c53ea4
Compare
cyco130
force-pushed
the
feat/esm-loader
branch
from
February 27, 2023 09:25
3c53ea4
to
795bc3f
Compare
6 tasks
9 tasks
Closed
This was referenced Mar 9, 2023
It works also for sveltekit! |
Closed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR implements a Node ESM loader that transpiles modules with Vite. It enables sourcemaps and breakpoints, fixing #6.
The problem
By default, Vite transforms modules loaded with
ssrLoadModule
into an async function using a function constructor. So the following module:is transformed into something like the following:
This way, Vite can create, in runtime, pseudo-modules with full control over module resolution, transformation, and execution.
Unfortunately, functions created from strings have limited support for sourcemaps and breakpoints, making it painful to debug server-side code when using frameworks like Rakkas, vite-plugin-ssr, SvelteKit, Astro, Qwik.js, or SolidStart.
The solution
This PR disables Vite's builtin SSR transform and implements a Node ESM loader to resolve and transform SSR modules instead. The loader uses Vite's dev server to perform the resolutions and transformations. Since it hooks into Node's builtin loading mechanism, sourcemaps and breakpoints work as expected. It's also faster and -potentially- more compatible.
Usage
If you're already using
vavite
, you can simply usevavite dev --use-loader
instead ofvite dev
. Otherwise:@vavite/node-loader
as a dev dependency.import { nodeLoaderPlugin } from "@vavite/node-loader/plugin"
to your Vite confignodeLoaderPlugin()
to your Vite pluginsvavite-loader vite dev
(orvavite-loader rakkas dev
,vavite-loader astro dev
, etc.)Compatibility
It should work with any Vite SSR project or framework that uses
ssrLoadModule
to load SSR modules in the same process as Vite itself. I tested with the following:vite-plugin-ssr
with React and VueNuxt 3 doesn't work because it seems to have implemented its own fix using the
node:vm
module to load SSR modules.I only did in-depth testing with Rakkas and I did encounter a few glitches on SvelteKit and Astro. But, overall, it seems to work.
It doesn't work on WebContainers (StackBlitz) or Deno.
Limitations