Skip to content

Commit b0d43e7

Browse files
committed
feat: summary
1 parent e904a67 commit b0d43e7

File tree

15 files changed

+192
-11
lines changed

15 files changed

+192
-11
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"prettier-plugin-astro": "^0.13.0",
3131
"typescript": "^5.4.5",
3232
"unocss": "^0.59.4",
33-
"vite": "^5.2.0"
33+
"vite": "^5.2.0",
34+
"yaml": "^2.4.5"
3435
},
3536
"packageManager": "[email protected]+sha512.d1a029e1a447ad90bc96cd58b0fad486d2993d531856396f7babf2d83eb1823bb83c5a3d0fc18f675b2d10321d49eb161fece36fe8134aa5823ecd215feed392"
3637
}

pnpm-lock.yaml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

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

77
# Defining load hook
88

9-
Let's start by adding a [`load`](https://rollupjs.org/plugin-development/#load) hook in our Vite plugin.
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.
1010

1111
```js
1212
{
@@ -16,14 +16,14 @@ Let's start by adding a [`load`](https://rollupjs.org/plugin-development/#load)
1616
}
1717
```
1818

19-
Vite will call this hook with all loaded files. As our plugin is only interested in `.yaml` files, we can check `id` for this extension.
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.
2020

2121
```js
2222
{
2323
name: "yaml-plugin",
2424
load(id, options) {
2525
if(id.endsWith('.yaml')) {
26-
// YAML related logic comes here
26+
// id === "/home/tutorial/content.yaml"
2727
}
2828
}
2929
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
employees:
2+
- id: 1
3+
name: John Doe
4+
job: Developer
5+
skills:
6+
- JavaScript
7+
- Python
8+
- C++
9+
- id: 2
10+
name: Jane Doe
11+
job: Designer
12+
skills:
13+
- Photoshop
14+
- Illustrator
15+
- InDesign
16+
projects:
17+
- id: 101
18+
name: Project Alpha
19+
description: This is the first project.
20+
- id: 102
21+
name: Project Beta
22+
description: This is the second project.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import content from "./content.yaml";
2+
3+
export { content };
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { defineConfig } from "vite";
2+
3+
export default defineConfig({
4+
plugins: [
5+
{
6+
name: "yaml-plugin",
7+
load(id, options) {
8+
if (id.endsWith(".yaml")) {
9+
return {
10+
code: `export default "Trying to load '${id}' file.";`,
11+
};
12+
}
13+
},
14+
},
15+
],
16+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { readFileSync } from "node:fs";
2+
import { parse } from "yaml";
3+
import { defineConfig } from "vite";
4+
5+
export default defineConfig({
6+
plugins: [
7+
{
8+
name: "yaml-plugin",
9+
load(id, options) {
10+
if (id.endsWith(".yaml")) {
11+
const content = readFileSync(id, "utf8");
12+
const yaml = parse(content);
13+
14+
return {
15+
code: `export default ${JSON.stringify(yaml)}`,
16+
};
17+
}
18+
},
19+
},
20+
],
21+
});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
type: lesson
3+
title: Processing YAML files
4+
focus: /vite.config.ts
5+
---
6+
7+
# Processing YAML files
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.
10+
11+
To get the content of loaded 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`.
13+
14+
```ts
15+
import { readFileSync } from "node:fs";
16+
import { parse } from "yaml";
17+
18+
const content = readFileSync("./content.yaml", "utf8");
19+
const yaml = parse(content);
20+
// ^^^^ [{ employees: [{ id: 1, ...}, ...], projects: { id: 101, ...}, ... }]
21+
```
22+
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`:
24+
25+
```ts
26+
return {
27+
code: `export default ${JSON.stringify(yaml)}`,
28+
};
29+
```
30+
31+
Add required logic to Vite plugin's load hook:
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
36+
37+
Contents of `content.yaml` should appear in preview as JSON. ✅
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
employees:
2+
- id: 1
3+
name: John Doe
4+
job: Developer
5+
skills:
6+
- JavaScript
7+
- Python
8+
- C++
9+
- id: 2
10+
name: Jane Doe
11+
job: Designer
12+
skills:
13+
- Photoshop
14+
- Illustrator
15+
- InDesign
16+
projects:
17+
- id: 101
18+
name: Project Alpha
19+
description: This is the first project.
20+
- id: 102
21+
name: Project Beta
22+
description: This is the second project.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import content from "./content.yaml";
2+
3+
export { content };

0 commit comments

Comments
 (0)