Skip to content

Commit c5ec7a0

Browse files
committed
chore: changes examples build setup
1 parent 531aa9c commit c5ec7a0

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

vite.config-examples.ts

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,54 @@ const restructureExamples = () => {
1313
const paths: string[] = [];
1414

1515
for (const file of files) {
16+
const directory = path.dirname(file);
17+
const rootFolder = directory.split(path.sep)[0];
18+
19+
let targetDirectory: string | undefined;
20+
let assetsPath: string | undefined;
21+
let resourcesPath: string | undefined;
22+
23+
const split = file.split("examples");
24+
if (split.length === 3 && split[1] && split[2]) {
25+
const baseName = path.basename(split[1]);
26+
const dirName = path.dirname(split[2]);
27+
const dir = path.join(baseName, dirName);
28+
targetDirectory = path.join(rootFolder, dir);
29+
assetsPath = "../../assets";
30+
resourcesPath = "../../../resources";
31+
}
32+
33+
if (split.length === 2) {
34+
const exampleName = path.basename(directory);
35+
targetDirectory = path.join(rootFolder, exampleName);
36+
assetsPath = "../assets";
37+
resourcesPath = "../../resources";
38+
}
39+
40+
if (!(targetDirectory && assetsPath && resourcesPath)) continue;
41+
1642
const urlPath = file
17-
.split("examples")[1]
18-
.slice(1)
43+
// .split("examples")[1]
44+
// .slice(1)
45+
.slice(9)
1946
.replace(".html", ".ts")
2047
.replace(/\\/g, "/");
2148
paths.push(urlPath);
22-
const directory = path.dirname(file);
23-
const exampleName = path.basename(directory);
24-
const rootFolder = directory.split(path.sep)[0];
25-
const targetDirectory = path.join(rootFolder, exampleName);
49+
2650
if (!fs.existsSync(targetDirectory)) fs.mkdirSync(targetDirectory);
2751

2852
const buffer = fs.readFileSync(file);
2953
const newBuffer = buffer
3054
.toString()
31-
.replace(/(\.\.\/)+assets/g, "../assets")
32-
.replace(/(\.\.\/)+resources/g, "../../resources");
55+
.replace(/(\.\.\/)+assets/g, assetsPath)
56+
.replace(/(\.\.\/)+resources/g, resourcesPath);
3357
fs.writeFileSync(path.join(targetDirectory, "index.html"), newBuffer);
3458
}
3559

3660
if (fs.existsSync(outDir)) fs.rmSync(outDir, { recursive: true });
3761
fs.writeFileSync(
3862
path.join("examples", "paths.json"),
39-
JSON.stringify(paths),
63+
JSON.stringify(paths, null, 2),
4064
);
4165
},
4266
};

0 commit comments

Comments
 (0)