-
Notifications
You must be signed in to change notification settings - Fork 36
/
webpack.config.js
144 lines (133 loc) · 3.39 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ImageminPlugin = require('imagemin-webpack-plugin').default;
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OfflinePlugin = require('offline-plugin');
const path = require('path');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const webpack = require('webpack');
const WebpackPwaManifest = require('webpack-pwa-manifest');
const config = require('./app.config.json');
const webapp = {
name: config.title,
short_name: config.short_name,
description: config.description,
background_color: config.theme_color,
theme_color: config.theme_color,
orientation: 'landscape',
icons: [
{
src: path.resolve('./src/images/logo.png'),
sizes: [96, 128, 192, 256, 384, 512]
}
]
};
const copyFiles = {
patterns: [
{ from: './src/images/', to: './images' },
{ from: './src/favicon.ico', to: './' },
],
};
const sw = {
safeToUseOptionalCaches: true,
caches: {
main: ['index.html'],
additional: ['*.js?*']
},
navigateFallbackURL: '/',
autoUpdate: true,
responseStrategy: 'cache-first',
ServiceWorker: { events: true },
AppCache: { events: true }
};
const baseWebpack = {
entry: {
app: './src/app.js'
},
output: {
publicPath: '/',
path: path.resolve(__dirname, 'dist'),
filename: '[name].bundle.js'
},
module: {
rules: [
{
test: /\.pug/,
loader: 'pug-loader',
},
{
test: /\.scss/,
use: [
'style-loader',
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: { importLoaders: 1 }
},
'postcss-loader',
{
loader: 'sass-loader'
}
]
},
{
test: /\.jpe?g$|\.gif$|\.png$|\.svg$/,
use: 'file-loader'
},
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
}
]
},
plugins: [
new webpack.DefinePlugin({
'process.env.WEBPACK_MODE': JSON.stringify(process.env.WEBPACK_MODE)
}),
new CleanWebpackPlugin({
cleanAfterEveryBuildPatterns: ['dist']
}),
new HtmlWebpackPlugin({
hash: true,
template: './src/index.pug'
}),
new MiniCssExtractPlugin({
filename: 'style.[contenthash].css',
}),
new CopyWebpackPlugin(copyFiles)
]
};
const prodStart = () => {
baseWebpack.optimization = {
minimizer: [new UglifyJsPlugin()],
};
baseWebpack.plugins.push(new ImageminPlugin({ test: /\.(jpe?g|png|gif|svg)$/i }));
baseWebpack.plugins.push(new BundleAnalyzerPlugin({ analyzerMode: 'disabled' }));
baseWebpack.plugins.push(new WebpackPwaManifest(webapp));
baseWebpack.plugins.push(new OfflinePlugin(sw));
};
const devStart = () => {
baseWebpack.devServer = {
contentBase: path.join(__dirname, 'dist'),
compress: true,
open: true,
port: 9000
};
};
module.exports = (env, options) => {
if (options.mode === 'production') {
prodStart();
}
if (options.mode === 'development') {
devStart();
}
return baseWebpack;
};