Skip to content

Commit c8cbc29

Browse files
committed
feat: summary
1 parent d09cc24 commit c8cbc29

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
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
@@ -10,7 +10,7 @@ Hey there, and welcome to Vite Plugin tutorial 👋!
1010

1111
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.
1212

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

1515
```js [index.js]
1616
import content from "./content.yaml";

src/content/tutorial/1-vite-plugin/1-yaml-plugin/2-defining-custom-plugin/content.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ Plugins can have various properties that are listed in [Plugin API | Vite](https
1111
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.
1212

1313
Inside the `plugins` array, define a new object with `name` `'yaml-plugin'`.
14+
15+
Successfully defined plugin should be reported in the Preview tab:
16+
17+
> Loaded Vite plugins: yaml-plugin ✅

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

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

99
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.
1010

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`.
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`.
1212
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`.
1313

1414
```ts
@@ -17,10 +17,10 @@ import { parse } from "yaml";
1717

1818
const content = readFileSync("./content.yaml", "utf8");
1919
const yaml = parse(content);
20-
// ^^^^ [{ employees: [{ id: 1, ...}, ...], projects: { id: 101, ...}, ... }]
20+
// ^^^^ [{ 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`:
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):
2424

2525
```ts
2626
return {

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,38 @@ focus: /vite.config.ts
77
# Summary
88

99
Our custom Vite plugin can now intercept `*.yaml` file imports and convert them into Javascript.
10+
11+
<div class="mt-6 p-3 border-1 border-current border-dashed border-rd-md">
12+
<div class="mb-3">Source code loads YAML-file:</div>
13+
<code>import content from "./some-file.yaml"</code>
14+
</div>
15+
16+
<div class="mt-6 w-full text-center">⬇️</div>
17+
18+
<div class="mt-6 p-3 border-1 border-current border-dashed border-rd-md">
19+
Vite plugin's <code>load()</code> hook intercepts <code>.yaml</code> request
20+
</div>
21+
22+
<div class="mt-6 w-full text-center">⬇️</div>
23+
24+
<div class="mt-6 p-3 border-1 border-current border-dashed border-rd-md">
25+
Contents of requested file are read from file system using <code>readFileSync()</code> from <code>node:fs</code> module
26+
</div>
27+
28+
<div class="mt-6 w-full text-center">⬇️</div>
29+
30+
<div class="mt-6 p-3 border-1 border-current border-dashed border-rd-md">
31+
YAML content is converted into Javascript using <code>parse()</code> from <code>yaml</code> module
32+
</div>
33+
34+
<div class="mt-6 w-full text-center">⬇️</div>
35+
36+
<div class="mt-6 p-3 border-1 border-current border-dashed border-rd-md">
37+
Javascript object is serialized into text using <code>JSON.stringify()</code> and returned as default export from <code>load()</code> hook
38+
</div>
39+
40+
<div class="mt-6 w-full text-center">⬇️</div>
41+
42+
<div class="mt-6 p-3 border-1 border-current border-dashed border-rd-md">
43+
Source code successfully loads Javascript object from <code>.yaml</code> file import ✅
44+
</div>

0 commit comments

Comments
 (0)