forked from CTFd/CTFd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
300 lines (291 loc) · 9.72 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
const path = require('path')
const webpack = require('webpack')
const FixStyleOnlyEntriesPlugin = require("webpack-fix-style-only-entries")
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const RemoveStrictPlugin = require('remove-strict-webpack-plugin')
const WebpackShellPlugin = require('webpack-shell-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const roots = {
'themes/core': {
'css': {
'challenge-board': 'assets/css/challenge-board.scss',
'fonts': 'assets/css/fonts.scss',
'main': 'assets/css/main.scss',
'core': 'assets/css/core.scss',
'codemirror': 'assets/css/codemirror.scss',
},
'js': {
'pages/main': 'assets/js/pages/main.js',
'pages/setup': 'assets/js/pages/setup.js',
'pages/challenges': 'assets/js/pages/challenges.js',
'pages/scoreboard': 'assets/js/pages/scoreboard.js',
'pages/settings': 'assets/js/pages/settings.js',
'pages/stats': 'assets/js/pages/stats.js',
'pages/notifications': 'assets/js/pages/notifications.js',
'pages/teams/private': 'assets/js/pages/teams/private.js',
}
},
'themes/admin': {
'css': {
'admin': 'assets/css/admin.scss',
'challenge-board': 'assets/css/challenge-board.scss',
'codemirror': 'assets/css/codemirror.scss',
},
'js': {
'pages/main': 'assets/js/pages/main.js',
'pages/challenge': 'assets/js/pages/challenge.js',
'pages/challenges': 'assets/js/pages/challenges.js',
'pages/configs': 'assets/js/pages/configs.js',
'pages/notifications': 'assets/js/pages/notifications.js',
'pages/editor': 'assets/js/pages/editor.js',
'pages/pages': 'assets/js/pages/pages.js',
'pages/reset': 'assets/js/pages/reset.js',
'pages/scoreboard': 'assets/js/pages/scoreboard.js',
'pages/statistics': 'assets/js/pages/statistics.js',
'pages/submissions': 'assets/js/pages/submissions.js',
'pages/team': 'assets/js/pages/team.js',
'pages/teams': 'assets/js/pages/teams.js',
'pages/user': 'assets/js/pages/user.js',
'pages/users': 'assets/js/pages/users.js',
}
},
}
function getJSConfig(root, type, entries, mode) {
const out = {}
const ext = mode == 'development' ? 'dev' : 'min'
const chunk_file = `[name].${ext}.chunk.js`
for (let key in entries) {
out[key] = path.resolve(__dirname, 'CTFd', root, entries[key])
}
return {
entry: out,
mode: mode,
output: {
path: path.resolve(__dirname, 'CTFd', root, 'static', type),
publicPath: '/' + root + '/static/' + type,
filename: `[name].${ext}.js`,
chunkFilename: chunk_file,
},
optimization: {
splitChunks: {
chunks: 'all',
cacheGroups: {
echarts: {
name: 'echarts',
filename: `echarts.bundle.${ext}.js`,
test: /echarts/,
priority: 1,
enforce: true,
},
vendor: {
name: 'vendor',
filename: `vendor.bundle.${ext}.js`,
test: /node_modules/,
// maxSize: 1024 * 256,
enforce: true,
},
graphs: {
name: 'graphs',
filename: `graphs.${ext}.js`,
test: /graphs/,
priority: 1,
reuseExistingChunk: true,
},
helpers: {
name: 'helpers',
filename: `helpers.${ext}.js`,
test: /helpers/,
priority: 1,
reuseExistingChunk: true,
},
components: {
name: 'components',
filename: `components.${ext}.js`,
test: /components/,
priority: 1,
reuseExistingChunk: true,
},
default: {
filename: `core.${ext}.js`,
minChunks: 2,
priority: -1,
reuseExistingChunk: true,
},
},
},
minimizer: [
new UglifyJsPlugin({
cache: true,
parallel: true,
uglifyOptions: {
compress: {
// Remove console.log in production
drop_console: mode === 'production'
},
},
}),
],
},
module: {
rules: [
{
test: /\.js$/,
use: {
loader: 'babel-loader',
options: {
cacheDirectory: true,
presets: [
['@babel/preset-env', { useBuiltIns: 'entry', modules: 'commonjs' }],
],
}
}
},
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
css: ['vue-style-loader', {
loader: 'css-loader',
}],
js: [
'babel-loader',
],
},
cacheBusting: true,
},
},
// This rule is magically used to load the <style> section of VueJS SFC.
// Don't really understand what magic Vue is using here but it works.
{
test: /\.css$/,
use: [
'vue-style-loader',
'css-loader'
]
},
],
},
plugins: [
new VueLoaderPlugin(),
new webpack.NamedModulesPlugin(),
new RemoveStrictPlugin(),
// Identify files that are generated in development but not in production and create stubs to avoid a 404
// Pretty nasty hack, would be a little better if this was purely JS
new WebpackShellPlugin({
onBuildEnd:[
mode == 'development' ? 'echo Skipping JS stub generation' : 'python3 -c \'exec(\"\"\"\nimport glob\nimport os\n\nstatic_js_dirs = [\n "CTFd/themes/core/static/js/**/*.dev.js",\n "CTFd/themes/admin/static/js/**/*.dev.js",\n]\n\nfor js_dir in static_js_dirs:\n for path in glob.glob(js_dir, recursive=True):\n if path.endswith(".dev.js"):\n path = path.replace(".dev.js", ".min.js")\n if os.path.isfile(path) is False:\n open(path, "a").close()\n\"\"\")\''
],
safe: true,
}),
],
resolve: {
extensions: ['.js'],
alias: {
core: path.resolve(__dirname, 'CTFd/themes/core/assets/js/'),
},
},
}
}
function getCSSConfig(root, type, entries, mode) {
const out = {}
const ext = mode == 'development' ? 'dev' : 'min'
const ouptut_file = `[name].${ext}.css`
const chunk_file = `[id].${ext}.css`
for (let key in entries) {
out[key] = path.resolve(__dirname, 'CTFd', root, entries[key])
}
return {
entry: out,
mode: mode,
output: {
path: path.resolve(__dirname, 'CTFd', root, 'static', type),
publicPath: '/' + root + '/static/' + type,
},
optimization: {
minimizer: [
new OptimizeCssAssetsPlugin({})
]
},
module: {
rules: [
{
test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?(#\w+)?$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
publicPath: '../fonts',
outputPath: '../fonts',
}
}
]
},
{
test: /\.(s?)css$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
importLoaders: 2,
}
},
{
loader: 'string-replace-loader',
options: {
multiple: [
// Replace core font-faces
{ search: "font-face\s*{\s*font-family:\s*[\"']Lato[\"']", replace: "font-face{font-family:'LatoOffline'", flags: 'gm' },
{ search: "font-face\s*{\s*font-family:\s*[\"']Raleway[\"']", replace: "font-face{font-family:'RalewayOffline'", flags: 'gm' },
// Replace Font-Awesome font-faces
{ search: "font-face\s*{\s*font-family:\s*[\"']Font Awesome 5 Free[\"']", replace: "font-face{font-family:'Font Awesome 5 Free Offline'", flags: 'gm' },
{ search: "font-face\s*{\s*font-family:\s*[\"']Font Awesome 5 Brands[\"']", replace: "font-face{font-family:'Font Awesome 5 Brands Offline'", flags: 'gm' },
// Replace Font-Awesome class rules
{ search: "far\s*{\s*font-family:\s*[\"']Font Awesome 5 Free[\"']", replace: "far{font-family:'Font Awesome 5 Free','Font Awesome 5 Free Offline'", flags: 'gm' },
{ search: "fas\s*{\s*font-family:\s*[\"']Font Awesome 5 Free[\"']", replace: "fas{font-family:'Font Awesome 5 Free','Font Awesome 5 Free Offline'", flags: 'gm' },
{ search: "fab\s*{\s*font-family:\s*[\"']Font Awesome 5 Brands[\"']", replace: "fab{font-family:'Font Awesome 5 Brands','Font Awesome 5 Brands Offline'", flags: 'gm' },
],
strict: true,
}
},
{
loader: 'sass-loader',
},
],
},
]
},
resolve: {
extensions: ['.css'],
alias: {
core: path.resolve(__dirname, 'CTFd/themes/core/assets/css/'),
},
},
plugins: [
new FixStyleOnlyEntriesPlugin(),
new MiniCssExtractPlugin({
filename: ouptut_file,
chunkFilename: chunk_file
}),
],
}
}
const mapping = {
'js': getJSConfig,
'css': getCSSConfig,
}
module.exports = (env, options) => {
let output = []
let mode = options.mode
for (let root in roots) {
for (let type in roots[root]) {
let entry = mapping[type](root, type, roots[root][type], mode);
output.push(entry)
}
}
return output
}