-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.base.js
executable file
·141 lines (137 loc) · 3.49 KB
/
webpack.base.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
const webpack = require('webpack');
const path = require('path');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const autoprefixer = require('autoprefixer');
const HTMLWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const NODE_ENV = process.env.NODE_ENV;
const devMode = NODE_ENV !== 'production';
const isTest = NODE_ENV === 'test';
const babelConfig = require('./babel.config');
module.exports = {
output: {
filename: devMode ? 'bundle.js' : 'bundle.[hash].js',
chunkFilename: devMode
? '[name].lazy-chunk.js'
: '[name].lazy-chunk.[hash].js',
path: path.resolve(__dirname, 'public/dist'),
publicPath: '/',
},
resolve: {
extensions: ['.js', '.jsx', '.json', '.scss', 'css'],
},
node: {
fs: 'empty',
},
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: [
{
loader: 'babel-loader',
options: babelConfig,
},
],
},
{
test: /\.(sa|sc|c)ss$/,
exclude: /node_modules/,
use: [
{
loader: devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
},
{
loader: 'css-loader',
options: {
minimze: true,
sourceMap: devMode,
importLoaders: 1,
},
},
{
loader: 'postcss-loader',
options: {
indent: 'postcss',
plugins: [autoprefixer()],
sourceMap: devMode,
},
},
{
loader: 'sass-loader',
options: {
sourceMap: devMode,
includePaths: ['client/styles/main.scss'],
},
},
],
},
{
test: /\.html$/,
loader: 'html-loader',
options: {
attrs: ['img:src'],
},
},
{
test: /\.(jpe?g|png|gif|ico)$/,
loader: 'file-loader',
options: {
name: devMode ? '[name].[ext]' : '[name].[hash].[ext]',
},
},
{
test: /\.svg$/,
loader: 'file-loader',
options: {
name: devMode ? '[name].[ext]' : '[name].[hash].[ext]',
},
},
],
},
optimization: {
runtimeChunk: 'single',
splitChunks: {
chunks: 'all',
cacheGroups: {
vendors: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
priority: -10,
},
default: {
minChunks: 2,
priority: -20,
reuseExistingChunk: true,
},
},
},
},
plugins: [
new CleanWebpackPlugin(['public/dist']),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(NODE_ENV),
},
}),
new HTMLWebpackPlugin({
template: './public/index.html',
favicon: './static/favicons/favicon.ico',
}),
new MiniCssExtractPlugin({
filename: devMode ? '[name].css' : '[name].[chunkhash].css',
chunkFilename: devMode ? '[id].css' : '[id].[chunkhash].css',
}),
new CopyWebpackPlugin([
{ from: `${__dirname}/static`, to: `${__dirname}/public/dist` },
]),
isTest
? new BundleAnalyzerPlugin({
generateStatsFile: true,
})
: null,
].filter(Boolean),
};