forked from availchet/availchet.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
127 lines (124 loc) · 4.76 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
const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const safePostCssParser = require('postcss-safe-parser');
module.exports = webpackEnv => {
const isEnvProduction = webpackEnv === 'production';
return {
entry: ['./src/index.js'],
output: {
path: path.resolve(__dirname, './dist'),
publicPath: '/',
filename: 'static/app.bundle.js',
},
mode: isEnvProduction ? 'production' : 'development',
resolve: {
modules: [path.resolve(__dirname, 'src'), 'node_modules'],
extensions: ['.js', '.jsx', '.json', '.scss'],
},
devtool: isEnvProduction ? 'source-map' : 'inline-source-map',
devServer: {
contentBase: path.join(__dirname, 'dist'),
disableHostCheck: true,
historyApiFallback: true,
hot: true,
},
plugins: [
new CleanWebpackPlugin(),
new MiniCssExtractPlugin({
filename: 'static/css/[name].[contenthash:8].css',
chunkFilename: 'static/css/[name].[contenthash:8].chunk.css',
}),
// Generates an `index.html` file with the <script> injected.
new HtmlWebpackPlugin({
inject: true,
filename: 'index.html',
template: path.join(__dirname, 'public', 'index.html'),
}),
new CopyWebpackPlugin([
{ from: 'src/assets', to: 'assets' },
{ from: 'public', to: '' },
]),
],
optimization: {
minimize: isEnvProduction,
minimizer: [
// This is only used in production mode
new OptimizeCSSAssetsPlugin({
cssProcessorOptions: {
parser: safePostCssParser,
map: {
inline: false,
annotation: true,
},
},
cssProcessorPluginOptions: {
preset: ['default', { minifyFontValues: { removeQuotes: false } }],
},
}),
],
// Automatically split vendor and commons
splitChunks: {
chunks: 'all',
name: false,
},
// Keep the runtime chunk separated to enable long term caching
runtimeChunk: {
name: entrypoint => `runtime-${entrypoint.name}`,
},
},
module: {
rules: [
{
test: /\.(js|jsx)?$/,
exclude: /node_modules/,
use: {
loader: require.resolve('babel-loader'),
options: {
// Babel configuration is in babel.config.json
// because jest requires it to be there.
cacheDirectory: true,
cacheCompression: false,
compact: isEnvProduction,
},
},
},
{
test: /\.svg$/,
use: ['@svgr/webpack'],
},
// "file" loader makes sure the assets get served by WebpackDevServer.
{
test: /.(woff|woff2|eot|ttf|otf)$/,
exclude: /node_modules/,
loader: 'file-loader',
options: {
limit: 10000,
name: 'static/media/[name].[hash:8].[ext]',
},
},
// "url" loader works like "file" loader except that it embeds assets
// smaller than specified limit in bytes as data URLs to avoid requests.
{
test: /.(bmp|gif|jpe?g|png)$/,
exclude: /node_modules/,
loader: require.resolve('url-loader'),
options: {
limit: 10000,
name: 'static/media/[name].[hash:8].[ext]',
},
},
{
test: /\.css$/,
use: [
isEnvProduction ? MiniCssExtractPlugin.loader : 'style-loader',
'css-loader',
],
},
],
},
};
};