-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathwebpack.config.js
311 lines (274 loc) · 8.21 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
'use strict';
// Modules
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const path = require('path');
process.traceDeprecation = true;
/**
* Env
* Get npm lifecycle event to identify the environment
*/
const ENV = process.env.npm_lifecycle_event;
const isTest = ENV === 'test' || ENV === 'test-watch';
const isProd = ENV === 'build';
module.exports = (function makeWebpackConfig() {
/**
* Config
* Reference: http://webpack.github.io/docs/configuration.html
* This is the object where all configuration gets set
*/
var config = {};
config.context = path.resolve(__dirname, 'app');
/**
* Plugins
* Reference: http://webpack.github.io/docs/configuration.html#plugins
* List: http://webpack.github.io/docs/list-of-plugins.html
*/
config.plugins = [
/**
* Provide plugin
* Reference: https://webpack.js.org/plugins/provide-plugin/#root
*
*/
new webpack.ProvidePlugin({
jQuery: 'jquery',
$: 'jquery',
'window.jQuery': 'jquery',
'window.DOMPurify': 'dompurify',
}),
];
/**
* Loaders
* Reference: http://webpack.github.io/docs/configuration.html#module-loaders
* List: http://webpack.github.io/docs/list-of-loaders.html
* This handles most of the magic responsible for converting modules
*/
// Initialize module
config.module = {
rules: [
{
test: require.resolve('angular'),
loader: 'exports-loader',
options: {
exports: 'single window.angular',
type: 'commonjs',
},
},
{
// JS LOADER
// Reference: https://github.com/babel/babel-loader
// Transpile .js and .jsx files using babel-loader
// Compiles ES6 and ES7 into supported by target browsers code
test: /\.(js|jsx)$/,
include: [path.resolve(__dirname, 'app', 'scripts'), path.resolve(__dirname, 'app', 'lib')],
exclude: /(node_modules|bower_components)/,
use: [
{
loader: 'babel-loader',
},
],
},
// ASSET LOADER
// Reference: https://webpack.js.org/guides/asset-modules/
{
test: /\.(png|jpg|jpeg|gif)$/,
type: 'asset/resource',
},
{
// without hash
test: /\.(svg|woff|woff2|ttf|eot)$/,
type: 'asset/resource',
generator: {
filename: '[name][ext]',
},
},
{
test: /\.html$/,
type: 'asset/source',
},
],
};
config.resolve = {
extensions: ['*', '.js', '.jsx'],
alias: {
'paho-mqtt': path.resolve(__dirname, 'node_modules/paho-mqtt/paho-mqtt.js'),
},
};
/**
* Entry
* Reference: http://webpack.github.io/docs/configuration.html#entry
*/
config.entry = {
main: {
import: [
'angular',
'oclazyload',
'jquery',
'paho-mqtt',
'bootstrap',
'angular-touch',
'angular-sanitize',
'./3rdparty/ui-bootstrap',
'spectrum-colorpicker',
'./lib/angular-spectrum-colorpicker/dist/angular-spectrum-colorpicker',
'ui-select',
'angular-elastic/elastic',
'angular-xeditable',
'angular-sortable-view/src/angular-sortable-view',
'angular-rangeslider',
'ng-toast',
'angular-translate',
'angular-translate-loader-partial',
'angular-spinkit',
'angular-ui-scroll',
'angular-dynamic-locale',
'angularjs-dropdown-multiselect',
'dompurify',
// Taken from https://github.com/angular/angular.js/tree/master/src/ngLocale
'./scripts/i18n/angular-locale_en.js',
'./scripts/i18n/angular-locale_ru.js',
'./scripts/app.js',
],
},
};
// Reference: https://webpack.js.org/plugins/split-chunks-plugin/
config.optimization = {
splitChunks: {
chunks: 'all',
minChunks: 1,
cacheGroups: {
react: {
test: /[\\/]node_modules[\\/](react|react-dom|react-select|mobx|mobx-react-lite|react-tabs|react-focus-lock|react-responsive|react-responsive-carousel)[\\/]/,
name: 'react',
chunks: 'all',
},
plotly: {
test: /[\\/]node_modules[\\/]plotly\.js-basic-dist-min[\\/]/,
name: 'plotly',
chunks: 'all',
},
codemirror: {
test: /[\\/]node_modules[\\/](@codemirror|@uiw)[\\/]/,
name: 'codemirror',
chunks: 'all',
},
jsoneditor: {
test: /[\\/]app[\\/]3rdparty[\\/]jsoneditor.js/,
name: 'jsoneditor',
chunks: 'all',
},
},
},
};
config.plugins.push(
// Reference: https://github.com/ampedandwired/html-webpack-plugin
// Render index.html
new HtmlWebpackPlugin({
filename: './index.html',
template: './index.ejs',
chunksSortMode: function (a, b) {
var order = ['polyfills', 'commons', 'libs', 'js', 'vendor', 'main'];
return order.indexOf(a) - order.indexOf(b);
},
inject: 'body',
minify: false,
// Options passed to template
// Set to true when building for stable release
stableRelease: false,
})
);
// Production specific settings
if (isProd) {
console.log('Production build');
config.mode = 'production';
/**
* Output
* Reference: https://webpack.js.org/concepts/#output
*/
config.output = {
// Absolute output directory
path: path.resolve(__dirname, 'dist'),
// Output path from the view of the page
publicPath: '/',
// Filename for entry points
filename: '[name].[chunkhash].js',
// Filename for non-entry points
chunkFilename: '[name].[chunkhash].js',
};
config.devtool = 'nosources-source-map';
config.optimization['minimize'] = true;
config.optimization['minimizer'] = [
'...',
new CssMinimizerPlugin({
minimizerOptions: {
preset: [
'default',
{
discardComments: { removeAll: true },
},
],
},
}),
];
config.plugins.push(
// Copy assets from the public folder
// Reference: https://github.com/kevlened/copy-webpack-plugin
new CopyWebpackPlugin({
patterns: [
{ from: path.join(__dirname, 'app', 'images'), to: 'images' },
{ from: path.join(__dirname, 'app', '404.html'), to: '404.html' },
{ from: path.join(__dirname, 'app', 'favicon.ico'), to: 'favicon.ico' },
{ from: path.join(__dirname, 'app', 'robots.txt'), to: 'robots.txt' },
{ from: path.join(__dirname, 'app', 'scripts/i18n'), to: 'scripts/i18n' },
],
}),
// Reference: https://github.com/webpack-contrib/mini-css-extract-plugin
// Extract CSS files from JS
new MiniCssExtractPlugin({ filename: 'css/[name].[contenthash].css' })
);
// Load styles
config.module.rules.push({
test: /\.css$/i,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader'],
});
} else {
// Development settings
config.mode = 'development';
config.output = {
// Absolute output directory
path: path.resolve(__dirname, 'dist'),
// Output path from the view of the page
// Uses dev-server in development
publicPath: 'http://localhost:8080/',
// Filename for entry points
filename: '[name].bundle.js',
// Filename for non-entry points
chunkFilename: '[name].bundle.js',
};
config.devtool = 'eval-source-map';
// Load styles
config.module.rules.push({
test: /\.css$/i,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader'],
});
config.plugins.push(
// Extract CSS files from JS
new MiniCssExtractPlugin({ filename: 'css/[name].css' })
);
}
/**
* Dev server configuration
* Reference: https://webpack.js.org/configuration/dev-server/#devserver
*/
config.devServer = {
static: {
directory: path.join(__dirname, 'app'),
},
port: 8080,
hot: true,
};
return config;
})();