-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
250 lines (221 loc) · 5.49 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
// ======================================================================================
const path = require('path');
// Files
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CopyPlugin = require('copy-webpack-plugin');
// Minify
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const ImageminPlugin = require('imagemin-webpack-plugin').default;
const BabelMinifyPlugin = require('babel-minify-webpack-plugin');
const TerserWebpackPlugin = require('terser-webpack-plugin');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
// ======================================================================================
const MODE = {
CURRENT: process.env.NODE_ENV,
isDEVELOPMENT: process.env.NODE_ENV === 'development',
isPRODUCTION: process.env.NODE_ENV === 'production',
};
// ======================================================================================
// Resolve
const resolvePath = (relativePath) => path.resolve(__dirname, relativePath);
// Filename
const getFilePath = (dir, title, exp) => `${dir}/${exp}/${MODE.isPRODUCTION ? `${title}.[contenthash:8].${exp}` : MODE.isDEVELOPMENT && `${title}.${exp}`}`;
const getFileChunk = (dir, title, exp) => `${dir}/${exp}/${MODE.isPRODUCTION ? `${title}.[contenthash:8].chunk.${exp}` : MODE.isDEVELOPMENT && `${title}.chunk.${exp}`}`;
// ======================================================================================
const PATH = {
ENTRY: {
MAIN: resolvePath('src/app.js'),
BABEL: '@babel/polyfill',
},
INPUT: {
HTML: resolvePath('src/index.html'),
IMG: resolvePath('src/img/'),
},
OUTPUT: {
DIR: resolvePath('dist'),
HTML: 'index.html',
JS: {
NAME: getFilePath('static', 'bundle', 'js'),
CHUCK: getFileChunk('static', 'bundle', 'js'),
},
CSS: {
TYPE: 'text/css',
NAME: getFilePath('static', 'style', 'css'),
CHUCK: getFileChunk('static', 'style', 'css'),
PUBLIC: '../../',
},
IMG: 'img/',
MEDIA: 'assets/',
FONTS: 'fonts/',
},
};
const TEST = {
SCSS: /\.s[ac]ss$/,
IMG: /\.(png|jpe?g|svg|gif)$/,
FONTS: /\.(woff|woff2|otf|ttf|eot)(\?v=\d+\.\d+\.\d+)?$/,
JS: /\.js$/,
};
// ======================================================================================
// PLUGINS
const getPlugins = () => {
const htmlWebpack = {
template: PATH.INPUT.HTML,
filename: PATH.OUTPUT.HTML,
};
MODE.isPRODUCTION ? htmlWebpack.minify = {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
} : undefined;
const plugins = [
new MiniCssExtractPlugin({
linkType: PATH.OUTPUT.CSS.TYPE,
filename: PATH.OUTPUT.CSS.NAME,
chunkFilename: PATH.OUTPUT.CSS.CHUCK,
}),
new HtmlWebpackPlugin(htmlWebpack),
new CopyPlugin({
patterns: [
{ from: PATH.INPUT.IMG, to: PATH.OUTPUT.IMG },
],
}),
];
if (MODE.isPRODUCTION) {
plugins.push(
new ImageminPlugin({
disable: true,
test: TEST.IMG,
optipng: { optimizationLevel: 3 },
jpegtran: { progressive: true },
gifsicle: { optimizationLevel: 1 },
svgo: {},
}),
new BundleAnalyzerPlugin({
analyzerMode: 'disabled',
}),
new BabelMinifyPlugin(),
);
}
return plugins;
};
// CSS LOADERS
const cssLoaders = (preProcessor) => {
const loaders = [
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: PATH.OUTPUT.CSS.PUBLIC,
},
},
{
loader: 'css-loader',
options: {},
},
{
loader: 'postcss-loader',
options: {},
},
];
if (preProcessor) {
loaders.push(preProcessor);
}
return loaders;
};
// MODULE RULES
const getModuleRules = () => {
const rules = [
{
test: TEST.SCSS,
use: cssLoaders('sass-loader'),
},
{
test: TEST.IMG,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[hash:8].[ext]',
outputPath: PATH.OUTPUT.MEDIA,
},
},
],
},
{
test: TEST.FONTS,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[hash:8].[ext]',
outputPath: PATH.OUTPUT.FONTS,
},
},
],
},
{
test: TEST.JS,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
},
},
},
];
return rules;
};
// SERVER
const getServer = () => {
const server = {
watchContentBase: true,
compress: true,
port: 4001,
hot: true,
noInfo: true,
};
return server;
};
// ======================================================================================
const CONFIG = {
mode: MODE.CURRENT,
devtool: MODE.isPRODUCTION ? 'source-map' : 'cheap-module-source-map',
entry: {
main: [PATH.ENTRY.BABEL, PATH.ENTRY.MAIN],
},
output: {
path: PATH.OUTPUT.DIR,
filename: PATH.OUTPUT.JS.NAME,
chunkFilename: PATH.OUTPUT.JS.CHUCK,
},
plugins: getPlugins(),
module: {
rules: getModuleRules(),
},
devServer: getServer(),
};
if (MODE.isPRODUCTION) {
CONFIG.output.publicPath = './';
CONFIG.optimization = {
splitChunks: {
chunks: 'all',
},
minimizer: [
new TerserWebpackPlugin(),
new OptimizeCssAssetsPlugin({
cssProcessorOptions: { discardComments: { removeAll: true } },
}),
],
};
}
// ======================================================================================
module.exports = CONFIG;
// ======================================================================================