Skip to content

Commit a76d79c

Browse files
committed
feat: source map support to replace plugin
1 parent 32607e7 commit a76d79c

File tree

10 files changed

+3695
-121
lines changed

10 files changed

+3695
-121
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"@unocss/transformer-directives": "^0.59.4",
2929
"astro": "^4.8.6",
3030
"fast-glob": "^3.3.2",
31+
"magic-string": "^0.30.10",
3132
"prettier-plugin-astro": "^0.13.0",
3233
"typescript": "^5.4.5",
3334
"unocss": "^0.59.4",

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mountHTML(`
2+
<div>
3+
Hello world!
4+
</div>
5+
`);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export default function vitePluginReplace(options) {
2+
return {
3+
name: "replace-plugin",
4+
transform(src, id) {
5+
if (id.includes("tutorial-example.js")) {
6+
return { code: src.replaceAll(options.from, options.to) };
7+
}
8+
},
9+
};
10+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { defineConfig } from "vite";
2+
import Inspect from "vite-plugin-inspect";
3+
import replacePlugin from "./vite-plugin-replace";
4+
5+
export default defineConfig({
6+
plugins: [
7+
Inspect({
8+
build: true,
9+
outputDir: ".vite-inspect",
10+
}),
11+
12+
replacePlugin({
13+
from: "Initial value",
14+
to: "Replaced value",
15+
}),
16+
],
17+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import MagicString from "magic-string";
2+
3+
export default function vitePluginReplace(options) {
4+
return {
5+
name: "replace-plugin",
6+
transform(src, id) {
7+
if (id.includes("tutorial-example.js")) {
8+
const s = new MagicString(src);
9+
s.replaceAll(options.from, options.to);
10+
11+
return {
12+
code: s.toString(),
13+
map: s.generateMap({ hires: "boundary" }),
14+
};
15+
}
16+
},
17+
};
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { defineConfig } from "vite";
2+
import Inspect from "vite-plugin-inspect";
3+
import replacePlugin from "./vite-plugin-replace";
4+
5+
export default defineConfig({
6+
plugins: [
7+
Inspect({
8+
build: true,
9+
outputDir: ".vite-inspect",
10+
}),
11+
12+
replacePlugin({
13+
from: "Initial value",
14+
to: "Replaced value",
15+
}),
16+
],
17+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
type: lesson
3+
title: Extra - Source map support
4+
focus: /vite.config.ts
5+
previews:
6+
- [3000, "Vite plugin inspect"]
7+
mainCommand: ["pnpm run /inspect/", "Inspect Vite build"]
8+
prepareCommands:
9+
- ["npm install", "Installing dependencies"]
10+
- ["npm run build", "Pre-build"]
11+
---
12+
13+
# Source map support

0 commit comments

Comments
 (0)