-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.renderer.config.js
More file actions
81 lines (76 loc) · 2.52 KB
/
webpack.renderer.config.js
File metadata and controls
81 lines (76 loc) · 2.52 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
const path = require("path");
const webpack = require("webpack");
const rules = require("./webpack.rules");
const forkTsPlugins = require("./webpack.plugins");
rules.push({
test: /\.css$/,
use: [{ loader: "style-loader" }, { loader: "css-loader" }],
});
/**
* @vercel/webpack-asset-relocator-loader injects:
* __webpack_require__.ab = __dirname + "/native_modules/"
* Electron Forge patches this via mainTemplate (webpack 4). Webpack 5 removed mainTemplate,
* so the patch never runs and sandboxed preload hits ReferenceError: __dirname is not defined.
* Mirror Forge's dev replacement: use the output directory for this chunk.
*/
class ForgeAssetRelocatorAbDirnamePlugin {
apply(compiler) {
compiler.hooks.thisCompilation.tap(
"ForgeAssetRelocatorAbDirnamePlugin",
(compilation) => {
compilation.hooks.processAssets.tap(
{
name: "ForgeAssetRelocatorAbDirnamePlugin",
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY,
},
() => {
const marker = '__webpack_require__.ab = __dirname + "/native_modules/"';
const outputRoot = compilation.outputOptions.path;
if (!outputRoot) return;
for (const { name, source, info } of compilation.getAssets()) {
if (!name.endsWith(".js")) continue;
const s = source.source();
if (typeof s !== "string" || !s.includes(marker)) continue;
const relDir = path.dirname(name);
const chunkDir =
relDir === "." ? outputRoot : path.join(outputRoot, relDir);
const normalized = path
.normalize(chunkDir)
.split(path.sep)
.join("/");
const prefix = JSON.stringify(
normalized.endsWith("/") ? normalized : `${normalized}/`
);
const next = s.replace(
marker,
`__webpack_require__.ab = ${prefix} + "native_modules/"`
);
compilation.updateAsset(
name,
new webpack.sources.RawSource(next),
info
);
}
}
);
}
);
}
}
module.exports = {
node: {
__dirname: true,
__filename: true,
},
module: {
rules,
},
plugins: [new ForgeAssetRelocatorAbDirnamePlugin(), ...forkTsPlugins],
resolve: {
extensions: [".js", ".ts", ".jsx", ".tsx", ".css"],
fallback: {
path: false,
fs: false,
},
},
};