-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
219 lines (197 loc) · 6.2 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
const path = require("path");
const ESLintPlugin = require("eslint-webpack-plugin");
const childProcess = require("child_process");
const { ConcatSource } = require("webpack-sources");
const Compilation = require("webpack/lib/Compilation");
const ESLintWebpackPlugin = require("eslint-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
// eslint-disable-next-line no-extend-native
String.prototype.interpolate = function (params) {
const names = Object.keys(params);
const vals = Object.values(params);
// eslint-disable-next-line no-new-func
return new Function(...names, `return \`${this}\`;`)(...vals);
};
const wrapPrefix = `
function wrapper(plugin_info) {
if (typeof window.plugin !== "function") {
window.plugin = function () {};
}
window.plugin.\${pluginCode} = window.plugin.\${pluginCode} = {};
window.plugin.\${pluginCode}.info = plugin_info;
window.plugin.\${pluginCode}.info.pluginId = '\${pluginCode}';
function setup () {
// Code injection
`;
const wrapSuffix = `
window.plugin.\${pluginCode}.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 extends ESLintWebpackPlugin {
constructor(options) {
super();
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/*", "https://intel-x.ingress.com/*"],
grant: "none",
version: "",
};
Object.assign(meta, this.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.interpolate(this.options.config)}\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.interpolate(this.options.config), compilation.assets[file], wrapSuffix.interpolate(this.options.config))
);
});
});
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 });
});
});
}
}
);
});
}
}
function getCommitShort() {
try {
return childProcess.execSync("git rev-parse --short HEAD").toString().trim();
} catch {
return null;
}
}
module.exports = (env, argv) => {
const pluginConfig = require("./plugin.config.json");
const commonMeta = pluginConfig.headers.common;
const build = env.build;
if (build in pluginConfig.headers) {
Object.assign(commonMeta, pluginConfig.headers[build]);
}
const dev = build !== "prod";
const commonConfig = {
mode: dev ? "development" : "production",
devtool: dev ? "eval-source-map" : undefined,
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: /\.scss$/,
use: [
// "to-string-loader",
"style-loader",
{
loader: "css-loader",
options: { esModule: false, url: false },
},
{
loader: "postcss-loader",
options: {
postcssOptions: {
plugins: [
[
"postcss-url",
{
url: "inline",
},
],
["cssnano", { preset: "default" }],
],
},
},
},
"sass-loader",
],
},
],
},
};
if (dev) {
commonConfig.devtool = "eval-source-map";
}
const commit = getCommitShort();
const configs = [];
pluginConfig.plugins.forEach((pluginCfg) => {
const config = Object.assign({}, commonConfig, {
name: pluginCfg.pluginName,
entry: {
init: `./src/code/${pluginCfg.pluginCode}.ts`,
},
output: {
filename: `${pluginCfg.pluginCode}.user.js`,
path: path.join(__dirname, pluginConfig.releaseFolder[build]),
},
});
const meta = Object.assign({}, commonMeta, pluginCfg.headers);
if (commit) meta.version += `-${commit}`;
config.plugins = [new ESLintPlugin({ fix: true }), new IITCScript({ config: pluginCfg, meta: meta, withMeta: true })].concat(dev ? [] : [new MiniCssExtractPlugin()]);
configs.push(config);
});
return configs;
};