-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
webpack.config.js
230 lines (216 loc) · 5.52 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
const path = require("path");
const outputPath = path.join(__dirname, "releases");
const ESLintPlugin = require("eslint-webpack-plugin");
const childProcess = require("child_process");
const { ConcatSource } = require("webpack-sources");
const Compilation = require("webpack/lib/Compilation");
const wrapPrefix = `
function wrapper(plugin_info) {
if (typeof window.plugin !== "function") {
window.plugin = function () {};
}
window.plugin.Wasabee = window.plugin.wasabee = {};
window.plugin.wasabee.info = plugin_info.script;
// Code injection
function setup () {
`;
const wrapSuffix = `
window.plugin.wasabee.init();
};
setup.info = plugin_info; //add the script info data to the function as a property
if (!window.bootPlugins) {
window.bootPlugins = [];
}
window.bootPlugins.push(setup);
// if IITC has already booted, immediately run the 'setup' function
if (window.iitcLoaded && typeof setup === "function") {
setup();
}
}
var script = document.createElement("script");
var info = {};
if (typeof GM_info !== "undefined" && GM_info && GM_info.script) {
info.script = {
version: GM_info.script.version,
name: GM_info.script.name,
description: GM_info.script.description,
};
}
script.appendChild(
document.createTextNode("(" + wrapper + ")(" + JSON.stringify(info) + ");")
);
(document.body || document.head || document.documentElement).appendChild(
script
);
`;
class IITCScript {
constructor(options) {
this.options = options || {};
this.header = "";
this.header += "// ==UserScript==\n";
const keys = [
"id",
"name",
"namespace",
"version",
"updateURL",
"downloadURL",
"description",
"author",
"match",
"category",
"grant",
];
const meta = {
match: "https://intel.ingress.com/*",
grant: "none",
version: "",
};
Object.assign(meta, options.meta);
meta.version = meta.version.replace(
"BUILDDATE",
new Date()
.toISOString()
.replace(/[^0-9]/g, "")
.slice(0, 14)
);
keys.forEach((key) => {
Array.prototype.concat
.apply([], meta[key] && [meta[key]])
.forEach((value) => {
if (value) this.header += `// @${key.padEnd(13)} ${value}\n`;
});
});
this.header += "// ==/UserScript==\n\n";
}
apply(compiler) {
compiler.hooks.compilation.tap("Userscript", (compilation) => {
compilation.hooks.processAssets.tap(
{
name: "Userscript",
stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE,
},
() => {
compilation.chunks.forEach((chunk) => {
chunk.files.forEach((file) => {
compilation.updateAsset(
file,
new ConcatSource(
this.header,
wrapPrefix,
compilation.assets[file],
wrapSuffix
)
);
});
});
if (this.options.withMeta) {
compilation.chunks.forEach((chunk) => {
chunk.files.forEach((file) => {
if (file.match(/user.js$/))
compilation.emitAsset(
file.replace(/user.js$/, "meta.js"),
new ConcatSource(this.header),
{ minimized: true }
);
});
});
}
}
);
});
}
}
const config = {
entry: {
init: "./src/code/init.ts",
},
output: {
filename: "wasabee.user.js",
},
resolve: {
modules: ["node_modules"],
extensions: [".ts", ".js"],
},
module: {
rules: [
{
test: /\.tsx?$/,
use: "ts-loader",
exclude: /node_modules/,
},
{
test: /\.svg$/,
loader: "raw-loader",
options: { esModule: false },
},
{
test: /\.css$/,
use: [
"to-string-loader",
{
loader: "css-loader",
options: { esModule: false, url: false },
},
{
loader: "postcss-loader",
options: {
postcssOptions: {
plugins: [
[
"postcss-url",
{
url: "inline",
},
],
["cssnano", { preset: "default" }],
],
},
},
},
],
},
],
},
plugins: [new ESLintPlugin({ fix: true })],
};
function getCommitShort() {
try {
const commit = childProcess
.execSync("git rev-parse --short HEAD")
.toString()
.trim();
return commit;
} catch {
return null;
}
}
module.exports = (env, argv) => {
const pluginConfig = require("./plugin.config.json");
const meta = pluginConfig.headers.common;
const build = env.build;
if (build in pluginConfig.headers)
Object.assign(meta, pluginConfig.headers[build]);
switch (build) {
case "prod":
case "dev":
case "testing":
case "scot":
config.output.path = path.join(outputPath, build);
break;
case "pr":
meta.version += env.pr;
config.output.path = path.join(outputPath, "dev");
default:
}
if (build !== "prod") {
const commit = getCommitShort();
if (commit) meta.version += `-${commit}`;
config.devtool = "eval";
config.mode = "development";
} else {
config.mode = "production";
}
config.plugins.push(new IITCScript({ meta: meta, withMeta: true }));
return config;
};