Skip to content

Commit 9be88c5

Browse files
authored
feat: virtual file plugin tutorial (#1)
* fix: lesson title incorrect * feat: env plugin, env vars in node * feat: importing virtual module * feat: resolve id of virtual module * feat: env variables in load hook
1 parent db16233 commit 9be88c5

File tree

30 files changed

+1351
-8
lines changed

30 files changed

+1351
-8
lines changed

src/content/tutorial/1-vite-plugin/1-yaml-plugin/1-importing-yaml-files/content.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
type: lesson
3-
title: importing-yaml-files
3+
title: Importing YAML files
44
focus: /index.js
55
---
66

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const message = process.env.TUTORIAL_MESSAGE;
2+
3+
console.log(`Message is ${message}`);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
type: lesson
3+
title: Environment Variables in Node
4+
focus: /index.js
5+
previews: false
6+
template: empty
7+
mainCommand: ""
8+
prepareCommands: []
9+
terminal:
10+
activePanel: 1
11+
panels:
12+
- ["terminal", "Terminal"]
13+
---
14+
15+
# Vite Env plugin
16+
17+
In this part we'll be creating a plugin that allows us to use environment variables from virtual module.
18+
By "virtual" we mean a module that doesn't really exist on file system but is provided by a Vite plugin in runtime.
19+
20+
We'll be defining a module entrypoint for `"virtual:tutorial-env"`. Note that `virtual:` is a [convention of Vite virtual modules](https://vitejs.dev/guide/api-plugin#virtual-modules-convention).
21+
22+
```ts
23+
import env from "virtual:tutorial-env";
24+
```
25+
26+
But first let's take a look at how environment variables work in Node.
27+
28+
- On Unix systems environment variables can be passed to commands by defining them before command itself
29+
30+
```sh
31+
TUTORIAL_MESSAGE=Hello node index.js
32+
```
33+
34+
<br aria-hidden />
35+
36+
- Environments variables are available in [`process.env`](https://nodejs.org/api/process.html#processenv)
37+
```js
38+
console.log(process.env.TUTORIAL_MESSAGE);
39+
// Hello
40+
```
41+
42+
Try passing a different value to `node ./index.js` in the terminal.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const output = "Initial output";
2+
3+
export default output;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { defineConfig } from "vite";
2+
3+
export default defineConfig({
4+
5+
});
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { defineConfig } from "vite";
2+
3+
export default defineConfig({
4+
plugins: [
5+
{
6+
name: "env-plugin",
7+
load(id, options) {
8+
if (id === "virtual:tutorial-env") {
9+
return "export default 'This should work 🤔'";
10+
}
11+
},
12+
},
13+
],
14+
});
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
type: lesson
3+
title: Importing virtual module
4+
focus: /index.js
5+
---
6+
7+
# Importing virtual module
8+
9+
Now that we know how environment variables work, let's continue with our custom plugin!
10+
11+
Add an import for the virtual module in `index.js`. At this point it should break our Vite setup but that's fine!
12+
13+
```ts add={1,2} del={3}
14+
import env from "virtual:tutorial-env";
15+
const output = env;
16+
const output = "Initial output";
17+
18+
export default output;
19+
```
20+
21+
To fix the loading error we'll need to create a Vite plugin that can handle this special `virtual:tutorial-env` import.
22+
Let's add a new plugin with the name `env-plugin`.
23+
24+
```ts
25+
export default defineConfig({
26+
plugins: [
27+
{
28+
name: "env-plugin",
29+
},
30+
],
31+
});
32+
```
33+
34+
In the previous lessons we saw that a plugin can intercept module request in the `load()` hook. Let's try using the same trick here.
35+
36+
```ts
37+
{
38+
name: "env-plugin",
39+
load(id, options) {
40+
if (id === "virtual:tutorial-env") {
41+
return "export default 'This should work 🤔'";
42+
}
43+
},
44+
},
45+
```
46+
47+
Even when we are handling the loading of the file, Vite keeps showing an error. Note how this error is thrown by another Vite plugin - Vite's internal `plugin:vite:import-analysis`.
48+
49+
> [plugin:vite:import-analysis] Failed to resolve import "virtual:tutorial-env" from "index.js". Does the file exist?
50+
51+
Let's look at this error more in the next lesson!
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { defineConfig } from "vite";
2+
3+
export default defineConfig({
4+
plugins: [
5+
{
6+
name: "env-plugin",
7+
load(id, options) {
8+
if (id === "virtual:tutorial-env") {
9+
return "export default 'This should work 🤔'";
10+
}
11+
},
12+
},
13+
],
14+
});

0 commit comments

Comments
 (0)