-
Notifications
You must be signed in to change notification settings - Fork 138
/
vite.config-examples.ts
72 lines (65 loc) · 2.05 KB
/
vite.config-examples.ts
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
/* eslint-disable import/no-extraneous-dependencies */
import { defineConfig } from "vite";
import * as path from "path";
import * as fs from "fs";
import { globSync } from "glob";
const restructureExamples = () => {
return {
name: "examples-refactor",
async writeBundle() {
const outDir = "examples/packages";
const files = globSync(`${outDir}/**/example.html`);
const paths: string[] = [];
for (const file of files) {
const urlPath = file
.split("examples")[1]
.slice(1)
.replace(".html", ".ts")
.replace(/\\/g, "/");
paths.push(urlPath);
const directory = path.dirname(file);
const exampleName = path.basename(directory);
const rootFolder = directory.split(path.sep)[0];
const targetDirectory = path.join(rootFolder, exampleName);
if (!fs.existsSync(targetDirectory)) fs.mkdirSync(targetDirectory);
const buffer = fs.readFileSync(file);
const newBuffer = buffer
.toString()
.replace(/(\.\.\/)+assets/g, "../assets")
.replace(/(\.\.\/)+resources/g, "../../resources");
fs.writeFileSync(path.join(targetDirectory, "index.html"), newBuffer);
}
if (fs.existsSync(outDir)) fs.rmSync(outDir, { recursive: true });
fs.writeFileSync(
path.join("examples", "paths.json"),
JSON.stringify(paths),
);
},
};
};
const entries = globSync("packages/**/src/**/example.html").map((file) => {
const directory = path.dirname(file);
const exampleName = path.basename(directory);
const fixedName = exampleName[0].toLowerCase() + exampleName.slice(1);
const entry = [fixedName, path.resolve(file)];
return entry;
});
const input = Object.fromEntries(entries);
export default defineConfig({
base: "./",
esbuild: {
supported: {
"top-level-await": true,
},
},
build: {
outDir: "./examples",
rollupOptions: {
input,
output: {
entryFileNames: "assets/[name].js",
},
},
},
plugins: [restructureExamples()],
});