forked from FrankerFaceZ/FrankerFaceZ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
371 lines (328 loc) · 7.56 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* global module __dirname */
const path = require('path');
const semver = require('semver');
const {exec, execSync} = require('child_process');
const { VueLoaderPlugin } = require('vue-loader');
const { WebpackManifestPlugin } = require('webpack-manifest-plugin');
const { EsbuildPlugin } = require('esbuild-loader');
const CopyPlugin = require('copy-webpack-plugin');
if ( process.env.NODE_ENV == null )
process.env.NODE_ENV = 'production';
// Are we in development?
const DEV_SERVER = process.env.WEBPACK_SERVE == 'true';
const DEV_BUILD = process.env.NODE_ENV !== 'production';
// Is this for an extension?
const FOR_EXTENSION = !! process.env.FFZ_EXTENSION;
// Get the public path.
const FILE_PATH = DEV_SERVER
? 'https://localhost:8000/script/'
: FOR_EXTENSION
? ''
: 'https://cdn.frankerfacez.com/static/';
console.log('NODE_ENV:', process.env.NODE_ENV);
console.log('FOR_EXTENSION:', FOR_EXTENSION, FOR_EXTENSION ? ` (${process.env.FFZ_EXTENSION})` : '');
console.log('IS_DEV_BUILD:', DEV_BUILD);
console.log('IS SERVE:', DEV_SERVER);
console.log('FILE PATH:', FILE_PATH);
// Version Stuff
const VERSION = semver.parse(require('./package.json').version);
const commit_hash = DEV_SERVER
? null
: execSync('git rev-parse HEAD').toString().trim();
// The Config
const ENTRY_POINTS = {
bridge: './src/bridge.js',
esbridge: './src/esbridge.js',
player: './src/player.js',
avalon: './src/main.ts',
clips: './src/clips.js'
};
const TARGET = 'es2020';
/** @type {import('webpack').Configuration} */
const config = {
mode: DEV_BUILD
? 'development'
: 'production',
devtool: DEV_BUILD
? 'inline-source-map'
: 'source-map',
target: ['web', TARGET],
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
alias: {
res: path.resolve(__dirname, 'res/'),
styles: path.resolve(__dirname, 'styles/'),
root: __dirname,
src: path.resolve(__dirname, 'src/'),
utilities: path.resolve(__dirname, 'src/utilities/'),
site: path.resolve(__dirname, 'src/sites/twitch-twilight/')
}
},
entry: ENTRY_POINTS,
externals: [
({context, request}, callback) => {
if ( request === 'vue' && ! /utilities/.test(context) )
return callback(null, 'root ffzVue');
callback();
}
],
output: {
chunkFormat: 'array-push',
clean: true,
publicPath: FOR_EXTENSION
? 'auto'
: FILE_PATH,
path: path.resolve(__dirname, 'dist'),
filename: (FOR_EXTENSION || DEV_SERVER)
? '[name].js'
: '[name].[contenthash:8].js',
chunkLoadingGlobal: 'ffzWebpackJsonp',
crossOriginLoading: 'anonymous'
},
optimization: {
minimizer: [
new EsbuildPlugin({
target: TARGET,
keepNames: true
})
],
splitChunks: {
chunks(chunk) {
return ! Object.keys(ENTRY_POINTS).includes(chunk.name);
},
cacheGroups: {
vendors: false
}
}
},
plugins: [
new CopyPlugin({
patterns: [
{
from: FOR_EXTENSION
? './src/entry_ext.js'
: './src/entry.js',
to: (DEV_SERVER || DEV_BUILD)
? 'script.js'
: 'script.min.js'
}
]
}),
new VueLoaderPlugin(),
new EsbuildPlugin({
define: {
__version_major__: JSON.stringify(VERSION.major),
__version_minor__: JSON.stringify(VERSION.minor),
__version_patch__: JSON.stringify(VERSION.patch),
__version_prerelease__: JSON.stringify(VERSION.prerelease),
__version_build__: JSON.stringify(process.env.FFZ_BUILD || null),
__git_commit__: JSON.stringify(commit_hash),
__extension__: FOR_EXTENSION
? JSON.stringify(process.env.FFZ_EXTENSION)
: JSON.stringify(false)
}
}),
new WebpackManifestPlugin({
publicPath: ''
})
],
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'esbuild-loader',
options: {
loader: 'jsx',
jsxFactory: 'createElement',
target: TARGET
}
},
{
test: /\.tsx?$/,
exclude: /node_modules/,
loader: 'esbuild-loader',
options: {
loader: 'tsx',
jsxFactory: 'createElement',
target: TARGET
}
},
{
test: /\.(graphql|gql)$/,
exclude: /node_modules/,
use: [
'graphql-tag/loader',
'minify-graphql-loader'
]
},
{
test: /\.json$/,
include: /src/,
type: 'asset/resource',
generator: {
filename: (FOR_EXTENSION || DEV_BUILD)
? '[name].json'
: '[name].[contenthash:8].json'
}
},
{
// This stupid rule goes out to Mozilla, who consistantly
// manage to have this one file not included in the bundle
// the same way as every other build on every other machine
// out of like twelve I've tested. So fine. We'll do it
// your way. Whatever. I don't care.
test: /entities.json$/,
include: /node_modules/,
type: 'asset/resource',
generator: {
filename: (FOR_EXTENSION || DEV_BUILD)
? '[name].json'
: '[name].[contenthash:8].json'
}
},
{
test: /\.(?:otf|eot|ttf|woff|woff2)$/,
use: [{
loader: 'file-loader',
options: {
name: (FOR_EXTENSION || DEV_BUILD)
? '[name].[ext]'
: '[name].[contenthash:8].[ext]'
}
}]
},
{
test: /\.md$/,
type: 'asset/source',
},
{
test: /\.svg$/,
type: 'asset/source'
},
{
test: /\.vue$/,
loader: 'vue-loader'
},
{
test: /\.(?:sa|sc|c)ss$/,
resourceQuery: {
not: [
/css_tweaks/
]
},
use: [
{
loader: 'file-loader',
options: {
name: (FOR_EXTENSION || DEV_BUILD)
? '[name].css'
: '[name].[contenthash:8].css'
}
},
{
loader: 'extract-loader',
options: {
publicPath: ''
}
},
{
loader: 'css-loader',
options: {
esModule: false,
sourceMap: DEV_BUILD ? true : false
}
},
{
loader: 'sass-loader',
options: {
sourceMap: true
}
}
]
},
{
test: /\.(?:sa|sc|c)ss$/,
resourceQuery: /css_tweaks/,
use: [
{
loader: 'raw-loader'
},
{
loader: 'extract-loader'
},
{
loader: 'css-loader',
options: {
esModule: false,
sourceMap: DEV_BUILD ? true : false
}
},
{
loader: 'sass-loader',
options: {
sourceMap: false
}
}
]
}
]
}
};
if ( DEV_SERVER )
config.devServer = {
client: false,
webSocketServer: false,
magicHtml: false,
liveReload: false,
hot: false,
server: 'https',
port: 8000,
compress: true,
allowedHosts: [
'.twitch.tv',
'.frankerfacez.com'
],
static: {
directory: path.join(__dirname, 'dev_cdn'),
},
devMiddleware: {
publicPath: '/script/',
},
proxy: {
'**': {
target: 'https://cdn.frankerfacez.com/',
changeOrigin: true
}
},
setupMiddlewares: (middlewares, devServer) => {
devServer.app.get('/update_font', (req, res) => {
const proc = exec('npm run font:save');
proc.stdout.on('data', data => {
console.log('FONT>>', data);
});
proc.stderr.on('data', data => {
console.error('FONT>>', data);
});
proc.on('close', code => {
console.log('FONT>> Exited with code', code);
res.redirect(req.headers.referer);
});
});
devServer.app.get('/dev_server', (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Private-Network', 'true');
res.json({
path: process.cwd(),
version: 2
})
});
middlewares.unshift((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Private-Network', 'true');
next();
});
return middlewares;
}
};
module.exports = config;