forked from msojocs/bilibili-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
131 lines (128 loc) · 3.67 KB
/
vite.config.ts
File metadata and controls
131 lines (128 loc) · 3.67 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
131
import { build, defineConfig, type LibraryOptions } from 'vite'
import { dirname, resolve } from 'node:path'
import react from '@vitejs/plugin-react-swc'
import { fileURLToPath } from 'node:url'
const __dirname = dirname(fileURLToPath(import.meta.url))
const watch = process.argv.includes('--watch') ? {} : false
// https://vite.dev/config/
export default defineConfig({
build: {
sourcemap: 'inline',
lib: {
entry: resolve(__dirname, 'src/extension/content.ts'),
fileName: (_format, entryName) => `extension/${entryName}.js`,
formats: ['iife'],
name: 'content'
},
emptyOutDir: false,
watch,
},
define: {
'process.env.NODE_ENV': '"production"'
},
plugins: [react()],
})
/**
* 为什么不合并在content.js里面?
* 权限不够:
* 1. content.js里面访问不到danmakuMange
* 2. content.js里面访问不到webview.isDevToolsOpened
* 3. content.js里面访问不到biliBridgePc
* 4. 方便测试开发content.js需要重启App(有无方法不需要重启?)
*/
// page libraries
{
const libraries: LibraryOptions[] = [
{
entry: resolve(__dirname, 'src/extension/page.ts'),
name: 'page',
fileName: (_format, entryName) => `extension/${entryName}.js`,
formats: ['iife'],
cssFileName: 'extension/bilibili'
},
];
// build
libraries.forEach(async (libItem) => {
await build({
configFile: false,
build: {
sourcemap: 'inline',
lib: libItem,
emptyOutDir: false,
rollupOptions: {
// other options
},
watch,
},
define: {
'process.env.NODE_ENV': '"production"'
},
plugins: [
react(),
],
});
});
}
{
const libraries: LibraryOptions[] = [
{
entry: resolve(__dirname, 'src/inject/index.ts'),
name: 'index',
fileName: (_format, entryName) => `inject/${entryName}.js`,
formats: ['cjs'], // 改为CommonJS格式
},
];
// build
libraries.forEach(async (libItem) => {
await build({
configFile: false,
build: {
sourcemap: 'inline',
lib: libItem,
emptyOutDir: false,
rollupOptions: {
// 使用函数动态判断哪些模块应该被视为外部依赖
external: (id) => {
// 处理所有Node.js内置模块
const builtinModules = [
'electron', 'fs', 'path', 'os', 'http', 'https', 'net', 'events',
'stream', 'util', 'crypto', 'buffer', 'querystring', 'url', 'zlib',
'child_process', 'cluster', 'dgram', 'dns', 'domain', 'http2',
'https', 'module', 'net', 'os', 'path', 'perf_hooks', 'punycode',
'querystring', 'readline', 'repl', 'stream', 'string_decoder',
'timers', 'tls', 'tty', 'url', 'util', 'v8', 'vm', 'zlib'
];
// 检查是否为Node.js内置模块或以node:前缀开头的模块
return builtinModules.includes(id) ||
id.startsWith('node:') ||
/^[a-z][a-z0-9-_]*$/.test(id); // 简单的npm包名匹配
},
output: {
// 使用IIFE格式但保持外部依赖通过require引入
format: 'commonjs',
inlineDynamicImports: false,
globals: {}
},
},
watch,
},
define: {
// 'process.env.NODE_ENV': '"production"'
},
plugins: [
{
name: 'replace',
async closeBundle() {
try {
// eslint-disable-next-line no-console
console.info('copy index.js')
const targetPath = resolve(__dirname, 'app/app/index.js')
await this.fs.copyFile(resolve(__dirname, 'dist/inject/index.js'), targetPath);
}
catch(_e){ /* empty */ }
}
}
],
});
});
}