-
Notifications
You must be signed in to change notification settings - Fork 14
/
vite.config-examples.ts
98 lines (85 loc) · 2.84 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
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
/* 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 directory = path.dirname(file);
const rootFolder = directory.split(path.sep)[0];
let targetDirectory: string | undefined;
let assetsPath: string | undefined;
let resourcesPath: string | undefined;
const split = file.split("examples");
if (split.length === 3 && split[1] && split[2]) {
const baseName = path.basename(split[1]);
const dirName = path.dirname(split[2]);
const dir = path.join(baseName, dirName);
targetDirectory = path.join(rootFolder, dir);
assetsPath = "../../assets";
resourcesPath = "../../../resources";
}
if (split.length === 2) {
const exampleName = path.basename(directory);
targetDirectory = path.join(rootFolder, exampleName);
assetsPath = "../assets";
resourcesPath = "../../resources";
}
if (!(targetDirectory && assetsPath && resourcesPath)) continue;
const urlPath = file
// .split("examples")[1]
// .slice(1)
.slice(9)
.replace(".html", ".ts")
.replace(/\\/g, "/");
paths.push(urlPath);
if (!fs.existsSync(targetDirectory)) fs.mkdirSync(targetDirectory);
const buffer = fs.readFileSync(file);
const newBuffer = buffer
.toString()
.replace(/(\.\.\/)+assets/g, assetsPath)
.replace(/(\.\.\/)+resources/g, resourcesPath);
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, null, 2),
);
},
};
};
const entries = globSync("packages/**/src/**/example.html").map(
(file: string) => {
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()],
});