-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.ts
99 lines (89 loc) · 2.88 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
import { defineConfig } from 'vite';
import { fileURLToPath } from 'url';
import preact from '@preact/preset-vite';
const iifeWrap = () => {
return {
name: 'iife-wrap-content',
generateBundle(code, bundle) {
const codeBundle = bundle['assets/content.js'];
codeBundle['code'] = `(() => {${codeBundle['code']}})();`;
},
};
};
const replaceCode = () => {
return {
name: 'replace-code',
generateBundle(code, bundle) {
const inlinedFiles = ['storage.js'];
const storedCode = {};
for (const key of inlinedFiles) {
const code = bundle['assets/' + key].code;
const exportRx = /export\{([a-z]+ as [a-z]+,?)+\};/;
const exports = code.match(exportRx);
const exportVars = exports[0].matchAll(/([a-z]+) as ([a-z]+),?/g);
const outVars = [];
const returnStrings = [];
for (const v of exportVars) {
const internal = v[1];
const out = v[2];
returnStrings.push(`${out}:${internal}`);
outVars.push(out);
}
const fullCode = `(() => {${code.replace(
exportRx,
''
)}return {${returnStrings.join(',')}}})();`;
storedCode[key] = fullCode;
}
for (const key of inlinedFiles) {
// eslint-disable-next-line no-useless-escape, prettier/prettier
const importRx = new RegExp(`import\{([a-z]+ as [a-z]+,?)+\}from"\.\/${key}";`);
for (const bundleKey of Object.keys(bundle)) {
if (!bundle[bundleKey].code) {
continue;
}
const code = bundle[bundleKey].code;
let stored = storedCode[key];
const imports = code.match(importRx);
if (imports) {
const importVars = imports[0].matchAll(/([a-z]+) as ([a-z]+),?/g);
const importStrings = [];
for (const v of importVars) {
const out = v[1];
const exposed = v[2];
importStrings.push(`${out}: ${exposed}`);
}
stored = `const {${importStrings.join(',')}}=${stored}`;
}
bundle[bundleKey].code = code.replace(importRx, stored);
}
}
},
};
};
// https://vitejs.dev/config/
export default defineConfig({
plugins: [preact()],
build: {
// sourcemap: 'inline',
rollupOptions: {
preserveEntrySignatures: 'strict',
input: {
popup: fileURLToPath(
new URL('./src/popup/index.html', import.meta.url)
),
inject: fileURLToPath(new URL('./src/inject.ts', import.meta.url)),
content: fileURLToPath(
new URL('./src/content/index.ts', import.meta.url)
),
},
output: {
name: '[name].[ext]',
entryFileNames: `assets/[name].js`,
chunkFileNames: `assets/[name].js`,
assetFileNames: `assets/[name].[ext]`,
},
plugins: [iifeWrap(), replaceCode()],
},
},
});