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
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`.
10
10
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.
12
12
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.
14
14
15
-
Successfully defined plugin should be reported in the Preview tab:
15
+
A successfully defined plugin should be reported in the Preview tab:
Copy file name to clipboardExpand all lines: src/content/tutorial/vite-plugin/yaml-plugin/defining-load-hook/content.md
+4-4Lines changed: 4 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
1
---
2
2
type: lesson
3
-
title: Defining load hook
3
+
title: Defining the load hook
4
4
focus: /vite.config.ts
5
5
---
6
6
7
-
# Defining load hook
7
+
# Defining the load hook
8
8
9
9
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.
10
10
@@ -16,7 +16,7 @@ Next we'll need to intercept loading of `.yaml` files. Let's start by adding a [
16
16
}
17
17
```
18
18
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.
20
20
21
21
```js
22
22
{
@@ -29,7 +29,7 @@ Vite will call this hook with all the loaded files. As our plugin is only intere
29
29
}
30
30
```
31
31
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:
Copy file name to clipboardExpand all lines: src/content/tutorial/vite-plugin/yaml-plugin/processing-yaml-files/content.md
+9-9Lines changed: 9 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -6,10 +6,10 @@ focus: /vite.config.ts
6
6
7
7
# Processing YAML files
8
8
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.
10
10
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.
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):
24
24
25
25
```ts
26
26
return {
27
27
code: `export default ${JSON.stringify(yaml)}`
28
28
};
29
29
```
30
30
31
-
Add required logic to Vite plugin's load hook:
31
+
Add the required logic to Vite plugin's load hook:
32
32
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.
36
36
37
-
Contents of `content.yaml` should appear in preview as JSON. ✅
37
+
The contents of `content.yaml` should appear in the preview as JSON. ✅
Copy file name to clipboardExpand all lines: src/content/tutorial/vite-plugin/yaml-plugin/summarize-yaml-plugin/content.md
+3-1Lines changed: 3 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ focus: /vite.config.ts
6
6
7
7
# Summary
8
8
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.
10
10
11
11
Before you head on to create your own plugins for loading some other non-Javascript files, let's summarize what we've done:
12
12
@@ -25,3 +25,5 @@ Before you head on to create your own plugins for loading some other non-Javascr
25
25
</ol>
26
26
27
27
📚 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