-
Notifications
You must be signed in to change notification settings - Fork 1
/
vite.config.mjs
133 lines (128 loc) · 3.4 KB
/
vite.config.mjs
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
127
128
129
130
131
132
133
import fs from "fs";
import { resolve } from "path";
import { defineConfig } from "vite";
import handlebars from "vite-plugin-handlebars";
const componentsPropsConfig = {
link: ["href", "rel", "target"],
button: ["disabled", "onclick", "type"],
input: ["disabled", "name", "placeholder", "required", "type", "value"],
select: ["disabled", "name", "placeholder", "required", "value"],
textarea: ["disabled", "name", "placeholder", "required", "value"],
};
const createEntryPoints = () => {
const fileNameList = fs.readdirSync(resolve(__dirname, "./src/"));
const htmlFileList = fileNameList.filter((file) => /.html$/.test(file));
const inputFiles = {};
for (let i = 0; i < htmlFileList.length; i++) {
const file = htmlFileList[i];
inputFiles[file.slice(0, -5)] = resolve(__dirname, "./src/" + file);
}
return inputFiles;
};
const metadata = JSON.parse(
fs.readFileSync(resolve(__dirname, "./src/metadata.json")),
);
export default defineConfig({
server: {
host: true,
},
base: "",
root: "./src",
publicDir: "public",
build: {
outDir: "../docs",
rollupOptions: {
external:["gsap", 'gsap/ScrollTrigger', "gsap/Draggable"],
output: {
assetFileNames: (assetInfo) => {
let extType = assetInfo.name.split(".").at(-1);
if (/ttf|otf|eot|woff|woff2/i.test(extType)) {
extType = "fonts";
}
if (/png|jpe?g|svg|gif|tiff|webp|bmp|ico/i.test(extType)) {
extType = "images";
}
return `assets/${extType}/[name][extname]`;
},
chunkFileNames: "assets/scripts/[name].js",
entryFileNames: "assets/scripts/[name].js",
},
input: createEntryPoints(),
},
},
plugins: [
handlebars({
partialDirectory: resolve(__dirname, "./src/components"),
context(pagePath) {
return metadata[pagePath];
},
helpers: {
arr: (...args) => {
return Array.from(args).slice(0, args.length - 1);
},
attributes: (component, attributes, root) => {
if (
Array.isArray(componentsPropsConfig[component]) &&
componentsPropsConfig[component].length
) {
return Object.entries(attributes)
.filter(([key, value]) => {
return (
componentsPropsConfig[component].includes(key) &&
["string", "boolean", "number"].includes(typeof value) &&
(!(key in root) || root[key] !== value)
);
})
.map(([key, value]) => `${key}="${value}"`)
.join("\n");
} else {
console.warn(`No component config found for "${component}"`);
}
return null;
},
classes: (...classes) => {
return classes
.filter((c) => typeof c === "string" && c.trim().length > 0)
.join(" ");
},
concat: (...strings) => {
return strings.filter((s) => typeof s === "string" && !!s).join("");
},
ifCond: (cond, value) => {
if (cond) return value;
return null;
},
obj: (obj) => {
return obj.hash;
},
op: (v1, operator, v2) => {
switch (operator) {
case "===":
return v1 === v2;
case "!==":
return v1 !== v2;
case "<":
return v1 < v2;
case "<=":
return v1 <= v2;
case ">":
return v1 > v2;
case ">=":
return v1 >= v2;
case "&&":
return v1 && v2;
case "||":
return v1 || v2;
case "??":
return v1 ?? v2;
default:
return false;
}
},
json: (obj) => {
return JSON.stringify(obj);
},
},
}),
],
});