-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite-plugin-examples-nav.js
More file actions
99 lines (86 loc) · 2.9 KB
/
vite-plugin-examples-nav.js
File metadata and controls
99 lines (86 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { resolve, dirname } from "node:path";
import { fileURLToPath } from "node:url";
import { copyFileSync, existsSync, mkdirSync } from "node:fs";
import { generateExamplesManifest, discoverExamples } from "./generate-examples-manifest.js";
const __dirname = dirname(fileURLToPath(import.meta.url));
function getExampleInputs() {
const examples = discoverExamples();
const inputs = {
main: resolve(__dirname, 'examples/index.html'),
};
examples.forEach(example => {
inputs[example.name] = example.path;
});
return inputs;
}
function copyManifestToPublic() {
const manifestPath = resolve(__dirname, "examples", "examples-manifest.json");
const publicDir = resolve(__dirname, "public");
const publicManifestPath = resolve(publicDir, "examples-manifest.json");
if (!existsSync(publicDir)) {
mkdirSync(publicDir);
}
copyFileSync(manifestPath, publicManifestPath);
}
function examplesManifestPlugin() {
let isExamplesBuild = false;
return {
name: "examples-navigation",
config(config, { command, mode }) {
if (command === "build" && mode === "examples") {
isExamplesBuild = true;
config.build = config.build || {};
config.build.rollupOptions = config.build.rollupOptions || {};
config.build.rollupOptions.input = getExampleInputs();
}
},
buildStart(_options) {
if (!isExamplesBuild) {
return;
}
generateExamplesManifest();
copyManifestToPublic();
console.log("Examples manifest generated and copied to public");
},
configureServer(server) {
const regenerateManifest = () => {
console.log("Regenerating examples manifest...");
try {
generateExamplesManifest();
copyManifestToPublic();
console.log("Manifest regenerated and copied to public");
} catch (error) {
console.error("Failed to regenerate manifest:", error);
}
};
regenerateManifest();
// watch the entire examples directory for changes (including new files/directories)
const fileWatcher = server.watcher;
const examplesDir = resolve(__dirname, "examples");
const distDir = resolve(__dirname, "examples", "dist");
fileWatcher.add(examplesDir);
fileWatcher.on("change", (path) => {
if (
path.startsWith(examplesDir) &&
path.endsWith("index.html") &&
!path.startsWith(distDir) &&
!path.endsWith("/examples/index.html") &&
!path.endsWith("examples-manifest.json")
) {
regenerateManifest();
}
});
fileWatcher.on("add", (path) => {
if (
path.startsWith(examplesDir) &&
path.endsWith("/index.html") &&
!path.startsWith(distDir) &&
!path.endsWith("examples-manifest.json")
) {
regenerateManifest();
}
});
},
};
}
export default examplesManifestPlugin;