-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathconfig.js
320 lines (304 loc) · 9.05 KB
/
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
/* eslint-disable import/no-extraneous-dependencies */
// const dotenv = require('dotenv').config();
const path = require('path');
const webpack = require('webpack');
// const SentryWebpackPlugin = require('@sentry/webpack-plugin');
const CleanPlugin = require('clean-webpack-plugin');
const ProgressBarPlugin = require('progress-bar-webpack-plugin');
const WebpackPwaManifest = require('webpack-pwa-manifest');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
// const WebpackAssetsManifest = require('webpack-assets-manifest');
// const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const HtmlWebpackPlugin = require('html-webpack-plugin');
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin');
// const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const DotenvPlugin = require('dotenv-webpack');
const ImageminPlugin = require('imagemin-webpack-plugin').default;
const TerserPlugin = require('terser-webpack-plugin');
const { GenerateSW } = require('workbox-webpack-plugin');
const NODE_ENV = process.env.NODE_ENV || 'development';
const IS_PROD_BUILD = NODE_ENV === 'production' || NODE_ENV === 'staging';
// const IS_DEV = !IS_PROD_BUILD;
const ROOT_DIR = path.resolve(__dirname, '../');
const SRC_DIR = path.resolve(ROOT_DIR, 'src');
const dotEnvPlugin = new DotenvPlugin({
path: path.resolve(ROOT_DIR, `.env.${NODE_ENV}`)
});
const commonPlugins = [
new webpack.NamedModulesPlugin(),
/* For optimizing lodash */
new LodashModuleReplacementPlugin({
shorthands: true, // Should be able to use _.map(arr, 'obj')
collections: true,
paths: true,
flattening: true
}),
/* For webpack build progress % */
new ProgressBarPlugin(),
/* Define globals */
new webpack.DefinePlugin({
'NODE_ENV': IS_PROD_BUILD ? '"production"' : '"development"'
}),
new HtmlWebpackPlugin({
template: path.resolve(ROOT_DIR, 'public/index.html'),
templateParameters: {
env: IS_PROD_BUILD ? 'production' : 'development'
},
minify: true
}),
new WebpackPwaManifest({
filename: 'manifest.json',
short_name: 'Teacher Dashboard',
name: 'Lido Learning Teacher Dashboard',
orientation: 'landscape-primary',
prefer_related_applications: false,
scope: '/',
start_url: '/?pwa=teacher-dashboard',
display: 'fullscreen',
theme_color: '#2e2d2e',
background_color: '#ffffff',
icons: [
{
src: path.resolve(ROOT_DIR, 'public/assets/images/Lido_dashboard_192.png'),
type: 'image/png',
sizes: '192x192',
},
{
src: path.resolve(ROOT_DIR, 'public/assets/images/Lido_dashboard_512.png'),
type: 'image/png',
sizes: '512x512',
},
],
}),
new GenerateSW({
include: [],
exclude: [/\.html$/, /\.js$/],
// Define runtime caching rules.
runtimeCaching: [{
// Match any request ends with .png, .jpg, .jpeg or .svg.
urlPattern: /\.(?:png|jpg|jpeg|svg)$/,
// Apply a cache-first strategy.
handler: 'CacheFirst',
options: {
// Use a custom cache name.
cacheName: 'images',
// Only cache 10 images.
expiration: {
maxEntries: 10,
},
},
}],
cleanupOutdatedCaches: true,
skipWaiting: true,
}),
dotEnvPlugin
];
const devPlugins = [
...commonPlugins,
];
const prodPlugins = [
...commonPlugins,
/* Clear dist folder before a new build */
new CleanPlugin(),
// new BundleAnalyzerPlugin(),
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: '[name].[hash].css',
chunkFilename: '[id].[hash].css',
}),
new ImageminPlugin({ test: /\.(jpe?g|png|gif|svg)$/i }),
// new SentryWebpackPlugin({
// include: '.',
// ignoreFile: '.sentrycliignore',
// ignore: ['node_modules', 'webpack.config.js'],
// configFile: path.resolve(ROOT_DIR, '.sentry.properties')
// })
];
module.exports = function webpackConfig() {
const config = {
mode: IS_PROD_BUILD ? 'production' : 'development',
devtool: 'source-map',
context: ROOT_DIR,
entry: {
main: [
path.resolve(SRC_DIR, 'index.js')
]
},
output: {
publicPath: '/',
path: path.resolve(ROOT_DIR, 'build'),
filename: IS_PROD_BUILD ? '[name].[chunkhash].js' : '[name].js',
chunkFilename: IS_PROD_BUILD ? 'chunks/[id].[chunkhash].js' : 'chunks/[id].js',
},
/* Disabling logs from plugins like mini css extract */
stats: {
children: false,
chunks: false,
assets: false,
},
devServer: {
contentBase: path.resolve(ROOT_DIR, 'build'),
hot: true,
publicPath: '/',
historyApiFallback: true,
host: '0.0.0.0',
port: 4000,
},
performance: {
hints: false
},
optimization: {},
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: [
/* Babel loader to convert next gen JS syntax to a more browser compatible syntax */
{
loader: 'babel-loader?cacheDirectory'
},
{
loader: 'react-hot-loader/webpack'
}
/* Eslint to enforce coding conventions */
// {
// loader: 'eslint-loader',
// options: {
// // Don't show warnings in webpack output
// quiet: true,
// failOnError: false,
// emitError: false
// }
// }
]
},
{
test: /\.json$/,
exclude: /(node_modules|bower_components)/,
type: 'javascript/auto',
loader: 'file-loader',
options: {
name: '[name].[ext]',
},
},
{
test: /\.css$/,
exclude: /(node_modules|bower_components)/,
use: [
{
loader: IS_PROD_BUILD ? MiniCssExtractPlugin.loader : 'style-loader',
},
{
loader: 'css-loader',
options: {
importLoaders: 1,
localIdentName: IS_PROD_BUILD ? '[hash:base64:5]' : '[folder]-[local]__[hash:base64:5]',
modules: true,
sourceMap: true,
}
},
{
loader: 'postcss-loader',
options: {
plugins: () => [
// require('postcss-import')({ root: path.resolve(SRC_DIR) }),
/* To enable css nesting like sass */
require('postcss-nested'),
/* Enable variables inside media queries */
require('postcss-custom-media')({
importFrom: [path.resolve(SRC_DIR, 'theme/variables/media.css')]
}),
/* for color modification functions like lighten darken etc. */
require('postcss-color-function'),
/* automated vendor prefixing */
require('autoprefixer'),
]
}
},
]
},
{
test: /\.css$/,
include: /(node_modules|bower_components)/,
use: [
{
loader: IS_PROD_BUILD ? MiniCssExtractPlugin.loader : 'style-loader',
},
{
loader: 'css-loader',
},
],
},
{
/* Fonts and Images */
test: /\.(png|ico|jpg|ttf|eot|otf|woff(2)?)(\?[a-z0-9]+)?$/,
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: (url, resourcePath, context) => {
if (/favicon\.ico/.test(resourcePath)) {
return `${url}`;
}
return `${resourcePath.split('/public/')[1]}`;
}
},
},
{
test: /\.svg$/,
include: [
path.resolve(ROOT_DIR, 'public/assets/icons')
],
issuer: {
test: /\.js?$/
},
use: [{
loader: '@svgr/webpack',
options: {
icon: true,
}
}, 'url-loader'],
},
{
test: /\.svg$/,
issuer: {
test: /\.js?$/
},
use: ['@svgr/webpack', 'url-loader'],
},
{
test: /\.svg$/,
use: ['url-loader'],
}
]
},
resolve: {
modules: [
SRC_DIR,
'node_modules'
],
extensions: ['.js', '.jsx', '.json'],
alias: {
core: path.join(__dirname, 'core'),
}
},
plugins: IS_PROD_BUILD ? prodPlugins : devPlugins
};
if (IS_PROD_BUILD) {
config.optimization.minimizer = [
/* Ugligy JS */
new TerserPlugin({
parallel: true,
}),
/* Minify CSS */
new OptimizeCSSAssetsPlugin({
cssProcessorOptions: {
safe: true
}
})
];
}
return config;
};