-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.config.js
More file actions
112 lines (98 loc) · 3.79 KB
/
Copy pathextension.config.js
File metadata and controls
112 lines (98 loc) · 3.79 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
const fs = require("node:fs");
const path = require("node:path");
function patchRuntimeAssets(outputPath) {
const manifestPath = path.join(outputPath, "manifest.json");
const sandboxHtmlPath = path.join(outputPath, "sandbox", "sandbox.html");
const sandboxJsPath = path.join(outputPath, "sandbox", "sandbox.js");
if (!fs.existsSync(manifestPath) || !fs.existsSync(sandboxHtmlPath) || !fs.existsSync(sandboxJsPath)) return;
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
manifest.sandbox = {
...manifest.sandbox,
pages: ["sandbox/sandbox.html"],
};
manifest.content_security_policy = {
...(manifest.content_security_policy ?? {}),
sandbox: "sandbox allow-scripts; script-src 'self' 'unsafe-eval'; object-src 'self';",
};
fs.writeFileSync(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`);
}
class RuntimeAssetsPatchPlugin {
apply(compiler) {
compiler.hooks.afterEmit.tap("RuntimeAssetsPatchPlugin", (compilation) => {
patchRuntimeAssets(compilation.outputOptions.path || compiler.outputPath);
});
}
}
module.exports = {
browser: {
chrome: {
chromeBinary: "/mnt/c/Program Files/Google/Chrome/Application/chrome.exe",
},
},
config: (config) => {
if (config.mode === "production") {
// Keep source maps in dev, but omit them from production builds.
config.devtool = false;
// Production bundles must not ship dev-only hot reload runtime into
// the extension service worker or UI entrypoints.
config.plugins = (config.plugins || []).filter((plugin) => {
const name = plugin?.constructor?.name || "";
return !name.includes("ReactRefresh") && !name.includes("HotModuleReplacement");
});
if (config.builtins?.react) {
config.builtins.react = {
...config.builtins.react,
refresh: false,
};
}
}
// Node.js CLI scripts in tools-cli/ may still appear as implicit
// dependencies during production builds -- stub out node builtins.
config.resolve = config.resolve || {};
config.resolve.fallback = {
...config.resolve.fallback,
fs: false,
path: false,
};
// The browser bundle includes the vendored TypeScript runtime for sandbox
// parsing/transpilation. Keep Node globals mocked, but suppress the known
// webpack-compatible warnings emitted from typescript.js itself.
config.node = {
...config.node,
__dirname: "mock",
__filename: "mock",
};
config.ignoreWarnings = [
...(config.ignoreWarnings || []),
(warning) => {
const resource = warning?.module?.resource || "";
const message = warning?.message || "";
const isTypeScriptRuntime = resource.includes("node_modules/typescript/lib/typescript.js")
|| message.includes("node_modules/typescript/lib/typescript.js");
if (!isTypeScriptRuntime) return false;
return message.includes("__filename")
|| message.includes("__dirname")
|| message.includes("the request of a dependency is an expression");
},
];
// Inject build timestamp and package version as global constants.
const { CopyRspackPlugin, DefinePlugin } = require("@rspack/core");
const pkg = require("./package.json");
config.plugins = config.plugins || [];
config.plugins.push(
new CopyRspackPlugin({
patterns: [
{ from: "offscreen", to: "offscreen" },
{ from: "sandbox/sandbox.html", to: "sandbox/sandbox.html" },
{ from: "sandbox/sandbox.js", to: "sandbox/sandbox.js" },
],
}),
new RuntimeAssetsPatchPlugin(),
new DefinePlugin({
__BUILD_TIMESTAMP__: JSON.stringify(new Date().toISOString()),
__APP_VERSION__: JSON.stringify(pkg.version),
}),
);
return config;
},
};