You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We've now built a plugin for providing entrypoint for virtual modules. Let's summarize the process:
15
+
16
+
<ol>
17
+
<li>Source code requests virtual module: <codeclass="whitespace-nowrap">import env from "virtual:tutorial-env"</code>
18
+
19
+
<liclass="mt2">Vite plugin's <code>resolveId()</code> is called to ask an internal id for the file. Hook returns an id and prefixes it with <code>\0</code>.</li>
20
+
21
+
<liclass="mt2">Vite plugin's <code>load()</code> is called with the internal id. Our custom plugin recognizes this id.</li>
22
+
23
+
<liclass="mt2">Other Vite plugins see <code>\0</code> prefix and know to skip this module</li>
24
+
25
+
<liclass="mt2">Custom plugin reads host machine's environment variables from <code>process.end</code> and collects the <code>TUTORIAL_</code> prefixed variables.</li>
26
+
27
+
<liclass="mt2">Javascript object is serialized into text using <code>JSON.stringify()</code> and returned as default export from <code>load()</code> hook</li>
28
+
29
+
<liclass="mt2">Source code successfully loads the virtual module ✅</li>
30
+
</ol>
31
+
32
+
📚 Homework: Build a Vite plugin that provides virtual module for importing version of your package - `import { version } from "virtual:tutorial-assignment"`. It should read the `"version"` field from `package.json` and return it as named export.
0 commit comments