Repository navigation
Expand file tree
/
Copy pathwebpack.main.config.js
More file actions
60 lines (55 loc) · 1.69 KB
/
Copy pathwebpack.main.config.js
File metadata and controls
60 lines (55 loc) · 1.69 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
const path = require("path");
const fs = require("fs");
module.exports = (_env, { mode }) => ({
devtool: mode === "development" ? "eval-cheap-module-source-map" : false,
entry: {
index: "./src/main/index.js",
},
module: {
rules: require("./webpack.rules")(mode),
},
// 确保worker可以正确解析
node: {
__dirname: false,
__filename: false,
},
// 明确指定输出
// NOTE: @electron-forge/plugin-webpack 8.x expects the main bundle as
// index.cjs (package.json "main" points to .webpack/main/index.cjs).
output: {
path: path.join(__dirname, ".webpack", "main"),
filename: "[name].cjs",
},
// 在构建后复制资源文件
plugins: [
{
apply: (compiler) => {
compiler.hooks.afterEmit.tap("CopyAssets", () => {
// 复制资源文件夹
const assetsSrcDir = path.join(__dirname, "src/renderer/assets");
const assetsDestDir = path.join(
__dirname,
".webpack",
"main",
"assets",
);
if (!fs.existsSync(assetsDestDir)) {
fs.mkdirSync(assetsDestDir, { recursive: true });
}
if (fs.existsSync(assetsSrcDir)) {
// 读取资源目录中的所有文件
const assetFiles = fs.readdirSync(assetsSrcDir);
// 复制每个文件
assetFiles.forEach((file) => {
const srcFilePath = path.join(assetsSrcDir, file);
const destFilePath = path.join(assetsDestDir, file);
if (fs.statSync(srcFilePath).isFile()) {
fs.copyFileSync(srcFilePath, destFilePath);
}
});
}
});
},
},
],
});