-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwebpack.config.js
145 lines (134 loc) · 4.32 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
const glob = require("glob");
const path = require("path");
const { exec } = require("child_process");
const chokidar = require("chokidar");
const CopyFilePlugin = require("copy-webpack-plugin");
const LicensePlugin = require("webpack-license-plugin");
const userScripts = glob.sync("./src/ts/userScript/*.user.ts");
const userScriptEntries = {};
for (const userScript of userScripts) {
const key = `../userScript/${path.basename(userScript, ".ts")}.js`;
userScriptEntries[key] = `./${userScript}`;
}
class RunCommandsPlugin {
generateTypeGuards(callback) {
console.log("Generating type guards...");
exec("npx ts-auto-guard ./src/ts/@types/**/*.ts", (err, stdout, stderr) => {
if (err) {
console.error(`Error: ${err}`);
} else {
console.log(stdout);
if (callback) {
callback();
}
}
});
}
apply(compiler) {
let watcher;
let isWatchMode = false;
compiler.hooks.beforeCompile.tapAsync("RunCommandsPlugin", (params, callback) => {
if (isWatchMode) {
callback();
return;
}
this.generateTypeGuards(callback);
});
compiler.hooks.watchRun.tapAsync("RunCommandsPlugin", (params, callback) => {
isWatchMode = true;
if (!watcher) {
watcher = chokidar.watch("src/ts/@types/**/*.d.ts");
watcher.on("change", (path) => {
console.log(`Type definition file changed: ${path}`);
this.generateTypeGuards();
});
this.generateTypeGuards(callback);
} else {
callback();
}
});
compiler.hooks.afterEmit.tapAsync("RunCommandsPlugin", (compilation, callback) => {
exec("npx ts-node ./script/updatePrivacyPolicy.ts", (err, stdout, stderr) => {
if (err) {
console.error(`Error: ${err}`);
} else {
console.log(stdout);
}
});
exec("npx ts-node ./script/addUserScriptsComment.ts", (err, stdout, stderr) => {
if (err) {
console.error(`Error: ${err}`);
} else {
console.log(stdout);
}
callback();
});
});
}
}
module.exports = {
mode: "production",
entry: {
"./js/browserAction.js": "./src/ts/browserAction/browserAction.ts",
"./js/contentScript.js": "./src/ts/contentScript.ts",
"./js/pageScript.js": "./src/ts/pageScript.ts",
"./js/background.js": "./src/ts/background.ts",
"./js/ossLicenses.js": "./src/ts/ossLicenses.ts",
"./js/privacyPolicy.js": "./src/ts/privacyPolicy.ts",
"./js/initialSetup.js": "./src/ts/initialSetup.ts",
...userScriptEntries
},
output: {
filename: "[name]",
clean: true
},
module: {
rules: [
{
test: /\.ts$/,
use: "ts-loader"
},
{
test: /\.css$/,
use: [
"style-loader",
"css-loader"
]
}
]
},
resolve: {
extensions: [".ts", ".js"]
},
watchOptions: {
ignored: /src\/ts\/@types\/.*(?<!\.d\.ts)$/,
},
plugins: [
new RunCommandsPlugin(),
new CopyFilePlugin({
patterns: [
{
from: "./src/css/",
to: "./css/[name][ext]"
},
{
from: "./src/html/",
to: "./html/[name][ext]"
},
{
from: "./src/image/",
to: "./image/[name][ext]"
}
]
}),
new LicensePlugin(
{
outputFilename: "./json/oss-licenses.json",
unacceptableLicenseTest: (licenseIdentifier) => {
const acceptableLicenses = ["BSD-3-Clause", "Apache-2.0", "MIT", "0BSD", "MPL-2.0"];
return !acceptableLicenses.includes(licenseIdentifier);
}
}
)
]
};