Skip to content

Commit b8f5a67

Browse files
authored
docs: env plugin updates (#13)
1 parent 9534d17 commit b8f5a67

File tree

5 files changed

+31
-33
lines changed

5 files changed

+31
-33
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
type: lesson
3-
title: Env variables in load hook
3+
title: Env variables in the load hook
44
focus: /vite.config.ts
55
mainCommand: ""
66
terminal:
@@ -9,11 +9,11 @@ terminal:
99
- ["terminal", "Terminal"]
1010
---
1111

12-
# Env variables in load hook
12+
# Env variables in the load hook
1313

14-
We are now able to intercept loading of the virtual module and have prevented other plugins from colliding with it.
14+
We are now able to intercept the loading of the virtual module and have prevented other plugins from colliding with it.
1515

16-
Next we'll need to handle the actual environment variables. As environment variables are an easy way to expose secrets, let's allow our plugin to only expose variables that are prefixed with `TUTORIAL_`:
16+
Next, we'll need to handle the actual environment variables. As environment variables are an easy way to expose secrets, let's extend our plugin to only expose variables that are prefixed with `TUTORIAL_`:
1717

1818
```ts
1919
function getTutorialEnvVariables() {
@@ -42,7 +42,7 @@ load(id, options) {
4242
},
4343
```
4444

45-
Next let's open up the terminal and start Vite server while providing an environment variable:
45+
Next, let's open up the terminal and start Vite server while providing an environment variable:
4646

4747
```sh
4848
TUTORIAL_MY_MESSAGE="Hello world" npm run dev

src/content/tutorial/vite-plugin/env-plugin/env-vars-in-node/content.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
type: lesson
3-
title: Environment Variables in Node
3+
title: Vite Env plugin
44
focus: /index.js
55
previews: false
66
template: empty
@@ -14,26 +14,26 @@ terminal:
1414

1515
# Vite Env plugin
1616

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.
17+
In this chapter, we'll be creating a plugin that allows us to use environment variables from a virtual module.
18+
By "virtual" we mean a module that doesn't exist on the file system but is provided by a Vite plugin in runtime.
1919

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).
20+
We'll define a module entry point for `"virtual:tutorial-env"`. Note that `virtual:` is a [virtual modules convention](https://vitejs.dev/guide/api-plugin#virtual-modules-convention).
2121

2222
```ts
2323
import env from "virtual:tutorial-env";
2424
```
2525

26-
But first let's take a look at how environment variables work in Node.
26+
But first, let's take a look at how environment variables work in Node.
2727

28-
- On Unix systems environment variables can be passed to commands by defining them before command itself
28+
- On Unix systems environment variables can be passed to commands by defining them before the command itself
2929

3030
```sh
3131
TUTORIAL_MESSAGE=Hello node index.js
3232
```
3333

3434
<br aria-hidden />
3535

36-
- Environments variables are available in [`process.env`](https://nodejs.org/api/process.html#processenv)
36+
- Environment variables are available in [`process.env`](https://nodejs.org/api/process.html#processenv)
3737
```js
3838
console.log(process.env.TUTORIAL_MESSAGE);
3939
// Hello

src/content/tutorial/vite-plugin/env-plugin/importing-virtual-module/content.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
---
22
type: lesson
3-
title: Importing virtual module
3+
title: Importing a virtual module
44
focus: /index.js
55
---
66

7-
# Importing virtual module
7+
# Importing a virtual module
88

99
Now that we know how environment variables work, let's continue with our custom plugin!
1010

11-
Add an import for the virtual module in `index.js`. At this point it should break our Vite setup but that's fine!
11+
Add an import for the virtual module in `index.js`. At this point, it should break our Vite setup but that's fine!
1212

1313
```ts add={1,2} del={3}
1414
import env from "virtual:tutorial-env";
@@ -31,7 +31,7 @@ export default defineConfig({
3131
});
3232
```
3333

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.
34+
In the previous chapter, we saw that a plugin can intercept module requests in the `load()` hook. Let's try using the same trick here.
3535

3636
```ts
3737
{
@@ -44,7 +44,7 @@ In the previous lessons we saw that a plugin can intercept module request in the
4444
},
4545
```
4646

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`.
47+
Even when we are handling the file loading, Vite keeps showing an error. Note how this error is thrown by another Vite plugin - Vite's internal `plugin:vite:import-analysis`.
4848

4949
> [plugin:vite:import-analysis] Failed to resolve import "virtual:tutorial-env" from "index.js". Does the file exist?
5050

src/content/tutorial/vite-plugin/env-plugin/resolve-id-of-virtual-module/content.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
---
22
type: lesson
3-
title: Resolving id of virtual module
3+
title: Resolving the id of a virtual module
44
focus: /vite.config.ts
55
---
66

7-
# Resolving id of virtual module
7+
# Resolving the id of a virtual module
88

9-
Our plugin is now able to load the virtual module. But there are other Vite plugins failing as they also attempt to load it.
9+
Our plugin is now able to load the virtual module. But other Vite plugins are failing as they also attempt to load it.
1010

11-
To solve this we'll need to internally mark the virtual module with a special prefix so that other plugins will know to skip it. There's [a convention](https://vitejs.dev/guide/api-plugin#virtual-modules-convention) of using `\0` in the internal module ids - let's add that!
11+
To solve this we'll need to internally mark the virtual module with a special prefix so that other plugins skip it. There's [a convention](https://vitejs.dev/guide/api-plugin#virtual-modules-convention) of using `\0` in the internal module ids - let's add that!
1212

13-
Vite will internally resolve ids for each requested module using the `resolveId` hook. Add this to the plugin:
13+
Vite internally resolve ids for each requested module using the `resolveId` hook. Add this to the plugin:
1414

1515
```ts add={3}
1616
{
@@ -24,7 +24,7 @@ Vite will internally resolve ids for each requested module using the `resolveId`
2424
}
2525
```
2626

27-
Next we'll need to use the special `\0` prefix in the resolved id. We can simply look for our virtual module's entrypoint and return that with the prefix:
27+
Next, we need to prefix the resolved id using the special `\0` marker. Once `resolveId` is called for our virtual module's entry point, we'll return it with this prefix:
2828

2929
```ts
3030
{
@@ -42,9 +42,7 @@ Next we'll need to use the special `\0` prefix in the resolved id. We can simply
4242
}
4343
```
4444

45-
Perfect - the error from Vite's internal `plugin:vite:import-analysis` is now gone! But `index.js` still fails to load.
46-
47-
To fix this we'll need to apply the `\0` prefix in the `load()` hook as well:
45+
Perfect - the error from Vite's internal `plugin:vite:import-analysis` is now gone! But `index.js` still fails to load. The `load()` hook receives the resolved ids, so we need to be checking for the `\0` prefix in it as well:
4846

4947
```ts add={9} del={10}
5048
{

src/content/tutorial/vite-plugin/env-plugin/summarize-env-plugin/content.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@ terminal:
1111

1212
# Summarize
1313

14-
We've now built a plugin for providing entrypoint for virtual modules. Let's summarize the process:
14+
We've now built a plugin for providing entry point for virtual modules. Let's summarize the process:
1515

1616
<ol>
1717
<li>Source code requests virtual module: <code class="whitespace-nowrap">import env from "virtual:tutorial-env"</code>
1818

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>
19+
<li class="mt2">Our Vite plugin's <code>resolveId()</code> is called to resolve the id for each imported module specifier. In our plugin, this hook returns an id prefixed with the <code>\0</code> marker, a convention to let other plugins know that this is a virtual module.</li>
2020

21-
<li class="mt2">Vite plugin's <code>load()</code> is called with the internal id. Our custom plugin recognizes this id.</li>
21+
<li class="mt2">Our Vite plugin's <code>load()</code> is called with the resolved id. Our custom plugin recognizes this id.</li>
2222

2323
<li class="mt2">Other Vite plugins see <code>\0</code> prefix and know to skip this module</li>
2424

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>
25+
<li class="mt2">A custom plugin reads the host machine's environment variables from <code>process.end</code> and collects the <code>TUTORIAL_</code> prefixed variables.</li>
2626

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>
27+
<li class="mt2">A JavaScript object is serialized into text using <code>JSON.stringify()</code> and returned as the default export from <code>load()</code> hook</li>
2828

29-
<li class="mt2">Source code successfully loads the virtual module&nbsp;✅</li>
29+
<li class="mt2">The source code successfully loads the virtual module&nbsp;✅</li>
3030
</ol>
3131

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.
32+
📚 Homework: Build a Vite plugin that provides a 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 a named export.

0 commit comments

Comments
 (0)