Skip to content

Commit 0ea072b

Browse files
committed
chore(dev): allow vite watch with symlinked bundle
1 parent eae527c commit 0ea072b

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

vite.config.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import fs from "node:fs";
12
import { builtinModules } from "node:module";
23
import path from "node:path";
34
import { svelte } from "@sveltejs/vite-plugin-svelte";
@@ -51,13 +52,43 @@ const external = [
5152

5253
export default defineConfig(({ mode }) => {
5354
const isProd = mode === "production";
55+
const outDir = isProd ? "." : "build";
56+
57+
const devSymlinkPlugin = () => ({
58+
name: "podnotes-dev-symlink",
59+
writeBundle() {
60+
if (isProd) return;
61+
62+
const ensureSymlink = (from: string, to: string) => {
63+
try {
64+
const stat = fs.lstatSync(to);
65+
if (stat.isSymbolicLink() || stat.isFile()) {
66+
fs.unlinkSync(to);
67+
}
68+
} catch {
69+
// target does not exist – that's fine
70+
}
71+
72+
fs.symlinkSync(from, to);
73+
};
74+
75+
const builtMain = path.resolve(__dirname, outDir, "main.js");
76+
const builtMap = path.resolve(__dirname, outDir, "main.js.map");
77+
const rootMain = path.resolve(__dirname, "main.js");
78+
const rootMap = path.resolve(__dirname, "main.js.map");
79+
80+
if (fs.existsSync(builtMain)) ensureSymlink(builtMain, rootMain);
81+
if (fs.existsSync(builtMap)) ensureSymlink(builtMap, rootMap);
82+
},
83+
});
5484

5585
return {
5686
plugins: [
5787
svelte({
5888
preprocess: sveltePreprocess(),
5989
compilerOptions: { css: "injected" },
6090
}),
91+
devSymlinkPlugin(),
6192
],
6293
resolve: {
6394
alias: {
@@ -72,7 +103,7 @@ export default defineConfig(({ mode }) => {
72103
fileName: () => "main.js",
73104
},
74105
target: "es2020",
75-
outDir: ".",
106+
outDir,
76107
emptyOutDir: false,
77108
sourcemap: !isProd,
78109
minify: isProd ? "esbuild" : false,

0 commit comments

Comments
 (0)