-
Notifications
You must be signed in to change notification settings - Fork 7
/
vite.config.js
210 lines (194 loc) · 6.06 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* eslint-disable no-undef */
import path from 'path'
import { fileURLToPath } from 'url'
import { defineConfig } from 'vite'
import assert from 'assert'
import react from '@vitejs/plugin-react-swc'
import getConfig from './libs/config.js'
import { rmdir } from './libs/file.js'
import Music from './libs/music.js';
import Background from './libs/background.js'
import directory from './libs/directory.js'
import OfficalInfo from './libs/offical_info.js';
global.__projectRoot = path.dirname(fileURLToPath(import.meta.url))
class ViteRunner {
#officalInfo = new OfficalInfo()
#globalConfig = getConfig(this.#officalInfo)
#mode
#baseViteConfig = {
configFile: false,
base: "",
server: {
host: '0.0.0.0',
},
build: {
emptyOutDir: false,
},
}
get config() {
let result;
const temp = process.env.npm_lifecycle_event.split(':')
const runType = temp[0]
this.#mode = temp[1]
switch (runType) {
case 'directory':
result = {
data: this.#directoryConfig,
}
if (this.#mode !== 'preview') {
rmdir(path.resolve(__projectRoot, this.#globalConfig.folder.release, "_directory"))
rmdir(path.resolve(__projectRoot, this.#globalConfig.folder.release, "index.html"))
}
break
case 'operator':
result = {
data: this.#operatorConfig,
}
break
default:
return null
}
return result
}
async start() {
const configObj = this.config
const viteConfig = configObj.data;
switch (this.#mode) {
case 'dev':
this.#dev(viteConfig)
break
case 'build':
case 'build-all':
this.#build(viteConfig)
break
case 'preview':
this.#preview(viteConfig)
break
default:
return
}
}
#dev(viteConfig) {
; (async () => {
const { createServer } = await import('vite')
const server = await createServer(viteConfig)
await server.listen()
server.printUrls()
})()
}
#build(viteConfig) {
; (async () => {
const { build } = await import('vite')
await build({
...viteConfig,
logLevel: 'silent',
})
})()
}
#preview(viteConfig) {
; (async () => {
const { preview } = await import('vite')
const previewServer = await preview({
...viteConfig,
})
previewServer.printUrls()
})()
}
get #operatorConfig() {
const operatorName = process.env.O || process.argv[3]
assert(operatorName, 'Please set the operator name by using environment variable O.')
const assetsDir = 'assets'
return {
...this.#baseViteConfig,
envDir: path.join(__projectRoot, this.#globalConfig.folder.operator, operatorName),
publicDir: path.resolve(__projectRoot, this.#globalConfig.folder.release, operatorName),
root: path.resolve(__projectRoot, this.#globalConfig.folder.showcase_src),
server: {
...this.#baseViteConfig.server,
},
resolve: {
alias: {
'@': path.resolve(__projectRoot, this.#globalConfig.folder.showcase_src, './src'),
'!': path.resolve(__projectRoot, this.#globalConfig.folder.operator, operatorName),
},
},
build: {
...this.#baseViteConfig.build,
chunkSizeWarningLimit: 10000,
outDir: path.resolve(__projectRoot, this.#globalConfig.folder.release, operatorName),
rollupOptions: {
output: {
entryFileNames: `${assetsDir}/[name].js`,
chunkFileNames: `${assetsDir}/[name].js`,
assetFileNames: `${assetsDir}/[name].[ext]`,
}
}
},
}
}
get #directoryConfig() {
if (process.env.npm_lifecycle_event === 'directory:build') {
global.__config = this.#globalConfig
}
const directoryDir = path.resolve(__projectRoot, this.#globalConfig.folder.directory_src)
const assetsDir = '_directory'
return {
...this.#baseViteConfig,
envDir: directoryDir,
base: "/",
plugins: [
react(),
],
publicDir: path.resolve(__projectRoot, this.#globalConfig.folder.release),
root: directoryDir,
server: {
...this.#baseViteConfig.server,
},
resolve: {
alias: {
'@': path.resolve(directoryDir, './src'),
'!': path.resolve(__projectRoot, this.#globalConfig.folder.showcase_src, './src'),
},
},
build: {
...this.#baseViteConfig.build,
outDir: path.resolve(__projectRoot, this.#globalConfig.folder.release),
rollupOptions: {
output: {
entryFileNames: `${assetsDir}/[name]-[hash:8].js`,
chunkFileNames: `${assetsDir}/[name]-[hash:8].js`,
assetFileNames: `${assetsDir}/[name]-[hash:8].[ext]`,
manualChunks: (id) => {
if (id.includes("node_modules")) {
return "vendor"; // all other package goes here
} else if (id.includes("directory/src") && id.includes(".json")) {
return "assets";
}
},
}
}
},
}
}
}
async function main() {
if (process.env.npm_lifecycle_event.includes('directory')) return;
const runner = new ViteRunner()
await runner.start()
}
main()
export default defineConfig(async () => {
if (process.env.npm_lifecycle_event.includes('directory')) {
const officalInfo = new OfficalInfo()
global.__config = getConfig(officalInfo)
const OPERATOR_SOURCE_FOLDER = path.join(__projectRoot, __config.folder.operator)
const OPERATOR_SOURCE_DATA_FOLDER = path.join(__projectRoot, __config.folder.operator_data)
const OPERATOR_SHARE_FOLDER = path.join(OPERATOR_SOURCE_DATA_FOLDER, __config.folder.share)
const background = new Background(OPERATOR_SHARE_FOLDER, OPERATOR_SOURCE_FOLDER)
await background.process()
const backgrounds = ['operator_bg.png', ...background.files]
const { musicMapping } = (new Music()).copy(OPERATOR_SHARE_FOLDER)
directory(OPERATOR_SOURCE_DATA_FOLDER, { backgrounds, musicMapping })
}
return (new ViteRunner()).config.data
})