-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvite.config.js
More file actions
58 lines (54 loc) · 1.51 KB
/
vite.config.js
File metadata and controls
58 lines (54 loc) · 1.51 KB
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
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import fs from 'fs';
import formidable from 'formidable';
import { resolve, join } from 'path';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
vue(),
fileList(),
]
})
function fileList() {
return {
name: 'vite-plugin-svg-png-files',
configureServer(server) {
server.middlewares.use((req, res, next) => {
const url = req.url ;
if (
(url.startsWith('/svg/') || url.startsWith('/png/')) &&
url.endsWith('/')
) {
const pwd = decodeURI(join(__dirname, 'public', url));
const files = fs.readdirSync(pwd, {
withFileTypes: true,
});
const list= [];
for (const item of files) {
if (item.isDirectory()) {
list.push({ name: item.name, type: 'directory' });
} else {
list.push({ name: item.name });
}
}
res.end(JSON.stringify(list));
} else if (url === '/img' && req.method === 'POST') {
const form = formidable({
uploadDir: decodeURI(join(__dirname, 'public', '/img')),
keepExtensions: true,
});
form.parse(req, (err, fields, files) => {
if (!err) {
res.end(
JSON.stringify({ url: '/img/' + files.file.newFilename })
);
}
});
} else {
next();
}
});
},
};
}