-
Notifications
You must be signed in to change notification settings - Fork 7
/
webpack.config.js
115 lines (110 loc) · 2.77 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
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const autoprefixer = require('autoprefixer');
const csso = require('postcss-csso');
const sourceDirectory = 'src';
const destinationDirectory = 'dist';
function makeCSSLoaders(useCSSModules, isDevelopment) {
return [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
sourceMap: isDevelopment,
importLoaders: 1,
...(useCSSModules && {
modules: true,
localIdentName: isDevelopment ? '[name]__[local]__[hash:base64:5]' : '[hash:base64:6]',
})
}
},
{
loader: 'postcss-loader',
options: {
plugins: [
...(isDevelopment ? [] : [csso]),
autoprefixer
],
sourceMap: isDevelopment && 'inline'
}
}
];
}
module.exports = (env, argv) => {
const {mode, analyze} = argv;
const isDevelopment = mode !== 'production';
return {
entry: `./${sourceDirectory}/index.js`,
mode,
devtool: isDevelopment ? 'inline-source-map' : undefined,
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: {
loader: 'babel-loader',
options: {
presets: [
'@babel/preset-env'
],
plugins: [
'@babel/plugin-transform-runtime',
'@babel/plugin-proposal-class-properties'
]
}
}
},
{
test: /\.css$/,
oneOf: [
{
resourceQuery: /(^|\?|&)module($|&)/i,
use: makeCSSLoaders(true, isDevelopment)
},
{
use: makeCSSLoaders(false, isDevelopment)
}
]
}
]
},
optimization: {
minimizer: [
new TerserPlugin({
sourceMap: true,
terserOptions: {
output: {
comments: false
}
}
})
]
},
output: {
path: path.resolve(__dirname, destinationDirectory),
filename: '[name].[hash].js'
},
devServer: {
contentBase: `./${destinationDirectory}`,
port: 8000
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name].[hash].css'
}),
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
template: `${sourceDirectory}/index.html`
}),
...(analyze ? [
new (require('webpack-bundle-analyzer').BundleAnalyzerPlugin)({
analyzerPort: 8001
})
] : [])
]
};
};