-
Notifications
You must be signed in to change notification settings - Fork 3
/
webpack.config.js
184 lines (173 loc) · 4.93 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
var webpack = require('webpack')
var path = require('path')
var shell = require('shelljs')
var WebpackMd5Hash = require('webpack-md5-hash')
var HtmlWebpackPlugin = require('html-webpack-plugin')
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var WebpackCleanupPlugin = require('webpack-cleanup-plugin')
var TransferWebpackPlugin = require('transfer-webpack-plugin')
var OpenBrowserPlugin = require('open-browser-webpack-plugin')
var node_modules_dir = path.resolve(__dirname, 'node_modules')
/*
* 用于分析模块的共用代码
* https://github.com/webpack/docs/wiki/optimization#multi-page-app
*/
var httpUrl = 'http://localhost:3332'
var source = {
entryAppJS: './src/app.js'
}
shell.mkdir('-p', 'dist')
var build = {
dir: 'dist',
HTML1: {
filename: 'index.html',
title: 'IFOS',
template: 'index.ejs'
}
}
var isProduction = function () {
console.log(process.env.NODE_ENV)
console.log(process.env.DEVICE)
return process.env.NODE_ENV === 'production'
}
var plugins = [
new webpack.ProvidePlugin({
_: 'lodash', // 使_变成全局变量,不用在自己文件require('lodash')了
React: 'react',
ReactDOM: 'react-dom',
classNames: 'classnames'
}),
new WebpackCleanupPlugin({
exclude: [".git/*", "dist/.git/*"]
}),
new webpack.BannerPlugin('This file is created by [email protected]'),
new webpack.HotModuleReplacementPlugin(),
new WebpackMd5Hash(),
new ExtractTextPlugin("[name].[hash].css"), //合并css
new webpack.optimize.CommonsChunkPlugin({
name: 'vendors',
filename: 'vendors.[hash].js',
minChunks: Infinity
}), //用于分析模块的共用代码
new webpack.NoErrorsPlugin(),
new HtmlWebpackPlugin({
filename: build.HTML1.filename,
title: build.HTML1.title,
template: build.HTML1.template,
chunks: ['config', 'common', 'vendors', 'app'],
inject: 'body'
}),
// new TransferWebpackPlugin([
// {from: 'src/json'}
// ], path.resolve(__dirname,'')),
new OpenBrowserPlugin({ url: httpUrl })
]
var entryApp = [
path.resolve(__dirname, source.entryAppJS)
]
if( isProduction() ) {
plugins.push(
new webpack.DefinePlugin({
"process.env": {
NODE_ENV: JSON.stringify("production")
}
})
// ,
// new webpack.optimize.UglifyJsPlugin({
// test: /(\.jsx|\.js)$/,
// mangle: {
// except: ['exports', 'require']
// },
// compress: {
// pure_getters: true,
// unsafe: true,
// unsafe_comps: true,
// screw_ie8: true,
// warnings: false
// },
// })
)
} else {
entryApp.push(
'webpack/hot/dev-server',
'webpack-dev-server/client?'+httpUrl
)
// entryMobile.push(
// 'webpack/hot/dev-server',
// 'webpack-dev-server/client?'+httpUrl
// )
}
var public_path = isProduction() ? '/' : '/'
var config = {
entry: {
app: entryApp,
config: [
'Api'
],
vendors: [
'react',
'react-router'],
common: ['Utils', 'Url', 'Auth', 'Arr', 'Str', 'Obj', 'Fetch', 'Img']
},
output: {
path: path.resolve(__dirname, build.dir),
publicPath: public_path, // 自动添加 css js 文件绝对路径
filename: '[name].[hash].js'
},
module: {
loaders: [
{
test: /\.js?$/,
loader: 'babel',
query:{
"presets": [
"es2015",
"stage-0",
"react"
],
"plugins": [
"transform-decorators-legacy",
"transform-object-rest-spread",
// "antd"
]
},
exclude: [node_modules_dir]
},{
test: /\.(less|css)$/,
loader: ExtractTextPlugin.extract('style-loader', 'css!less')
// ?importLoaders=1!autoprefixer
// importLoaders=1 解决autoprefixer css less 中存在的bug
},
{
test: /\.(jpg|png|gif|ico)$/,
loader: 'url?limit=8192'
},
{
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'url?limit=10000&mimetype=application/font-woff'
},
{
test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'file'
}
]
},
resolve:{
extensions:['','.js','.jsx'], // 可以忽略的后缀名
alias: {
//后续直接 require('Utils') 即可
Utils : path.resolve(__dirname, './src/common/utils/utils.js'),
Arr: path.resolve(__dirname, './src/common/utils/array.js'),
Str: path.resolve(__dirname, './src/common/utils/string.js'),
Obj: path.resolve(__dirname, './src/common/utils/object.js'),
Fetch: path.resolve(__dirname, './src/common/utils/fetch.js'),
Auth : path.resolve(__dirname, './src/common/utils/auth.js'),
Url : path.resolve(__dirname, './src/route/url.js'),
Api : path.resolve(__dirname, './src/api/index.js'),
Img : path.resolve(__dirname, './src/common/img/index.js')
}
},
plugins: plugins,
devtool: isProduction() ? null : 'source-map'
}
module.exports = config