-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.ts
153 lines (141 loc) · 3.92 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
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
import { Plugin, ResolvedConfig, UserConfig, defineConfig } from 'vite';
import { resolve, join, basename, dirname } from 'node:path';
import { promises as fs } from 'node:fs';
import history from 'connect-history-api-fallback';
import ejs from 'ejs';
import basicSsl from '@vitejs/plugin-basic-ssl';
function EjsPlugin(data: Record<string, any> = {}): Plugin {
let config: ResolvedConfig;
return {
name: 'vite-plugin-ejs',
// Get Resolved config
configResolved(resolvedConfig) {
config = resolvedConfig;
},
transformIndexHtml: {
enforce: 'pre',
async transform(html, ctx) {
const siteData = data[basename(ctx.filename)];
html = await ejs.render(
html,
{
NODE_ENV: config.mode,
isDev: config.mode === 'development',
...(siteData ?? {}),
},
{
views: ['./pages'],
async: false,
},
);
return html;
},
},
};
}
async function* walk(dir: string, recursive: boolean): AsyncGenerator<string, void, void> {
for await (const d of await fs.opendir(dir)) {
const entry = join(dir, d.name);
if (d.isDirectory() && recursive) yield* walk(entry, true);
else if (d.isFile()) yield entry;
}
return;
}
function MPAPlugin(dir: string): Plugin {
let files: Record<string, string> = {};
let localFiles: string[] = [];
let config: ResolvedConfig;
return {
name: 'vite-plugin-mpa',
enforce: 'pre',
async config(config) {
config.build = config.build || {};
config.build.rollupOptions = config.build.rollupOptions || {};
files = {};
localFiles = [];
for await (const p of walk(`./${dir}/`, false)) {
files[basename(p, '.html')] = resolve(__dirname, p);
localFiles.push(p);
}
config.build.rollupOptions.input = {
...files,
};
},
configResolved(resolvedConfig) {
config = resolvedConfig;
},
configureServer({ middlewares: app }) {
app.use(
history({
verbose: Boolean(process.env.DEBUG) && process.env.DEBUG !== 'false',
disableDotRule: undefined,
htmlAcceptHeaders: ['text/html', 'application/xhtml+xml'],
rewrites: [
{
from: /^\/$/,
to: `./${dir}/index.html`,
},
...localFiles
.sort((a, b) => {
if (a.startsWith(b)) {
return -1;
} else if (b.startsWith(a)) {
return 1;
} else {
return b.length - a.length || a.localeCompare(b);
}
})
.flatMap((pageName) => [
{
from: basename(pageName),
to: './' + pageName,
},
{
from: basename(pageName, '.html'),
to: './' + pageName,
},
]),
],
}),
);
},
async closeBundle() {
console.log(dir, config.build.outDir);
for (const file of localFiles) {
await fs.rename(`./${config.build.outDir}/${file}`, `./${config.build.outDir}/${file.substring(dir.length)}`);
}
await fs.rmdir(`./${config.build.outDir}/${dir}`);
},
};
}
export default defineConfig({
server: {
port: 4001,
proxy: {
'/api': 'http://127.0.0.1:4000/',
},
},
plugins: [
basicSsl(),
MPAPlugin('pages'),
EjsPlugin({
'send.html': {
title: 'LocalCast - Send',
navbarBadge: 'Sender',
},
'receive.html': {
title: 'LocalCast - Receive',
navbarBadge: 'Receiver',
},
}),
],
build: {
rollupOptions: {
// input: {
// index: resolve(__dirname, './index.html'),
// send: resolve(__dirname, './send.html'),
// receive: resolve(__dirname, './receive.html')
// }
},
},
});