forked from kresusapp/kresus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
282 lines (246 loc) · 9.15 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
const fs = require('fs');
const path = require('path');
const webpack = require('webpack');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const CssExtractPlugin = require('mini-css-extract-plugin');
const SpritesmithPlugin = require('webpack-spritesmith');
const GenerateJsonPlugin = require('generate-json-webpack-plugin');
// List available locales, to fetch only the required locales from Moment.JS:
// Build a regexp that selects the locale's name without the JS extension (due
// to the way moment includes those) and ensure that's the last character to
// not include locale variants. See discussion in
// https://framagit.org/kresusapp/kresus/merge_requests/448#note_130514
const locales = fs.readdirSync('shared/locales').map(
x => x.replace('.json', '')
);
const localesRegex = new RegExp(
'(' + locales.join('|') + ')$'
);
let entry = {
main: [
'@babel/polyfill',
'./node_modules/normalize.css/normalize.css',
'./node_modules/font-awesome/css/font-awesome.css',
'./node_modules/dygraphs/dist/dygraph.css',
'./node_modules/c3/c3.css',
'./node_modules/flatpickr/dist/themes/light.css',
'./node_modules/primer-tooltips/build/build.css',
'./node_modules/react-toastify/dist/ReactToastify.min.css',
'./client/css/base.css',
'./client/init.js'
]
};
// These extra locales should be put after the main client entrypoint to ensure
// that all the scripts are loaded and `window` is populated before trying to
// append locales to these objects.
locales.forEach(locale => {
if (locale !== 'en') {
// Flatpickr locales entries
entry.main.push(`flatpickr/dist/l10n/${locale}.js`);
}
});
const themes = fs.readdirSync('client/themes').filter(
f => fs.statSync(`client/themes/${f}`).isDirectory()
);
themes.forEach(theme => {
entry[`themes-${theme}-bundle`] = `./client/themes/${theme}/style.css`;
});
// Webpack config
const config = {
mode: process.env.NODE_ENV === "production" ? "production" : "development",
stats: "errors-warnings",
entry: entry,
output: {
path: path.resolve(__dirname, 'build', 'client'),
filename: '[name].js'
},
module: {
rules: [
{
test: /\.js$/,
// Exclude all but dygraphs
// Dygraphs ships ES5 files with arrow functions by default, so
// we need to pass Babel on them
exclude: /node_modules(?!\/dygraphs)/,
use: {
loader: 'babel-loader'
}
},
{
// Do not use the built-in json loader: we generate the content on the fly and
// return JS.
test: /dependenciesLicenses\.json$/,
type: "javascript/auto",
use: {
loader: 'dependencies-licenses-loader'
}
},
{
// Do not use the built-in json loader: we modify the content on the fly and
// return JS.
test: /\.json$/,
include: /shared\/locales/,
type: "javascript/auto",
use: [
{
loader: 'json-strip-loader',
query: {
key: 'server',
deep: false
}
}
]
},
{
test: /\.css$/,
use: [
CssExtractPlugin.loader,
"css-loader"
]
},
{
test: /\.(jpe?g|png|gif|svg)$/i,
use: [
{
loader: 'file-loader',
query: {
name: 'assets/images/[sha512:hash:hex].[ext]'
}
},
{
loader: 'image-webpack-loader',
query: {
bypassOnDebug: true,
'optipng': {
optimizationLevel: 7
},
'gifsicle': {
interlaced: false
}
}
}
]
},
{
test: /\.woff2?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
use: [
{
loader: 'url-loader',
query: {
limit: 10000,
mimetype: 'application/font-woff',
name: 'assets/fonts/[name]-[hash:16].[ext]'
}
}
]
},
{
test: /\.(ttf|otf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
use: [
{
loader: 'file-loader',
query: {
name: 'assets/fonts/[name]-[hash:16].[ext]'
}
}
]
}
]
},
devServer: {
disableHostCheck: true,
host: "0.0.0.0",
proxy: {
"/api": {
target: "http://localhost:9876/",
proxyTimeout: 5 * 60 * 1000,
onProxyReq: (proxyReq, req, res) => req.setTimeout(5 * 60 * 1000)
},
"/manifest": "http://localhost:9876/"
}
},
resolve: {
modules: ['node_modules', 'build/spritesmith-generated']
},
resolveLoader: {
modules: ['node_modules', path.resolve(__dirname, 'scripts', 'webpack', 'loaders')]
},
plugins: [
// Directly copy the static index and robots files.
new CopyWebpackPlugin([
{ from: './static/index.html' },
{ from: './static/robots.txt' },
{ from: './static/images/favicon', to: 'favicon' }
]),
// Extract CSS in a dedicated file.
new CssExtractPlugin({
filename: "[name].css"
}),
// Build bank icons sprite.
new SpritesmithPlugin({
src: {
cwd: path.resolve(__dirname, 'static', 'images', 'banks'),
glob: '*.png'
},
target: {
image: path.resolve(__dirname, 'build', 'spritesmith-generated', 'sprite.png'),
css: path.resolve(__dirname, 'build', 'spritesmith-generated', 'sprite.css')
},
apiOptions: {
cssImageRef: '~sprite.png'
}
}),
// Only keep the useful locales from Moment.
new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, localesRegex),
// Generate a themes.json file with the list of themes.
new GenerateJsonPlugin('themes.json', {themes: themes})
]
};
if (process.env.NODE_ENV === "production") {
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
// Report first error as hard error.
config.bail = true;
// Do not capture timing information for each module.
config.profile = false;
config.plugins.push(
// Minimize CSS.
new OptimizeCssAssetsPlugin({
cssProcessor: require('cssnano'),
cssProcessorOptions: {
zindex: false
}
})
);
} else {
// By default the development mode as the 'eval' value.
// See https://webpack.js.org/configuration/devtool/#devtool for other modes.
config.devtool = 'cheap-module-eval-source-map';
}
if (process.env.ANALYZE) {
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
config.plugins.push(
// Generate analyzer reports if needed
new BundleAnalyzerPlugin({
// Can be `server`, `static` or `disabled`.
// In `server` mode analyzer will start HTTP server to show bundle report.
// In `static` mode single HTML file with bundle report will be generated.
// In `disabled` mode you can use this plugin to just generate Webpack Stats JSON file by setting `generateStatsFile` to `true`.
analyzerMode: 'static',
// Path to bundle report file that will be generated in `static` mode.
// Relative to bundles output directory.
reportFilename: '../reports/client.html',
// Module sizes to show in report by default.
// Should be one of `stat`, `parsed` or `gzip`.
// See "Definitions" section for more information.
defaultSizes: 'parsed',
// Automatically open report in default browser
openAnalyzer: true,
// If `true`, Webpack Stats JSON file will be generated in bundles output directory
generateStatsFile: true,
// Name of Webpack Stats JSON file that will be generated if `generateStatsFile` is `true`.
// Relative to bundles output directory.
statsFilename: '../reports/client.json'
})
);
}
module.exports = config;