forked from EhTagTranslation/EhSyringe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
260 lines (247 loc) · 9.28 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import path from 'node:path';
import fs from 'node:fs';
import { fileURLToPath } from 'node:url';
import { glob } from 'glob';
import { execaCommandSync } from 'execa';
import semver from 'semver';
import webpack from 'webpack';
import CopyPlugin from 'copy-webpack-plugin';
import _WebExtensionPlugin from '@webextension-toolbox/webpack-webextension-plugin';
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
import WebpackUserScript from 'webpack-userscript';
import { TsconfigPathsPlugin } from 'tsconfig-paths-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';
/** @type {typeof _WebExtensionPlugin} */
const WebExtensionPlugin = _WebExtensionPlugin.default;
const __dirname = path.resolve(fileURLToPath(import.meta.url), '../');
/** @type { import('./src/info').packageJson & import('type-fest').PackageJson } */
const pkgJson = JSON.parse(fs.readFileSync(path.resolve(__dirname, './package.json')));
const manifestJson = JSON.parse(fs.readFileSync(path.resolve(__dirname, './manifest.json')));
/** @type {webpack.RuleSetUseItem[]} */
const cssLoaders = [
{
loader: 'style-loader',
options: {
insert: ':root',
},
},
{
loader: 'css-loader',
options: {
importLoaders: 1,
},
},
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: ['postcss-import', 'postcss-preset-env', 'cssnano'],
},
},
},
];
export default async (env = {}, argv = {}) => {
const dev = argv.mode === 'development';
const devServer = !!env.WEBPACK_SERVE;
const version = semver.parse(pkgJson.version);
const repo = new URL(pkgJson.homepage).pathname.replace(/(^\/|\/$)/g, '');
pkgJson.homepage = pkgJson.homepage.replace(/\/$/, '');
version.prerelease = version.build = [];
/** @type {'user-script' | 'web-ext'} */
const type = env.type;
/** @type {webpack.Configuration} */
const config = {
mode: dev ? 'development' : 'production',
module: {
rules: [
{
include: [path.resolve(__dirname, 'src/assets')],
type: 'asset',
},
{
test: /\.ts$/,
exclude: '/node_modules',
use: [
{
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
plugins: ['@babel/plugin-transform-runtime'],
},
},
'ts-loader',
],
},
{
test: /\.less$/,
exclude: '/node_modules',
use: [...cssLoaders, 'less-loader'],
},
{
test: /\.css$/,
use: [...cssLoaders],
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
plugins: [new TsconfigPathsPlugin()],
},
plugins: [
new webpack.NormalModuleReplacementPlugin(/providers\/(.+)$/, (resource) => {
/** @type {string} */
let req = resource.request;
if (req.startsWith('providers/common/') || req.startsWith(`providers/${type}/`)) {
return;
}
req = req.replace('providers/', `providers/${type}/`);
resource.request = req;
}),
new webpack.DefinePlugin({
__type: JSON.stringify(type),
}),
],
performance: false,
devtool: dev ? 'eval-source-map' : 'source-map',
optimization: {},
};
if (env.analyze) {
config.plugins.push(
new BundleAnalyzerPlugin({
analyzerMode: 'static',
}),
);
}
if (type === 'user-script') {
const outputPath = path.resolve(__dirname, 'releases');
if (devServer) {
// 在 e 站使用调试功能需要连接 websocket 到 localhost,必须启用 HTTPS
// 启用 chrome://flags/#allow-insecure-localhost
config.devServer = {
https: true,
port: 48792,
allowedHosts: [
'.e-hentai.org',
'.exhentai.org',
'.hath.network',
'.exhentai55ld2wyap5juskbm67czulomrouspdacjamjeloj7ugjbsad.onion',
],
liveReload: false,
hot: false,
static: {
directory: outputPath,
},
devMiddleware: {
writeToDisk: true,
},
headers: {
'Access-Control-Allow-Origin': '*',
},
client: {
webSocketURL: 'wss://localhost:48792/ws',
},
};
}
config.optimization.minimize = false;
const currentHEAD = execaCommandSync('git rev-parse HEAD').stdout.trim();
const fileHost = devServer
? `${config.devServer.https ? 'https' : 'http'}://${config.devServer.host || 'localhost'}:${
config.devServer.port || 8080
}`
: `${pkgJson.homepage}/releases/latest/download`;
/**
* @param {string} chunkName
* @param {boolean} meta
*/
const fileName = (chunkName, meta = false) => {
const name = chunkName === 'main' ? `${pkgJson.name}` : `${pkgJson.name}.${chunkName}`;
const ext = meta ? 'meta' : 'user';
return `${name}.${ext}.js`;
};
config.entry = {
main: [
path.resolve(__dirname, 'src/user-script/polyfills.ts'),
path.resolve(__dirname, 'src/user-script/index.ts'),
],
};
if (dev) {
config.entry.debug = path.resolve(__dirname, 'src/user-script/debug.ts');
config.plugins.push(
new webpack.DefinePlugin({
userScriptMainSource: JSON.stringify(`${fileHost}/${fileName('main')}`),
}),
);
}
config.output = {
path: outputPath,
publicPath: '/',
filename: (data) => fileName(data.chunk.name),
};
config.plugins.push(
new WebpackUserScript({
headers: (data) => ({
name: pkgJson.displayName || pkgJson.name,
description: manifestJson.description,
namespace: pkgJson.homepage,
version: dev ? `[version]+build.[buildTime].[buildNo]` : `[version]`,
license: pkgJson.license,
compatible: ['firefox >= 60', 'edge >= 16', 'chrome >= 61', 'safari >= 11', 'opera >= 48'],
match: manifestJson.content_scripts[0].matches,
exclude: manifestJson.content_scripts[0].exclude_matches,
icon: `https://fastly.jsdelivr.net/gh/${repo}@${currentHEAD}/src/assets/logo.svg`,
updateURL: `${fileHost}/${fileName(data.chunkName, true)}`,
downloadURL: `${fileHost}/${fileName(data.chunkName)}`,
'run-at': 'document-start',
grant: [
'unsafeWindow',
'GM_deleteValue',
'GM_listValues',
'GM_setValue',
'GM_getValue',
'GM_addValueChangeListener',
'GM_removeValueChangeListener',
'GM_openInTab',
'GM_notification',
],
}),
proxyScript: { enable: false },
}),
);
} else {
config.devtool = 'inline-source-map';
config.entry = (await glob('src/web-ext/**/*.ts')).reduce(function (obj, el) {
const name = path.parse(el).name;
if (name !== 'polyfills') obj[name] = path.resolve(__dirname, el);
return obj;
}, {});
config.output = {
path: path.resolve(__dirname, 'dist'),
filename: 'script/[name].js',
publicPath: '/',
};
const vendor = env.vendor ? String(env.vendor) : undefined;
config.plugins.push(
new CopyPlugin({
patterns: [{ from: 'src/assets', to: 'assets' }],
}),
new HtmlWebpackPlugin({
title: pkgJson.displayName,
filename: 'assets/popup.html',
template: 'src/web-ext/popup.html',
chunks: ['popup'],
}),
new WebExtensionPlugin({
vendor,
manifestDefaults: {
name: pkgJson.displayName,
short_name: pkgJson.displayName,
description: pkgJson.description,
author: pkgJson.author,
version: version.format(),
homepage_url: pkgJson.homepage,
},
}),
);
}
return config;
};