Skip to content

Commit 4b5bee8

Browse files
committed
feat: summary for env plugin
1 parent 2dfdcb6 commit 4b5bee8

File tree

5 files changed

+71
-21
lines changed

5 files changed

+71
-21
lines changed

src/content/tutorial/1-vite-plugin/1-yaml-plugin/5-summarize-yaml-plugin/_solution/vite.config.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/content/tutorial/1-vite-plugin/2-env-plugin/4-env-variables-in-load-hook/content.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,5 @@ Next let's open up the terminal and start Vite server while providing an environ
4747
```sh
4848
TUTORIAL_MY_MESSAGE="Hello world" npm run dev
4949
```
50+
51+
The passed environment variable should now appear in the preview tab. 
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import env from "virtual:tutorial-env";
2+
const output = env;
3+
4+
export default output;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { defineConfig } from "vite";
2+
3+
export default defineConfig({
4+
plugins: [
5+
{
6+
name: "env-plugin",
7+
resolveId(id) {
8+
if (id === "virtual:tutorial-env") {
9+
return "\0virtual:tutorial-env";
10+
}
11+
},
12+
load(id, options) {
13+
if (id === "\0virtual:tutorial-env") {
14+
const envVars = getTutorialEnvVariables();
15+
return `export default ${JSON.stringify(envVars)}`;
16+
}
17+
},
18+
},
19+
],
20+
});
21+
22+
function getTutorialEnvVariables() {
23+
const output = {};
24+
const names = Object.keys(process.env);
25+
26+
for (const name of names) {
27+
if (name.startsWith("TUTORIAL_")) {
28+
output[name] = process.env[name];
29+
}
30+
}
31+
32+
return output;
33+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
type: lesson
3+
title: Summary
4+
focus: /vite.config.ts
5+
mainCommand: ""
6+
terminal:
7+
activePanel: 1
8+
panels:
9+
- ["terminal", "Terminal"]
10+
---
11+
12+
# Summarize
13+
14+
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: <code class="whitespace-nowrap">import env from "virtual:tutorial-env"</code>
18+
19+
<li class="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+
<li class="mt2">Vite plugin's <code>load()</code> is called with the internal id. Our custom plugin recognizes this id.</li>
22+
23+
<li class="mt2">Other Vite plugins see <code>\0</code> prefix and know to skip this module</li>
24+
25+
<li class="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+
<li class="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+
<li class="mt2">Source code successfully loads the virtual module&nbsp;✅</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

Comments
 (0)