-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.js
126 lines (112 loc) · 3.16 KB
/
vite.config.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { defineConfig } from "vite";
import { resolve } from "path";
import { nodePolyfills } from "vite-plugin-node-polyfills";
import path from "path";
import fs from "fs";
/**
* Read all HTML Files
*/
const buildDir = "temp";
const directoriesToCopy = [
{ source: "pagefind", target: "pagefind" },
{ source: "models", target: "models" },
{ source: "img", target: "img" },
];
/**
* Common function to process files in a directory
*/
const processFilesInDir = (dir, callback) => {
const files = fs.readdirSync(dir);
files.forEach((file) => {
const filePath = resolve(dir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
processFilesInDir(filePath, callback);
} else if (stat.isFile()) {
callback(filePath);
}
});
};
/**
* Get HTML entries
*/
const getHtmlEntries = (dir) => {
const entries = {};
processFilesInDir(dir, (filePath) => {
if (filePath.endsWith(".html")) {
const name = path.relative(buildDir, filePath).replace(/\\/g, "/").replace(".html", "");
entries[name] = filePath;
}
});
return entries;
};
/**
* Recurvice copy directory
*/
const copyDirectoryRecursive = (source, target) => {
if (!fs.existsSync(target)) {
fs.mkdirSync(target, { recursive: true });
}
const entries = fs.readdirSync(source, { withFileTypes: true });
entries.forEach((entry) => {
const sourcePath = path.join(source, entry.name);
const targetPath = path.join(target, entry.name);
if (entry.isDirectory()) {
copyDirectoryRecursive(sourcePath, targetPath);
} else if (entry.isFile()) {
fs.copyFileSync(sourcePath, targetPath);
}
});
};
const input = getHtmlEntries(buildDir);
/**
* Vite plugin to copy content files
*/
const copyFilesPlugin = () => {
return {
name: "copy-files-plugin",
buildStart() {
// xml and txt files
processFilesInDir(buildDir, (filePath) => {
if (filePath.endsWith(".xml") || filePath.endsWith(".txt")) {
const targetPath = resolve("build", path.relative(buildDir, filePath));
fs.mkdirSync(path.dirname(targetPath), { recursive: true });
fs.copyFileSync(filePath, targetPath);
}
});
// Copy directories
directoriesToCopy.forEach(({ source, target }) => {
const sourceDir = resolve(buildDir, source);
const targetDir = resolve("build", target);
if (fs.existsSync(sourceDir)) {
copyDirectoryRecursive(sourceDir, targetDir);
console.log(`Copied '${source}' directory to ${targetDir}`);
} else {
console.warn(`Source directory '${sourceDir}' does not exist.`);
}
});
},
};
};
export default defineConfig({
root: buildDir,
watch: {
include: ["static/**"],
},
css: {
postcss: "./postcss.config.js",
},
build: {
outDir: resolve(__dirname, "build"),
rollupOptions: {
input,
external: ["/pagefind/pagefind.js"],
output: {
entryFileNames: "assets/[name]-[hash].js",
chunkFileNames: "assets/[name]-[hash].js",
assetFileNames: "assets/[name]-[hash][extname]",
},
},
},
plugins: [nodePolyfills(), copyFilesPlugin()],
});