Skip to content

Commit 8863dde

Browse files
authored
docs: YAML plugin lesson updates (#12)
1 parent b8f5a67 commit 8863dde

File tree

5 files changed

+28
-26
lines changed

5 files changed

+28
-26
lines changed
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
---
22
type: lesson
3-
title: Defining custom plugin
3+
title: Defining a custom plugin
44
focus: /vite.config.ts
55
---
66

7-
# Defining custom plugin
7+
# Defining a custom plugin
88

9-
Plugins can have various properties that are listed in [Plugin API | Vite](https://vitejs.dev/guide/api-plugin.html) documentation. The only **required property** is `name`.
9+
A plugin is a plain object that defines hooks to modify the way Vite resolves ids, loads and transforms modules, and more. It can have various properties that are listed in the [Plugin API | Vite](https://vitejs.dev/guide/api-plugin.html) documentation. The only **required property** is `name`.
1010

11-
Let's start by adding a custom plugin in our Vite configuration. To do this, add a new `plugins` property into the Vite configuration. The `plugins` should be an array.
11+
Let's start by adding a custom plugin in our Vite config file. To do this, add a new `plugins` array property in the exported config object.
1212

13-
Inside the `plugins` array, define a new object with `name` `'yaml-plugin'`.
13+
Then, let's add a new object with `name` `'yaml-plugin'` inside the `plugins` array.
1414

15-
Successfully defined plugin should be reported in the Preview tab:
15+
A successfully defined plugin should be reported in the Preview tab:
1616

1717
> Loaded Vite plugins: yaml-plugin ✅

src/content/tutorial/vite-plugin/yaml-plugin/defining-load-hook/content.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
22
type: lesson
3-
title: Defining load hook
3+
title: Defining the load hook
44
focus: /vite.config.ts
55
---
66

7-
# Defining load hook
7+
# Defining the load hook
88

99
Next we'll need to intercept loading of `.yaml` files. Let's start by adding a [`load`](https://rollupjs.org/plugin-development/#load) hook in our Vite plugin.
1010

@@ -16,7 +16,7 @@ Next we'll need to intercept loading of `.yaml` files. Let's start by adding a [
1616
}
1717
```
1818

19-
Vite will call this hook with all the loaded files. As our plugin is only interested in `.yaml` files, we can check `id` for this extension.
19+
Vite will call the load hook of every plugin for each resolved id to the load the module code. A load hook can return custom code for an id, or skip it by to let other plugins handle it. As our plugin is only interested in `.yaml` files, we can check `id` for this extension.
2020

2121
```js
2222
{
@@ -29,7 +29,7 @@ Vite will call this hook with all the loaded files. As our plugin is only intere
2929
}
3030
```
3131

32-
Our `load` function should return an object with `code` property. This property should contain the actual code that importing this specific `.yaml` file should produce. As we don't yet know how to convert `.yaml` files into Javascript, we can return something simpler here.
32+
Our `load` function should return an object with a `code` property for these ids. This property should contain the actual code that importing this specific `.yaml` file should produce. As we don't yet know how to convert `.yaml` files into JavaScript, let's return a simple module that exports a default string first:
3333

3434
```js
3535
{

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ title: Importing YAML files
44
focus: /index.js
55
---
66

7-
# Welcome to Vite Plugin tutorial
7+
# Welcome to the Vite Plugin API tutorial
88

9-
Hey there, and welcome to Vite Plugin tutorial 👋!
9+
Hey there, and welcome to the Vite Plugin API tutorial 👋!
1010

11-
In this tutorial we'll go through steps for creating custom Vite plugins. We'll start by creating a custom Vite plugin for loading `.yaml` files.
11+
We'll create custom Vite plugins step by step, starting with a custom Vite plugin for loading `.yaml` files.
1212

13-
Our goal is to be able to import `content.yaml` file in a Javascript file and use it as plain Javascript object.
13+
Our goal is to be able to import a `content.yaml` file in a module and use it as plain JavaScript object:
1414

1515
```js [index.js]
1616
import content from "./content.yaml";
@@ -19,7 +19,7 @@ console.log(content);
1919
// > [{ employees: [{ id: 1, ... }, ...], projects: [{ id: 101, ...}, ...] }]
2020
```
2121

22-
Try doing this in `index.js`:
22+
Starting with the content as a variable in `index.js`, try changing the code to import it from the YAML file instead:
2323

2424
```js [index.js] add={1} del={2}
2525
import content from "./content.yaml";
@@ -28,4 +28,4 @@ const content = ["Initial content"];
2828
export default content;
2929
```
3030

31-
At this point we should run into error when Vite fails to load `.yaml` file. 
31+
We should run into an error when Vite fails to load `.yaml` file. 

src/content/tutorial/vite-plugin/yaml-plugin/processing-yaml-files/content.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ focus: /vite.config.ts
66

77
# Processing YAML files
88

9-
Our Vite plugin can now recognize `.yaml` files that are being loaded. Next we'll need to add logic for converting the `.yaml` files into Javascript and pass the output from `load` hook.
9+
Our Vite plugin can now properly intercept `.yaml` files that are being loaded. Next we'll need to add logic for converting the `.yaml` files into JavaScript and return that as the output of our `load` hook.
1010

11-
To get the content of requested file we'll need to use [`readFileSync`](https://nodejs.org/api/fs.html#fsreadfilesyncpath-options) method from `node:fs`.
12-
We can convert the YAML content into Javascript using [`yaml`](https://www.npmjs.com/package/yaml) package. It has a `parse()` function that takes YAML content as `string`.
11+
To get the content of requested file we can use [`readFileSync`](https://nodejs.org/api/fs.html#fsreadfilesyncpath-options) method from `node:fs`.
12+
We can convert the YAML content into JavaScript using the [`yaml`](https://www.npmjs.com/package/yaml) npm package. It has a `parse()` function that takes YAML content as `string` and returns a JavaScript object.
1313

1414
```ts
1515
import { readFileSync } from "node:fs";
@@ -20,18 +20,18 @@ const yaml = parse(content);
2020
// ^^^^ [{ employees: [{ id: 1, ...}, ...], projects: [{ id: 101, ...}] }]
2121
```
2222

23-
The `yaml` variable now holds a Javascript object that represents our `content.yaml` contents. To return this from `load` hook, we'll need to serialize it to string with [`JSON.stringify`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify):
23+
The `yaml` variable now holds an object that represents our `content.yaml` contents. To return this from the `load` hook, we'll need to serialize it to a string with [`JSON.stringify`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify):
2424

2525
```ts
2626
return {
2727
code: `export default ${JSON.stringify(yaml)}`
2828
};
2929
```
3030

31-
Add required logic to Vite plugin's load hook:
31+
Add the required logic to Vite plugin's load hook:
3232

33-
- Read requested file from file system using `readFileSync` (filename is in `id` variable)
34-
- Convert contents of `.yaml` file into Javascript using `parse` from `yaml` package
35-
- Serialize the Javascript object into `string` and return it as default export from the `load` hook
33+
- Read requested file from file system using `readFileSync` (the filename is the `id` parameter).
34+
- Convert contents of `.yaml` file into Javascript using `parse` from `yaml` package.
35+
- Serialize the JavaScript object into a `string` and return it as the default export of a module from the `load` hook.
3636

37-
Contents of `content.yaml` should appear in preview as JSON. 
37+
The contents of `content.yaml` should appear in the preview as JSON. 

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ focus: /vite.config.ts
66

77
# Summary
88

9-
Our custom Vite plugin can now intercept `*.yaml` file imports and convert them into Javascript.
9+
Our custom Vite plugin can now intercept `*.yaml` file imports and load them as JavaScript modules.
1010

1111
Before you head on to create your own plugins for loading some other non-Javascript files, let's summarize what we've done:
1212

@@ -25,3 +25,5 @@ Before you head on to create your own plugins for loading some other non-Javascr
2525
</ol>
2626

2727
📚 Homework: Build a Vite plugin that can import [`.csv` files](https://en.wikipedia.org/wiki/Comma-separated_values) directly into Javascript. You can even build your own minimal CSV-parser!
28+
29+
Tip: Before creating your own plugin, search for popular Vite or compatible Rollup plugins. For YAML support, you can directly add the official [@rollup/plugin-yaml](https://www.npmjs.com/package/@rollup/plugin-yaml) to your Vite config.

0 commit comments

Comments
 (0)