-
-
Notifications
You must be signed in to change notification settings - Fork 146
/
webpack.config.js
190 lines (174 loc) · 6.18 KB
/
webpack.config.js
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const fs = require("fs");
const path = require("path");
const ProvidePlugin = require("webpack").ProvidePlugin;
const CopyWebPackPlugin = require("copy-webpack-plugin");
const sharp = require("sharp");
function deepCopy (obj) {
if (obj instanceof Array) {
return obj.slice();
}
const result = {};
Object.assign(result, obj);
Object.keys(result)
.filter(key => (typeof result[key]) === "object")
.forEach(key => result[key] = deepCopy(result[key]));
return result;
};
const browserFiles = [
".github/ISSUE_TEMPLATE.md",
"src/manifest.json",
"src/options.html",
"src/index.html",
"src/browserAction.html",
"static/firenvim.svg",
]
const config = {
mode: "development",
entry: {
background: "./src/background.ts",
browserAction: "./src/browserAction.ts",
content: "./src/content.ts",
index: "./src/frame.ts",
},
output: {
filename: "[name].js",
// Overwritten by browser-specific config
// path: __dirname + "/target/extension",
},
// Enable sourcemaps for debugging webpack's output.
devtool: "inline-source-map",
resolve: {
// Add '.ts' and '.tsx' as resolvable extensions.
extensions: [".ts", ".tsx", ".js", ".json"],
},
module: {
rules: [
// Load ts files with ts-loader
{ test: /\.tsx?$/, loader: "ts-loader" },
// For non-firefox browsers, we need to load a polyfill for the "browser"
// object. This polyfill is loaded through webpack's Provide plugin.
// Unfortunately, this plugin is pretty dumb and tries to provide an
// empty object named "browser" to the webextension-polyfill library.
// This results in the library not creating a browser object. The
// following line makes sure `browser` is undefined when
// webextension-polyfill is ran so that it can create a `browser` object.
// This is why we shouldn't load webextension-polyfill for firefox -
// otherwise we'd get a proxy instead of the real thing.
{
test: require.resolve("webextension-polyfill"),
use: [{
loader: "imports-loader",
options: {
additionalCode: 'browser = undefined;',
},
}]
}
]},
// Overwritten by browser-specific config
plugins: [],
}
const package_json = JSON.parse(require("fs").readFileSync(path.join(__dirname, "package.json")))
const chrome_target_dir = path.join(__dirname, "target", "chrome")
const firefox_target_dir = path.join(__dirname, "target", "firefox")
const chromeConfig = (config, env) => {
const result = Object.assign(deepCopy(config), {
output: {
path: chrome_target_dir,
},
plugins: [new CopyWebPackPlugin({ patterns: browserFiles.map(file => ({
from: file,
to: chrome_target_dir,
transform: (content, src) => {
if (path.basename(src) === "manifest.json") {
const manifest = JSON.parse(content.toString())
manifest["key"] = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAk3pkgh862ElxtREZVPLxVNbiFWo9SnvZtZXZavNvs2GsUTY/mB9yHTPBGJiBMJh6J0l+F5JZivXDG7xdQsVD5t39CL3JGtt93M2svlsNkOEYIMM8tHbp69shNUKKjZOfT3t+aZyigK2OUm7PKedcPeHtMoZAY5cC4L1ytvgo6lge+VYQiypKF87YOsO/BGcs3D+MMdS454tLBuMp6LxMqICQEo/Q7nHGC3eubtL3B09s0l17fJeq/kcQphczKbUFhTVnNnIV0JX++UCWi+BP4QOpyk5FqI6+SVi+gxUosbQPOmZR4xCAbWWpg3OqMk4LqHaWpsBfkW9EUt6EMMMAfQIDAQAB";
manifest["version"] = package_json.version;
manifest["description"] = package_json.description;
manifest["icons"] = {
"128": "firenvim128.png",
"16": "firenvim16.png",
"48": "firenvim48.png"
}
manifest.browser_action["default_icon"] = "firenvim128.png";
if (env.endsWith("testing")) {
manifest.content_security_policy = "script-src 'self' 'unsafe-eval'; object-src 'self';"
}
content = JSON.stringify(manifest, undefined, 3);
}
return content;
}
})).concat([16, 48, 128].map(n => ({
from: "static/firenvim.svg",
to: () => path.join(chrome_target_dir, `firenvim${n}.png`),
transform: (content) => sharp(content).resize(n, n).toBuffer(),
})))}),
new ProvidePlugin({ "browser": "webextension-polyfill" })
]
});
try {
fs.rmSync(result.output.path, { recursive: true })
} catch (e) {
console.log(`Could not delete output dir (${e.message})`);
}
return result;
}
const firefoxConfig = (config, env) => {
const result = Object.assign(deepCopy(config), {
output: {
path: firefox_target_dir,
},
plugins: [new CopyWebPackPlugin({
patterns: browserFiles.map(file => ({
from: file,
to: firefox_target_dir,
transform: (content, src) => {
switch(path.basename(src)) {
case "manifest.json":
const manifest = JSON.parse(content.toString());
manifest.browser_specific_settings = {
"gecko": {
"id": "firenvim@lacamb.re",
"strict_min_version": "88.0"
}
};
manifest.version = package_json.version;
manifest.description = package_json.description;
if (env.endsWith("testing")) {
manifest.content_security_policy = "script-src 'self' 'unsafe-eval'; object-src 'self';"
}
content = JSON.stringify(manifest, undefined, 3);
}
return content;
}
}))
})]
});
try {
fs.rmSync(result.output.path, { recursive: true })
} catch (e) {
console.log(`Could not delete output dir (${e.message})`);
}
return result;
}
module.exports = args => {
let env = "";
if (args instanceof Object) {
delete args.WEBPACK_BUNDLE;
delete args.WEBPACK_BUILD;
const keys = Object.keys(args);
if (keys.length > 0) {
env = keys[0];
}
}
if (env.endsWith("testing")) {
config.entry.content = "./src/testing/content.ts";
config.entry.index = "./src/testing/frame.ts";
config.entry.background = "./src/testing/background.ts";
}
if (env.startsWith("chrome")) {
return [chromeConfig(config, env)];
} else if (env.startsWith("firefox")) {
return [firefoxConfig(config, env)];
}
return [chromeConfig(config, env), firefoxConfig(config, env)];
}