-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
92 lines (83 loc) · 2.5 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
const path = require('path');
// 生产环境单独抽离css文件(不依赖js)
const ExtractTextPlugin = require("extract-text-webpack-plugin");
// 每次构建清理dist目录
const CleanWebpackPlugin = require('clean-webpack-plugin');
// 自动修改html的出口文件引用
const HtmlWebpackPlugin = require('html-webpack-plugin');
// 为了调用webpack中的HRM插件
const webpack = require('webpack');
// 判断是否为开发环境
const isDevEnv = process.env.NODE_ENV === "development";
const _ = require("lodash");
const extractSass = new ExtractTextPlugin({
filename: "[name].css",
disable: isDevEnv
});
const useHRM = true;
const tempConfig = {
entry: {
riskMap: './src/feature/main.js',
test: './src/feature/test.js'
},
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [{
// sass loader
test: /\.scss$/,
use: extractSass.extract({
use: [{
loader: "css-loader"
}, {
loader: "sass-loader"
}],
// 在开发环境使用 style-loader,将css样式添加为内联样式
fallback: "style-loader",
})
},{
// css loader
test: /\.css$/,
use: ['style-loader','css-loader']
},{
test: /\.(png|svg|jpg|gif)$/,
use: 'file-loader'
}]
},
plugins: [
// 构建前清理dist目录
new CleanWebpackPlugin(['dist']),
// 分离css文件
// ,
// 动态生成html(自动插入生成的js和css)
new HtmlWebpackPlugin({
// title: 'Output Management',
template : "src/feature/main.html",
filename: "main.html"
}),extractSass
],
devServer: {
contentBase: './dist',
openPage: 'main.html'
}
}
if (isDevEnv) {
_.defaultsDeep(tempConfig, {
devtool: "source-map",
devServer: {
hot: useHRM
}
});
if (useHRM) {
tempConfig.entry = './src/feature/main.js';
tempConfig.plugins.push(new webpack.HotModuleReplacementPlugin());
}
} else {
_.defaultsDeep(tempConfig, {
// webpack-dev-server监听文件改变重新构建并刷新浏览器
});
}
console.log(tempConfig)
module.exports = tempConfig;