forked from 54sword/admin.xiaoduyu.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.production.config.js
170 lines (147 loc) · 4.02 KB
/
webpack.production.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
var webpack = require('webpack')
var HtmlwebpackPlugin = require('html-webpack-plugin')
var path = require('path')
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var CleanWebpackPlugin = require('clean-webpack-plugin')
var ServiceWorkerWebpackPlugin = require('serviceworker-webpack-plugin')
var config = require('./config')
const extractSass = new ExtractTextPlugin({
filename: "[name].[hash].css"
})
module.exports = {
entry: {
app: [
'babel-polyfill',
'bootstrap/dist/css/bootstrap.min.css',
'./client/index'
],
vendors: [
'react',
'react-dom',
'react-router',
'redux',
'react-redux',
'react-document-meta',
'axios',
'jquery',
'popper.js',
'bootstrap/dist/js/bootstrap.min.js'
]
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].[hash].js',
publicPath: config.public_path + "/"
},
resolveLoader: {
moduleExtensions: ["-loader"]
},
module: {
rules: [
// js 文件解析
{
test: /\.js$/i,
exclude: /node_modules/,
loader: 'babel',
query: {
cacheDirectory: true,
plugins: [
// http://technologyadvice.github.io/es7-decorators-babel6/
'transform-decorators-legacy'
],
presets: ['es2015', 'react', 'stage-0']
}
},
// 支持解析scss
{
test: /\.scss$/,
use: extractSass.extract({
use: [{
loader: `css`,
options: {
// css module
modules: true,
localIdentName: config.class_scoped_name,
// If you are having trouble with urls not resolving add this setting.
// See https://github.com/webpack-contrib/css-loader#url
// url: false,
// 压缩css
minimize: true,
// sourceMap: true
}
}, {
loader: "sass"
}],
// use style-loader in development
fallback: "style"
})
},
// 支持解析css
{
test: /\.css$/,
use: extractSass.extract({
use: [{
loader: `css`
}],
// use style-loader in development
// fallback: "style-loader"
})
},
// 支持解析图片
{ test: /\.(png|jpg|gif)$/, loader: 'url?limit=40000' }
]
},
plugins: [
// 定义环境变量
new webpack.DefinePlugin({
// 是否是生产环境
'process.env': {
NODE_ENV: JSON.stringify(process.env.NODE_ENV),
},
// 是否是 Node
'__NODE__': JSON.stringify(process.env.__NODE__),
// 是否是开发环境
'__DEV__': JSON.stringify(process.env.NODE_ENV == 'development')
}),
// 清空打包目录
new CleanWebpackPlugin(['dist'], {
root: path.resolve(__dirname),
verbose: true,
dry: false
}),
extractSass,
// 将公共部分打包在一个文件里面
new webpack.optimize.CommonsChunkPlugin({
name: 'common',
filename: 'common.[hash].bundle.js'
}),
// new webpack.optimize.CommonsChunkPlugin({
// name: 'common-child',
// filename: 'common-child.[hash].bundle.js',
// children: true,
// deepChildren: true
// }),
new webpack.optimize.UglifyJsPlugin({
output: {
comments: false,
},
minimize: true,
compress: {
warnings: false
}
}),
new HtmlwebpackPlugin({
filename: path.resolve(__dirname, 'dist/index.ejs'),
template: 'src/view/index.html',
public_path: config.public_path + '/',
meta: '<%- meta %>',
htmlDom: '<%- html %>',
reduxState: '<%- reduxState %>',
// public_path: config.public_path + '/',
// cdn: config.qiniu.url + '/',
}),
// new ServiceWorkerWebpackPlugin({
// entry: path.join(__dirname, 'client/sw.js'),
// })
]
}