-
Notifications
You must be signed in to change notification settings - Fork 8
/
webpack.config.babel.js
118 lines (111 loc) · 2.71 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
import path from 'path'
import AssetsPlugin from 'assets-webpack-plugin'
import CopyPlugin from 'copy-webpack-plugin'
import MiniCssExtractPlugin from 'mini-css-extract-plugin'
import OptimizeCssAssetsPlugin from 'optimize-css-assets-webpack-plugin'
import TerserPlugin from 'terser-webpack-plugin'
const APP_FILE_NAME = 'app'
const VENDORS_FILE_NAME = 'lib'
const PATH_SRC = './app'
const PATH_DIST = './static'
const NODE_MODULES_NAME = 'node_modules'
function getPath(filePath) {
return path.resolve(__dirname, filePath)
}
module.exports = (env, options) => {
const isDevelopment = (options.mode === 'development')
const filename = isDevelopment ? '[name]' : '[name]-[contenthash:4]'
const exclude = new RegExp(NODE_MODULES_NAME)
return {
entry: {
[APP_FILE_NAME]: getPath(`${PATH_SRC}/scripts/index.js`),
},
output: {
filename: `${filename}.js`,
path: getPath(PATH_DIST),
},
resolve: {
modules: [NODE_MODULES_NAME],
},
module: {
rules: [
{
test: /\.js$/,
exclude,
enforce: 'pre',
loader: 'eslint-loader',
},
{
test: /\.js$/,
exclude,
use: {
loader: 'babel-loader',
},
},
{
test: /\.css$/,
use: [
{
loader: 'css-loader',
},
],
},
{
test: /\.scss$/,
exclude,
use: [
{
loader: MiniCssExtractPlugin.loader,
},
{
loader: 'css-loader',
},
{
loader: 'postcss-loader',
},
{
loader: 'sass-loader',
options: {
sassOptions: {
outputStyle: isDevelopment ? 'nested' : 'compressed',
},
sourceMap: isDevelopment,
},
},
],
},
],
},
optimization: {
minimizer: [
new TerserPlugin({
sourceMap: isDevelopment,
}),
new OptimizeCssAssetsPlugin(),
],
splitChunks: {
cacheGroups: {
commons: {
test: new RegExp(NODE_MODULES_NAME),
chunks: 'all',
name: VENDORS_FILE_NAME,
},
},
},
},
devtool: isDevelopment ? 'cheap-module-source-map' : undefined,
plugins: [
new AssetsPlugin(),
new CopyPlugin({
patterns: [{
flatten: true,
from: getPath(`${PATH_SRC}/images/*`),
to: getPath(PATH_DIST),
}],
}),
new MiniCssExtractPlugin({
filename: `${filename}.css`,
}),
],
}
}