/
webpack.build.config.js
119 lines (115 loc) · 2.95 KB
/
webpack.build.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
/**
* Created by sean@ihuanqu.com on 2018/8/30.
*/
const path = require("path");
const webpack = require("webpack");
const CleanWebpackPlugin = require('clean-webpack-plugin');
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const HtmlWebPackPlugin = require("html-webpack-plugin");
const CopyWebpackPlugin = require('copy-webpack-plugin');
const SRC_PATH = path.resolve(__dirname, 'src');
const DIST_PATH = path.resolve(__dirname, 'dist');
const PUBLIC_PATH = path.resolve(__dirname, 'public');
const CDN_PATH = '';
module.exports = {
entry:{
background:[path.resolve(SRC_PATH,"background")],
contentscript:[path.resolve(SRC_PATH,"contentscript")],
inject:[path.resolve(SRC_PATH,"inject")],
main:["babel-polyfill",path.resolve(SRC_PATH,"index")]
},
output:{
path: DIST_PATH,
publicPath: CDN_PATH,
filename: 'scripts/[name].js',
chunkFilename: 'scripts/[name].js'
},
devServer: {
contentBase: "dist",
//热更新
hot:true,
overlay: true
},
optimization: {
minimizer: [
new UglifyJsPlugin({
cache: true,
parallel: true,
sourceMap: true // set to true if you want JS source maps
}),
new OptimizeCSSAssetsPlugin({})
]
},
plugins: [
new webpack.DefinePlugin({
"process.env": {
NODE_ENV: JSON.stringify("production")
}
}),
new CleanWebpackPlugin('dist', {
root: __dirname,
verbose: true,
dry: false
}),
new webpack.optimize.OccurrenceOrderPlugin(),
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: "styles/main.css",
chunkFilename: "[id].css"
}),
new HtmlWebPackPlugin({
template:"./src/index.html",
filename:"index.html",
chunks: ['main'],
inject: 'body',
minify: {
removeComments: true,//移除html中的注释
collapseWhitespace: true//删除空白符和换行符
}
}),
new CopyWebpackPlugin([
{
from:PUBLIC_PATH,
to:DIST_PATH,
ignore: ['.*']
}
])
],
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.css$/,
use:[MiniCssExtractPlugin.loader,"css-loader"]
},
{
test: /\.less$/,
use: [
{
loader: MiniCssExtractPlugin.loader
},
{
loader: 'css-loader'
},
{
loader: 'less-loader'
}
]
},
{
test:/\.(jpg|png|svg|gif)$/,
//多个loader需要从后到前进行解析(大于1000kb打包)
use:["url-loader?limit=1000&name=images/[name]-[hash:8].[ext]"]
}
]
}
};