-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
109 lines (105 loc) · 2.67 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
const path= require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const isProd = process.env.NODE_ENV === 'production';
console.log('NODE_ENV = ' + process.env.NODE_ENV);
const VENDOR_LIST = ['react','react-dom','semantic-ui-react'];
const config = {
entry: {
main: './src/main.js',
contact: './src/contact.js',
vendor: VENDOR_LIST
},
output: {
path: path.join( __dirname, 'dist/'),
filename: isProd ? '[name]-[chunkhash:8].js' : '[name].js',
},
module:{
rules:[
{
test: /\.s?css$/,
use: isProd ?
ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ['css-loader', 'sass-loader'],
publicPath: './dist'
}):
['style-loader', 'css-loader', 'sass-loader'],
},
{
test: /\.js$/,
exclude: /node_modules/,
use: 'babel-loader'
},
{
test: /\.(png|svg|jpg|gif)$/,
use: [
'file-loader'
]
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
use: [
'file-loader'
]
}
]
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks: function (module) {
return module.context && module.context.indexOf('node_modules') !== -1;
}
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'manifest',
minChunks: Infinity
}),
new HtmlWebpackPlugin({
title: 'Main Page',
template: './src/index.html',
favicon: '',
chunks: ['main','vendor','manifest'],
minify: {collapseWhitespace: true},
}),
new HtmlWebpackPlugin({
title: 'Contact Page',
template: './src/index.html',
filename: 'contact.html', // 指定 template 要輸出的路徑,與template同名可不設
chunks: ['contact','vendor','manifest'],
minify: {collapseWhitespace: true},
})
],
devtool: isProd ? false : 'eval-source-map',
devServer: {
contentBase: path.join(__dirname, 'dist/'), // server執行目錄
compress: true, // gzip
port: 9527,
stats: 'errors-only', // 只輸出錯誤訊息
open: true,// 自動打開瀏覽器
hot: true, // 啟動 HMR
}
}
if (isProd) { //production specify plugin
config.plugins.push(
new ExtractTextPlugin({
filename: '[name]-[hash:8].css',
disable: false, // [for dev] true; [for build] false
allChunks: true
}),
new CleanWebpackPlugin(['dist'],{ // [for build] 清空dist
verbose: true, //show message
exclude : []
})
);
}
else{ //dev specify plugin
config.plugins.push(
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin() // [for dev]HMR顯示文件名
);
}
module.exports = config;