Skip to content

Commit e904a67

Browse files
committed
feat: define load hook
1 parent 5baad8b commit e904a67

File tree

7 files changed

+76
-8
lines changed

7 files changed

+76
-8
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
@@ -28,4 +28,4 @@ const content = ["Initial content"];
2828
export { content };
2929
```
3030

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

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

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

77
# Defining custom plugin
88

9-
Let's start by adding a custom plugin in our Vite configuration.
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`.
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.
12+
13+
Inside the `plugins` array, define a new object with `name` `'yaml-plugin'`.

src/content/tutorial/1-vite-plugin/1-yaml-plugin/3-defining-load-hook/_solution/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default defineConfig({
77
load(id, options) {
88
if (id.endsWith(".yaml")) {
99
return {
10-
code: `export default 'Loaded ${id}';`,
10+
code: `export default "Trying to load '${id}' file.";`,
1111
};
1212
}
1313
},

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

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

77
# Defining load hook
88

9-
Let's start by adding a load hook in our Vite configuration.
9+
Let's start by adding a [`load`](https://rollupjs.org/plugin-development/#load) hook in our Vite plugin.
10+
11+
```js
12+
{
13+
name: "yaml-plugin",
14+
load(id, options) {
15+
}
16+
}
17+
```
18+
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.
20+
21+
```js
22+
{
23+
name: "yaml-plugin",
24+
load(id, options) {
25+
if(id.endsWith('.yaml')) {
26+
// YAML related logic comes here
27+
}
28+
}
29+
}
30+
```
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.
33+
34+
```js
35+
{
36+
name: "yaml-plugin",
37+
load(id, options) {
38+
if(id.endsWith('.yaml')) {
39+
return {
40+
code: `export default "Trying to load '${id}' file.";`,
41+
};
42+
}
43+
}
44+
}
45+
```

src/templates/default/main.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const ID = "vite-plugin-content";
55
document.querySelector("#app").innerHTML = `
66
<div>
77
<h1>Hello Vite Plugin tutorial!</h1>
8+
<p>Loaded Vite plugins: ${window.TUTORIAL_LOADED_VITE_PLUGINS || "none"}</p>
89
<pre id="${ID}"></pre>
910
</div>
1011
`;
@@ -18,6 +19,8 @@ import("./index.js")
1819
);
1920
})
2021
.catch((error) => {
21-
const message = `Failed to load index.js: "${error instanceof Error ? error.message : String(error)}"`;
22-
document.getElementById(ID).textContent = message;
22+
const element = document.getElementById(ID);
23+
const message = `Failed to load index.js: "${error instanceof Error ? error.message : String(error)}" ❌`;
24+
element.textContent = message;
25+
element.classList.add("error");
2326
});

src/templates/default/style.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,11 @@ pre {
106106
border-radius: 5px;
107107
padding: 16px;
108108
}
109+
110+
.error {
111+
font-weight: 600;
112+
color: red;
113+
background-color: white;
114+
border: 2px dashed red;
115+
outline: 2px dashed red;
116+
}
Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
1-
import { defineConfig, mergeConfig } from "vite";
1+
import {
2+
defineConfig,
3+
mergeConfig,
4+
type Plugin,
5+
type PluginOption,
6+
} from "vite";
27

38
import viteConfig from "./vite.config";
49

10+
const loadedPlugins = viteConfig.plugins?.map(getName).filter(Boolean).join();
11+
512
export default mergeConfig(
613
defineConfig({
7-
// More options here?
14+
define: {
15+
TUTORIAL_LOADED_VITE_PLUGINS: `"${loadedPlugins || "none"}"`,
16+
},
817
}),
918
viteConfig
1019
);
20+
21+
function getName(plugin: PluginOption) {
22+
if (plugin && "name" in plugin && plugin.name.length > 0) {
23+
return `${plugin.name} ✅`;
24+
}
25+
26+
return null;
27+
}

0 commit comments

Comments
 (0)