-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.impl.mjs
More file actions
108 lines (98 loc) · 2.59 KB
/
build.impl.mjs
File metadata and controls
108 lines (98 loc) · 2.59 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
import esbuild from 'esbuild';
const isWatch = process.argv.includes('--watch');
const isRelease = process.argv.includes('--release');
const isDev = isWatch && !isRelease;
// 插件:Release 模式下移除调试日志,保留 error 和 warn
const stripDebugLogsPlugin = {
name: 'strip-debug-logs',
setup(build) {
if (!isRelease) return;
build.onLoad({ filter: /\.jsx?$/ }, async (args) => {
const fs = await import('fs');
let contents = await fs.promises.readFile(args.path, 'utf8');
// 移除 console.log, console.debug, console.info(保留 error, warn)
// 匹配 console.log(...) 包括多行和嵌套括号
contents = contents.replace(/console\.(log|debug|info)\s*\([^;]*\);?/g, '');
return {
contents,
loader: args.path.endsWith('.jsx') ? 'jsx' : 'js'
};
});
}
};
const commonOptions = {
bundle: true,
format: 'iife',
platform: 'browser',
target: ['chrome115'],
sourcemap: isDev ? 'inline' : false,
minify: isRelease,
plugins: [stripDebugLogsPlugin],
define: {
'process.env.NODE_ENV': isDev ? '"development"' : '"production"',
},
logLevel: 'info'
};
// React 相关配置
const reactOptions = {
...commonOptions,
loader: {
'.js': 'jsx',
'.jsx': 'jsx'
},
jsx: 'automatic', // 使用 React 17+ 的自动 JSX 运行时
};
const builds = [
{
...commonOptions,
entryPoints: ['src/content/index.js'],
outfile: 'dist/content.js'
},
{
...commonOptions,
entryPoints: ['src/background/index.js'],
outfile: 'dist/background.js'
},
{
...commonOptions,
entryPoints: ['src/popup/popup.js'],
outfile: 'dist/popup.js'
},
{
...commonOptions,
entryPoints: ['src/setup/setup.js'],
outfile: 'dist/setup.js'
},
{
...reactOptions,
entryPoints: ['src/sidepanel/index.jsx'],
outfile: 'dist/sidepanel.js'
},
{
...commonOptions,
entryPoints: ['src/sidepanel/styles/index.css'],
outfile: 'dist/sidepanel.css'
}
];
async function build() {
try {
if (isWatch) {
console.log('Watching for changes...');
const contexts = await Promise.all(
builds.map(options => esbuild.context(options))
);
await Promise.all(contexts.map(ctx => ctx.watch()));
} else {
await Promise.all(builds.map(options => esbuild.build(options)));
if (isRelease) {
console.log('√ Release build completed! (debug logs removed, minified)');
} else {
console.log('√ Build completed!');
}
}
} catch (error) {
console.error('× Build failed:', error);
process.exit(1);
}
}
build();