-
Notifications
You must be signed in to change notification settings - Fork 8
/
webpack.config.js
151 lines (133 loc) · 3.91 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
/**
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright IBM Corporation 2018, 2020
*/
const PACKAGE = require('./package.json');
const DEVSERVER_HOST = PACKAGE.devServerHost || 'localhost';
const proxy = PACKAGE.proxy;
const APP_VERSION = PACKAGE.version;
const REACT_APP_ENVIRONMENT = process.env.NODE_ENV;
const debug = REACT_APP_ENVIRONMENT !== 'production';
const prod = REACT_APP_ENVIRONMENT === 'production';
const analyze = process.env.ANALYZE;
const OUTPUT_FOLDER = process.env.OUTPUT_FOLDER || 'dist';
const webpack = require('webpack');
const path = require('path');
const TerserPlugin = require('terser-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const CompressionPlugin = require('compression-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const copyArray = [{
from: path.resolve(__dirname, './WebContent/zlux-hooks'),
to: path.resolve(OUTPUT_FOLDER, 'zlux-hooks'),
},
{
from: path.resolve(__dirname, './WebContent/css'),
to: path.resolve(OUTPUT_FOLDER, 'css'),
},
{
from: path.resolve(__dirname, './WebContent/img'),
to: path.resolve(OUTPUT_FOLDER, 'img'),
},
{
from: path.resolve(__dirname, './WebContent/favicon.ico'),
to: path.resolve(OUTPUT_FOLDER),
},
];
const copyTask = new CopyWebpackPlugin({
patterns: copyArray,
});
const cleanTask = new CleanWebpackPlugin();
const rules = [
{
test: /\.jsx?$/,
use: {
loader:'babel-loader',
options: {
presets: ['@babel/react', '@babel/preset-env'],
plugins: [['react-html-attrs'], ['@babel/plugin-proposal-decorators',{ 'legacy': true}]],
},
},
include: [
path.join(__dirname, 'WebContent'),
path.join(__dirname, 'tests'),
],
},
{
test: /\.(png|jpg|svg)$/,
use: ['url-loader?limit=1&name=img/[name].[ext]'],
},
{
test: /\.css$/,
use: ['style-loader','css-loader'],
},
];
const htmlTask = new HtmlWebpackPlugin({ template: './WebContent/index.html' });
const entry = path.join(__dirname, 'WebContent/js/index.js');
const output = {
path: path.join(__dirname, OUTPUT_FOLDER),
filename: 'app.[hash].min.js',
};
const defineEnvConstants = {
'process.env.REACT_SYNTAX_HIGHLIGHTER_LIGHT_BUILD': true,
'process.env.NODE_ENV': JSON.stringify(REACT_APP_ENVIRONMENT),
'process.env.APP_VERSION': JSON.stringify(APP_VERSION),
};
if (debug) {
defineEnvConstants['process.env.DEVSERVER_HOSTNAME'] = JSON.stringify(DEVSERVER_HOST);
}
const definePlugin = new webpack.DefinePlugin(defineEnvConstants);
const plugins = debug ? [cleanTask, definePlugin, copyTask, htmlTask] : [cleanTask, definePlugin,
new CompressionPlugin({
threshold: 100000,
minRatio: 0.8,
}),
copyTask,
htmlTask,
];
if (analyze) {
plugins.push(new BundleAnalyzerPlugin());
}
const optimization = {
minimize: prod,
minimizer: debug ? [] : [new TerserPlugin({
terserOptions: {
ecma: 8,
compress: {
ie8: false,
warnings: false,
},
},
extractComments: false,
})],
};
const devServer = {
host: DEVSERVER_HOST,
https: true,
proxy: {
'*': {
target: proxy.target,
secure: false,
},
},
open: true,
};
module.exports = {
devtool: debug ? 'source-map' : false,
entry,
module: {
rules,
},
output,
plugins,
optimization,
mode: REACT_APP_ENVIRONMENT,
devServer,
};