This repository has been archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwebpack.config.babel.js
167 lines (162 loc) · 3.97 KB
/
webpack.config.babel.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
import 'dotenv/config';
import path from 'path';
import webpack from 'webpack';
import { CLIEngine } from 'eslint';
import HtmlPlugin from 'html-webpack-plugin';
import TerserPlugin from 'terser-webpack-plugin';
import FaviconsPlugin from 'favicons-webpack-plugin';
import StylelintPlugin from 'stylelint-webpack-plugin';
import MiniCSSExtractPlugin from 'mini-css-extract-plugin';
import { CleanWebpackPlugin as CleanPlugin } from 'clean-webpack-plugin';
import OptimizeCSSAssetsPlugin from 'optimize-css-assets-webpack-plugin';
import { BundleStatsWebpackPlugin as BundleStatsPlugin } from 'bundle-stats';
const analyzeBundle = Boolean(process.env.ANALYZE_BUNDLE);
const compareBundle = Boolean(process.env.COMPARE_BUNDLE);
const dev = process.env.NODE_ENV === 'development';
const apiUrl = process.env.API_URL;
const plugins = [
new CleanPlugin(),
new StylelintPlugin({
configFile: '.stylelintrc',
context: 'src',
files: '**/*.scss',
failOnError: true,
quiet: false,
syntax: 'scss'
}),
new HtmlPlugin({
template: './src/index.html'
}),
new FaviconsPlugin('./src/media/favicon.png'),
new MiniCSSExtractPlugin({
filename: '[name].css'
}),
new webpack.DefinePlugin({
API_URL: JSON.stringify(dev || !apiUrl ? 'http://localhost:3000' : apiUrl)
})
];
if (dev) {
plugins.push(new webpack.HotModuleReplacementPlugin());
}
if (compareBundle || analyzeBundle) {
plugins.push(
new BundleStatsPlugin({
compare: compareBundle
})
);
}
export default {
mode: dev ? 'development' : 'production',
devtool: dev ? 'cheap-module-eval-source-map' : 'cheap-module-source-map',
entry: './src/index.js',
devServer: {
compress: dev,
open: true,
overlay: true,
historyApiFallback: true,
hot: dev,
port: 9000
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: [
'babel-loader',
{
loader: 'eslint-loader',
options: {
formatter: CLIEngine.getFormatter('stylish')
}
}
]
},
{
test: /\.(sa|sc|c)ss$/,
use: [
dev ? 'style-loader' : MiniCSSExtractPlugin.loader,
'css-loader',
{
loader: 'postcss-loader',
options: {
postcssOptions: {
ident: 'postcss',
plugins: [
require('autoprefixer'),
require('postcss-flexbugs-fixes')
]
},
sourceMap: dev
}
},
'sass-loader'
]
},
{
test: /\.html$/,
use: [
{
loader: 'html-loader',
options: { minimize: true }
}
]
},
{
test: /\.(png|svg|jpe?g|webp)$/,
use: ['file-loader']
}
]
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js',
chunkFilename: '[name].js',
publicPath: '/'
},
plugins,
resolve: {
extensions: ['.js', '.json'],
modules: ['node_modules', 'src']
},
optimization: {
minimizer: [
new TerserPlugin({
terserOptions: {
ecma: 9
}
}),
new OptimizeCSSAssetsPlugin({})
],
splitChunks: {
cacheGroups: {
default: false,
vendors: false,
vendor: {
chunks: 'initial',
name: 'vendor',
priority: -4,
test: /[\\/]node_modules[\\/]/
},
react: {
chunks: 'initial',
name: 'vendor-react',
priority: -1,
test: /[\\/]node_modules[\\/](react)/
},
redux: {
chunks: 'initial',
name: 'vendor-redux',
priority: -3,
test: /[\\/]node_modules[\\/](@?redux)/
},
ui: {
chunks: 'initial',
name: 'ui-assets',
priority: -2,
test: /[\\/]node_modules[\\/](popper|@fortawesome)/
}
}
}
}
};