-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvite.config.ts
87 lines (76 loc) · 2.26 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
import react from '@vitejs/plugin-react';
import { defineConfig } from 'vite';
import { ViteStaticCopyOptions, viteStaticCopy } from 'vite-plugin-static-copy';
//import dts from 'vite-plugin-dts';
const isProduction = process.env.NODE_ENV == 'production';
const scriptSearch = '[BOOT_SCRIPT_MODULE]';
const moduleSearch = '[BOOT_LINK_MODULE]';
/**
* Coleta as tag scripts e adicionado em uma variavel do json no HTML do projeto
* @returns
*/
const jsToBottomNoModule = () => {
return {
name: 'no-attribute',
transformIndexHtml(html: any) {
const jss = [];
const modules = [];
//Procura as tags scripts, coleta a propriedade src e remove a tag
for (const matches of html.matchAll(/<script[^>]*src=['"](.*)['"][^>]*>(.*?)<\/script[^>]*>/g)) {
const tag = matches[0];
const src = matches[1];
if (src.includes('bootloader.js')) {
continue;
}
jss.push(src);
html = html.replace(tag, '');
}
//Procura as tags scripts, coleta a propriedade src e remove a tag
for (const matches of html.matchAll(/<link[^>]*rel=['"]modulepreload['"][^>]*href=['"](.*)['"][^>]*>/g)) {
const tag = matches[0];
const href = matches[1];
modules.push(href);
html = html.replace(tag, '');
}
return html //
.replace(scriptSearch, JSON.stringify(jss))
.replace(moduleSearch, JSON.stringify(modules));
},
};
};
const staticCopy: ViteStaticCopyOptions = {
targets: [{ src: 'src/bootloader.js', dest: 'src' }],
};
const clearProdBootLoaderVariables = () => {
return {
name: 'no-attribute',
transformIndexHtml(html: any) {
return html //
.replace(scriptSearch, '[]')
.replace(moduleSearch, '[]');
},
};
};
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
//
react(),
//dts(),
viteStaticCopy(staticCopy),
isProduction ? jsToBottomNoModule() : clearProdBootLoaderVariables(),
],
build: {
target: 'es2020',
rollupOptions: {
output: {
manualChunks: {
monacoeditor: ['@monaco-editor/react'],
opencvts: ['opencv-ts'],
},
entryFileNames: '[name]_[hash].js',
chunkFileNames: 'deps/[name].js',
},
},
},
});