Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: source map support to replace plugin #3

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@unocss/transformer-directives": "^0.59.4",
"astro": "^4.8.6",
"fast-glob": "^3.3.2",
"magic-string": "^0.30.10",
"prettier-plugin-astro": "^0.13.0",
"typescript": "^5.4.5",
"unocss": "^0.59.4",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mountHTML(`
<div>
Hello world!
</div>
`);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default function vitePluginReplace(options) {
return {
name: "replace-plugin",
transform(src, id) {
if (id.includes("tutorial-example.js")) {
return { code: src.replaceAll(options.from, options.to) };
}
},
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { defineConfig } from "vite";
import Inspect from "vite-plugin-inspect";
import replacePlugin from "./vite-plugin-replace";

export default defineConfig({
plugins: [
Inspect({
build: true,
outputDir: ".vite-inspect",
}),

replacePlugin({
from: "Initial value",
to: "Replaced value",
}),
],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import MagicString from "magic-string";

export default function vitePluginReplace(options) {
return {
name: "replace-plugin",
transform(src, id) {
if (id.includes("tutorial-example.js")) {
const s = new MagicString(src);
s.replaceAll(options.from, options.to);

return {
code: s.toString(),
map: s.generateMap({ hires: "boundary" }),
};
}
},
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { defineConfig } from "vite";
import Inspect from "vite-plugin-inspect";
import replacePlugin from "./vite-plugin-replace";

export default defineConfig({
plugins: [
Inspect({
build: true,
outputDir: ".vite-inspect",
}),

replacePlugin({
from: "Initial value",
to: "Replaced value",
}),
],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
type: lesson
title: Extra - Source map support
focus: /vite.config.ts
previews:
- [3000, "Vite plugin inspect"]
mainCommand: ["pnpm run /inspect/", "Inspect Vite build"]
prepareCommands:
- ["npm install", "Installing dependencies"]
- ["npm run build", "Pre-build"]
---

# Source map support
Loading