-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.options.ts
103 lines (79 loc) · 2.32 KB
/
vite.options.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
99
100
101
102
103
import np from 'node:path';
import nfs from 'node:fs';
import { fileURLToPath } from 'node:url';
import { createLogger } from 'vite';
import type {
SketchData,
TransformOptions,
BuildSketchesOptions,
StaticAssetsOptions,
} from './vite.plugins.js';
const input = [
'2024-08-12-fusilli-animata',
'2024-07-23-fusilli',
'2022-07-12-emoji-pack'
] as const;
const project = fileURLToPath(new URL('./', import.meta.url));
const base = 'sketches';
const root = np.join(project, base);
const dirnameOut = 'dist';
const dirnamePublic = 'public';
const subDirnameStatic = 'static';
const subDirnameViews = 'views';
const outDirPath = np.join(project, dirnameOut);
const outSubDirPath = np.join(outDirPath, subDirnameViews);
const publicDirPath = np.join(project, dirnamePublic);
const customLogger = createLogger();
const logInfo = customLogger.info.bind(customLogger);
customLogger.info = (msg, opts) => {
if (msg.includes('production')) return;
logInfo(msg, opts);
};
export { base, root, outDirPath, publicDirPath, customLogger };
const data = createData();
export const transformOptions: TransformOptions = {
base,
subDir: subDirnameViews,
data,
};
export const bundleOptions: BuildSketchesOptions = {
outPath: outSubDirPath,
publicPath: publicDirPath,
subDirStatic: subDirnameStatic,
subDirViews: subDirnameViews,
base,
data,
};
export const assetsOptions: StaticAssetsOptions = {
srcPath: np.join(publicDirPath, subDirnameStatic),
outPath: np.join(outDirPath, subDirnameStatic),
};
function createData(): BuildSketchesOptions['data'] {
return input.map((folder) => toData(folder)).sort((a, b) => sortData(a, b));
}
function toData(folder: string): SketchData {
const match = folder.match(/^\d{4}-\d{1,2}-\d{1,2}/);
const entry = np.join(root, folder);
let name: string;
let date: string;
if (match) {
date = match[0];
name = folder.replace(`${date}-`, '');
} else {
const stat = nfs.existsSync(entry) ? nfs.lstatSync(entry) : null;
date = stat ? new Date(stat.birthtime).toISOString().split('T')[0] : '';
name = folder;
}
const outDir = np.join(outSubDirPath, name);
return {
root: entry,
outDir,
name,
date,
};
}
function sortData(a: SketchData, b: SketchData) {
const dateA = !!a.date ? new Date(a.date).valueOf() : 0;
const dateB = !!b.date ? new Date(b.date).valueOf() : 0;
return dateB - dateA;
}