-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvite.config.ts
122 lines (108 loc) · 3.79 KB
/
vite.config.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { readdirSync } from "node:fs"
import { resolve as resolvePath } from "node:path"
import { getLastCommit } from "git-last-commit"
import { defineConfig } from "vite"
import { checker } from "vite-plugin-checker"
import handlebars from "vite-plugin-handlebars"
import { ViteMinifyPlugin } from "vite-plugin-minify"
import tsconfigPaths from "vite-tsconfig-paths"
import type { Commit } from "git-last-commit"
import type { Plugin as VitePlugin } from "vite"
/**
* html内で{{hoge}}とか{{fuga}}とかをグローバルに参照するためのkv
*/
const context = {}
const readdirNested = (baseDir: string, dirName: string) =>
readdirSync(resolvePath(baseDir, dirName)).map((file) => `${dirName}/${file}`)
const allFiles = [
...readdirSync(resolvePath(__dirname, "src/pages")),
...readdirNested("src/pages", "contents"),
...readdirNested("src/pages", "private"),
...readdirNested("src/pages", "sc-problems"),
]
const htmlFiles = allFiles.filter(
// .htmうごかないかも
(file) => file.endsWith(".html") || file.endsWith(".htm"),
)
const inputFiles: { [key: string]: string } = {}
for (const htmlFile of htmlFiles) {
// 拡張子とってkvにするよ
inputFiles[htmlFile.endsWith("l") ? htmlFile.slice(0, -5) : htmlFile.slice(0, -4)] = resolvePath(
__dirname,
`src/pages/${htmlFile}`,
)
}
export default defineConfig(async ({ mode }) => {
const lastCommit = await new Promise<Commit>((resolve, reject) => {
getLastCommit((err: Error | undefined, commit) => {
if (err) reject(err)
else resolve(commit)
})
})
const commitDate = new Date(+lastCommit.committedOn * 1000)
const formatDateTime = (date: Date) => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
return `${year}/${month}/${day} ${hour}:${minute}:${second}`
}
const commitContext = {
commit_date: formatDateTime(commitDate),
commit_message: lastCommit.subject,
}
return {
root: "src/pages",
base: "/",
publicDir: resolvePath(__dirname, "src/public"),
build: {
emptyOutDir: true,
outDir: "../../dist", // /dist
rollupOptions: {
input: inputFiles,
output: {
chunkFileNames: "assets/[name].js",
entryFileNames: "assets/[name].js",
assetFileNames: "assets/[name].[ext]",
},
},
sourcemap: mode === "development",
},
server: {
watch: {
// WSL2の場合はこれが必要
usePolling: true,
},
},
resolve: {
alias: [{ find: "@", replacement: resolvePath(__dirname, "src") }],
},
plugins: [
tsconfigPaths({
root: resolvePath(__dirname, "src"),
}),
handlebars({
partialDirectory: resolvePath(__dirname, "src/pages/components"),
context: {
...context,
...commitContext,
},
}) as unknown as VitePlugin,
checker({
typescript: { root: resolvePath(__dirname, "src") },
eslint: {
lintCommand: `eslint --cache-location=${resolvePath(
__dirname,
".eslintcache",
)} --cache ${resolvePath(__dirname, "src")}/**/*.{js,ts}`,
},
}),
ViteMinifyPlugin({}),
],
esbuild: {
target: "es2016",
},
}
})