Skip to content

Commit 32607e7

Browse files
authored
feat: replace plugin (#2)
1 parent 4b5bee8 commit 32607e7

File tree

32 files changed

+1261
-3
lines changed

32 files changed

+1261
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default function vitePluginReplace() {
2+
return {
3+
name: "replace-plugin",
4+
};
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { defineConfig } from "vite";
2+
3+
export default defineConfig({
4+
5+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default function vitePluginReplace() {
2+
return {
3+
name: "replace-plugin",
4+
};
5+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { defineConfig } from "vite";
2+
import replacePlugin from "./vite-plugin-replace";
3+
4+
export default defineConfig({
5+
plugins: [
6+
replacePlugin({
7+
from: "Initial value",
8+
to: "Replaced value",
9+
}),
10+
],
11+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
type: lesson
3+
title: Import plugin from file
4+
focus: /vite.config.ts
5+
---
6+
7+
# Vite Replace plugin
8+
9+
In this part we'll create a plugin that modifies our source code. This plugin will look for specific patterns in the source files and replace those with the configured value.
10+
11+
For example, with configuration of `{ from: "Initial value", to: "Replaced value" }` this plugin should replace all occurences of `"Initial value"` with `"Replaced value"`.
12+
13+
The plugin will also be in a separate file from our `vite.config.ts`.
14+
15+
```
16+
├── vite-plugin-replace.ts
17+
└── vite.config.ts
18+
```
19+
20+
To use this plugin we'll be importing it from the `vite-plugin-replace.ts` and passing it's return value to our Vite configuration's `plugins`.
21+
22+
```ts
23+
import replacePlugin from "./vite-plugin-replace";
24+
```
25+
26+
Options are passed to the plugin as function argument.
27+
28+
```ts
29+
replacePlugin({
30+
from: "Initial value",
31+
to: "Replaced value",
32+
});
33+
```
34+
35+
Import the plugin in `vite.config.ts` and pass it in `plugins`. The `vite-plugin-replace.ts` contains minimal plugin already.
36+
37+
Preview tab should indicate successful load of the plugin:
38+
39+
> Loaded Vite plugins: replace-plugin ✅
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mountHTML(`
2+
<div>
3+
Hello world!
4+
</div>
5+
`);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default function vitePluginReplace() {
2+
return {
3+
name: "replace-plugin",
4+
};
5+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { defineConfig } from "vite";
2+
import replacePlugin from "./vite-plugin-replace";
3+
4+
export default defineConfig({
5+
plugins: [
6+
replacePlugin({
7+
from: "Initial value",
8+
to: "Replaced value",
9+
}),
10+
],
11+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export default function vitePluginReplace() {
2+
return {
3+
name: "replace-plugin",
4+
transform(src, id) {
5+
if (id.includes("tutorial-example.js")) {
6+
return { code: "mountHTML('<h1>Changed!</h1>')" };
7+
}
8+
},
9+
};
10+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
type: lesson
3+
title: Defining transform hook
4+
focus: /vite-plugin-replace.ts
5+
---
6+
7+
# Transform hook
8+
9+
Modifying contents of a source code file is called **transforming** in Vite. To do this in a Vite plugin, we'll need to use `transform` hook.
10+
11+
```ts add={3}
12+
{
13+
name: "replace-plugin",
14+
transform(code, id) { }
15+
}
16+
```
17+
18+
This hooks is called with two options:
19+
20+
- `src` is the source code of the file, or the result of previous Vite plugin's transform
21+
- `id` is the name of the module
22+
23+
As return value Vite expects an object with various properties.
24+
In this tutorial we'll skip most of them and focus only on `code` property. This property should contain the transformed code that the plugin produces.
25+
26+
```ts
27+
{
28+
name: "replace-plugin",
29+
transform(src, id) {
30+
return { code: <transformed Javascript code> }
31+
}
32+
}
33+
```
34+
35+
In `tutorial-example.js` we have a helper function `mountHTML()`. It can be used to modify the page on preview tab. Try changing the contents of `tutorial-example.js` using `transform` hook so that `mountHTML` is called with `<h1>Changed!</h1>` instead.
36+
37+
> 💡 Note that `transform` hook is called for every single file that Vite processes. Make sure to specifically transform only `tutorial-example.js`. This can be detected by checking `id` for matches exactly as we did with the `load` hook.

0 commit comments

Comments
 (0)