-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
122 lines (119 loc) · 3.61 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
const path = require('path');
const webpack = require('webpack');
const chokidar = require('chokidar'); // hot reload for html, webpack-dev-server has include this plugin.
const htmlWebpackPlugin = require('html-webpack-plugin');
const miniCssExtractPlugin = require("mini-css-extract-plugin");
const terserPlugin = require("terser-webpack-plugin"); // minify js
const optimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const cleanWebpackPlugin = require('clean-webpack-plugin');
const devMode = process.env.NODE_ENV !== 'production';
/**
* CLI can use below variables of `env` object params:
* name - project name
* entry - js entry
* template - html template
* output - path output
* publicPath - prefix path for static file, used to on the production mode
*/
module.exports = ({
name = 'app',
entry = './index.js',
template = './index.html',
output = './dist',
publicPath = '/',
} = {}) => {
// it get the filename form whole file path without the extension
const getFilename = (pathString) => path.parse(pathString).name;
// All absolute path, here is about project root path, output, HTML actual path
const projectPath = path.resolve(__dirname, name);
const outputPath = path.resolve(__dirname, name, output);
const htmlPath = path.resolve(__dirname, name, template);
return {
context: projectPath,
entry: {
[getFilename(entry)]: entry
},
output: {
path: outputPath,
filename: devMode ? '[name].js' : '[name].[contenthash:8].js',
publicPath: devMode ? '/' : publicPath,
},
optimization: {
minimizer: [
new terserPlugin(),
new optimizeCSSAssetsPlugin()
],
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all'
}
}
}
},
devServer: {
before(app, server) {
// hot reload for html
chokidar.watch(htmlPath).on('all', function() {
server.sockWrite(server.sockets, 'content-changed');
})
},
port: 8000,
hot: true,
open: true
},
plugins: [
new htmlWebpackPlugin({
filename: getFilename(template) + '.html',
template: template
}),
new miniCssExtractPlugin({
filename: devMode ? '[name].css' : '[name].[contenthash:8].css',
}),
new cleanWebpackPlugin(devMode ? '' : outputPath),
devMode ? new webpack.HotModuleReplacementPlugin() : new webpack.HashedModuleIdsPlugin(),
],
module: {
rules: [
{
test: /\.(js)$/,
use: ['babel-loader'],
exclude: /node_modules/
},
{
test: /\.(sa|sc|c)ss$/,
use: [
devMode ? 'style-loader' : miniCssExtractPlugin.loader, // when on the development mode, using 'style-loader' can hot reload for the related file of CSS type.
'css-loader',
'postcss-loader',
'sass-loader',
]
},
{
test: /\.(html)$/,
use: [{
loader: 'html-loader',
options: {
minimize: true
}
}],
},
{
test: /\.(jpe?g|png|gif|svg)$/,
use: [
{
loader: 'url-loader',
options: {
limit: 5*1024, // if the file is less 5k size, it will be transform into base64 URIs.
outputPath: 'images',
name: devMode ? '[name].[ext]' : '[name].[contenthash:8].[ext]'
}
}
]
}
]
}
};
};