-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.ts
101 lines (89 loc) · 3.04 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
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig, type BuildOptions } from 'vite';
import path from 'path'
import fs from 'fs-extra'
import glob from 'glob'
import { fileURLToPath } from 'url';
import mime from 'mime-types'
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export default ({ mode }) => {
let build:BuildOptions = {}
// console.log("mode",{mode})
if(mode === 'production'){
// build.emptyOutDir = false;
}else if(mode === 'development'){
build.watch = {
include: 'src/**',
}
}
return defineConfig({
build,
server: {
fs: {
allow: ['data'],
},
},
plugins: [
{
name: 'prebuild-commands',
buildStart: async () => {
glob(path.join(__dirname,'work/**/*'), (err, files) => {
if (err) {
console.error(`Error globbing files: ${err}`);
return;
}
const knownDirs:string[] = [];
for(let filePath of files){
if(filePath.includes('!')) continue; //we assume this is part of a directory meant to be ignored
// console.log(`Processing file: ${filePath}`);
const relativePath = path.normalize(path.relative(path.join(__dirname,'work'),filePath))
// console.log(` : ${relativePath}`);
const baseName = path.basename(filePath);
const folderPath = path.join('static','projects',path.dirname(relativePath))
const fileType = baseName.includes('.') ? baseName.split('.').pop() : null;
if(!fileType) continue;
const mimeType = mime.lookup(fileType)
if(!mimeType) continue;
const mediaType = mimeType.split('/')[0];
// if(['image','video'].includes(mediaType)){
if(['video'].includes(mediaType)){
if(!knownDirs.find(dir=>dir.indexOf(folderPath)===0)){//not sure of exists
if(fs.existsSync(folderPath)){
}else{
fs.mkdirSync(folderPath, { recursive: true });
}
knownDirs.push(folderPath);
}
fs.ensureSymlinkSync(filePath, path.join(folderPath,baseName))
}
}
});
console.log('Clearing old build files.')
glob(path.join(__dirname,'build/**/*'), (err, files) => {
for(let filePath of files){
if(filePath.includes('.git')) continue;
// console.log(`Processing file: ${filePath}`);
const relativePath = path.normalize(path.relative(path.join(__dirname,'build'),filePath))
// console.log(` : ${relativePath}`);
const baseName = path.basename(filePath);
const folderPath = path.join('build',path.dirname(relativePath))
const fileType = baseName.includes('.') ? baseName.split('.').pop() : null;
if(!fileType) continue;
const mimeType = mime.lookup(fileType)
if(!mimeType) continue;
const mediaType = mimeType.split('/')[0];
if(['image','video'].includes(mediaType)){
}else{
fs.unlink(filePath);
console.log('Deleted '+ relativePath)
}
}
});
console.log('prebuild done')
}
},
sveltekit(),
]
});
}