-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathvite.config.ts
More file actions
130 lines (115 loc) · 3.82 KB
/
vite.config.ts
File metadata and controls
130 lines (115 loc) · 3.82 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
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
import mime from "mime";
import basicSSL from "@vitejs/plugin-basic-ssl";
import { glob } from "glob";
import { sveltekit } from "@sveltejs/kit/vite";
import { createSitemap } from "svelte-sitemap/src/index";
import { defineConfig, searchForWorkspaceRoot, type PluginOption } from "vite";
import { join, basename } from "node:path";
import { createReadStream } from "node:fs";
import { cp, readdir, mkdir } from "node:fs/promises";
const exposeLibAV: PluginOption = (() => {
const IMPUT_MODULE_DIR = join(__dirname, 'node_modules/@imput');
return {
name: "vite-libav.js",
configureServer(server) {
server.middlewares.use(async (req, res, next) => {
if (!req.url?.startsWith('/_libav/')) return next();
const filename = basename(req.url).split('?')[0];
if (!filename) return next();
const [file] = await glob(join(IMPUT_MODULE_DIR, '/**/dist/', filename));
if (!file) return next();
const fileType = mime.getType(filename);
if (!fileType) return next();
res.setHeader('Content-Type', fileType);
return createReadStream(file).pipe(res);
});
},
generateBundle: async (options) => {
if (!options.dir) {
return;
}
const assets = join(options.dir, '_libav');
await mkdir(assets, { recursive: true });
const modules = await readdir(IMPUT_MODULE_DIR).then(
modules => modules.filter(m => m.startsWith('libav.js'))
);
for (const module of modules) {
const distFolder = join(IMPUT_MODULE_DIR, module, 'dist/');
await cp(distFolder, assets, { recursive: true });
}
}
}
})();
const enableCOEP: PluginOption = {
name: "isolation",
configureServer(server) {
server.middlewares.use((_req, res, next) => {
res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
next();
})
}
};
const generateSitemap: PluginOption = {
name: "generate-sitemap",
async writeBundle(bundle) {
if (!process.env.WEB_HOST || !bundle.dir?.endsWith('server')) {
return;
}
await createSitemap(`https://${process.env.WEB_HOST}`, {
changeFreq: 'monthly',
outDir: '.svelte-kit/output/prerendered/pages',
resetTime: true
});
}
}
const checkDefaultApiEnv = (): PluginOption => ({
name: "check-default-api",
config() {
if (!process.env.WEB_DEFAULT_API) {
throw new Error(
"WEB_DEFAULT_API env variable is required, but missing."
);
}
},
});
export default defineConfig({
plugins: [
checkDefaultApiEnv(),
basicSSL(),
sveltekit(),
enableCOEP,
exposeLibAV,
generateSitemap
],
build: {
sourcemap: true,
rollupOptions: {
output: {
manualChunks: (id) => {
if (id.includes('/web/i18n') && id.endsWith('.json')) {
const lang = id.split('/web/i18n/')?.[1].split('/')?.[0];
if (lang) {
return `i18n_${lang}`;
}
}
}
}
}
},
server: {
headers: {
"Cross-Origin-Opener-Policy": "same-origin",
"Cross-Origin-Embedder-Policy": "require-corp"
},
fs: {
allow: [
searchForWorkspaceRoot(process.cwd())
]
},
proxy: {}
},
optimizeDeps: {
exclude: ["@imput/libav.js-remux-cli"]
},
});