-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
101 lines (81 loc) · 2.89 KB
/
Copy pathvite.config.ts
File metadata and controls
101 lines (81 loc) · 2.89 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
100
101
import { defineConfig } from 'vite';
import path from 'path';
import fs from 'fs';
import config from "./config.json";
import { buildStaging } from "./buildStaging";
import { addFinal } from './addFinal';
import { rmStaging } from './rmStaging';
const rootDir = config.stagingHtmlDir;
/**
* get a list for file entries for Vite to complie (with sub-direcories included)
*/
function getHtmlEntries(dir: string, base = dir, entries: Record<string, string> = {}): Record<string, string> {
for (const file of fs.readdirSync(dir)) {
const fullPath = path.join(dir, file);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) getHtmlEntries(fullPath, base, entries);
else if (file.endsWith('.html')) {
if (
file.startsWith('_template')
) continue;
const name = path
.relative(base, fullPath)
.replace(/\\/g, '/')
.replace(/\.html$/, '');
entries[name] = '/' + name + '.html';
}
}
return entries;
}
// build staging dir
await buildStaging();
// cleanup when closing (only for building--developemnt process must run the rmStage script)
switch (process.env.NODE_ENV) {
case "production":
process.on('exit', () => {
addFinal();
rmStaging();
// process.exit(0);
});
break;
case undefined:
console.log('what.');
break;
default:
console.log(`⚠ staging dir was made--make sure to run "npm run rmStage" to manually remove the staging folder.`);
['SIGINT', 'SIGTERM', 'SIGQUIT', 'SIGHUP', 'exit'].forEach(signal => {
process.on(signal, () => {
if (signal != "exit") console.log(`⚠ please run "npm run rmStage" to manually remove the staging folder.`);
process.exit(0);
});
});
break;
}
// vite config
export default defineConfig({
root: rootDir,
build: {
outDir: path.resolve(__dirname, config.outDir),
emptyOutDir: true,
watch: null,
rollupOptions: {
input: getHtmlEntries(path.resolve(__dirname, rootDir)),
output: {
entryFileNames: 'src/[name].js',
chunkFileNames: 'src/[name].js',
assetFileNames: (asset) => {
switch (true) {
case asset.names[0].endsWith('.css'):
return 'style/[name][extname]';
case asset.names[0].endsWith('.png'):
case asset.names[0].endsWith('.jpg'):
case asset.names[0].endsWith('.gif'):
return 'img/[name][extname]';
default:
return 'assets/[name][extname]';
}
},
},
}
}
});