-
Notifications
You must be signed in to change notification settings - Fork 3
/
webpack.config.js
193 lines (179 loc) · 5.25 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
185
186
187
188
189
190
191
192
193
const path = require('path')
const webpack = require('webpack')
const precss = require('precss')
const autoprefixer = require('autoprefixer')
const HappyPack = require('happypack')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const CommonsChunkPlugin = require('webpack/lib/optimize/CommonsChunkPlugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const SWPrecacheWebpackPlugin = require('sw-precache-webpack-plugin')
const ProgressBarPlugin = require('progress-bar-webpack-plugin')
const DashboardPlugin = require('webpack-dashboard/plugin')
const isProd = process.env.NODE_ENV === 'production'
const PATHS = {
app: path.join(__dirname, 'src/index.jsx'),
build: path.join(__dirname, 'public'),
publicPath: '//oesam4fld.qnssl.com/'
// publicPath: './'
}
const SW_PRECACHE_CONFIG = {
// plugin options
filename: 'service-worker.js',
// sw-precache options
cacheId: require('./package.json').name,
verbose: true,
runtimeCaching: [
{
handler: 'cacheFirst',
urlPattern: /^https:\/\/oesam4fld\.qnssl\.com/,
},
{
handler: 'fastest',
urlPattern: /^https:\/\/api\.lostg\.com/,
},
{
handler: 'fastest',
urlPattern: /^https:\/\/jimmylv\.gtihub\.io/,
},
{
handler: 'fastest',
urlPattern: /^https:\/\/raw\.githubusercontent\.com/,
options: {
cache: {
maxEntries: 10,
name: 'articles-cache'
}
}
}
],
}
const config = {
stats: { children: false },
entry: {
app: PATHS.app,
vendor: [
// react
'react',
'react-dom',
'react-redux',
'react-router',
'react-router-redux',
'redux',
'redux-thunk',
'redux-saga',
// react components
'react-player',
'react-disqus',
'react-disqus-thread',
'react-redux-loading-bar',
// 3rd dependencies
'node-uuid',
'classnames',
'history',
'js-yaml',
'marked',
'highlight.js',
'whatwg-fetch',
'fetch-jsonp',
'es6-promise',
'firebase',
]
},
output: {
path: PATHS.build,
publicPath: isProd ? PATHS.publicPath : '',
filename: '[name].bundle.js'
},
module: {
loaders: [
{ test: /\.jsx?$/, exclude: /node_modules/, loaders: ['react-hot', 'happypack/loader'] },
{ test: /\.json$/, loader: 'json' },
{ test: /\.css$/, loader: ExtractTextPlugin.extract('style', 'css?sourceMap!postcss') },
{ test: /\.less$/, loader: ExtractTextPlugin.extract('style', 'css?sourceMap!postcss!less') },
{ test: /\.(eot|woff|woff2|ttf|svg)(\?\S*)?$/, loader: 'url?limit=100000&name=./fonts/[name].[ext]' },
{ test: /\.(png|jpe?g|gif)$/, loader: 'file?limit=8192&name=./images/[name].[ext]' }
]
},
postcss() {
return [precss, autoprefixer]
},
plugins: [
new HappyPack({
cache: true,
loaders: ['babel?cacheDirectory=true'],
threads: 5
}),
new CopyWebpackPlugin([{ from: 'cache-polyfill.js' }]),
new CommonsChunkPlugin('vendor', 'vendor.js'),
new ExtractTextPlugin('[name].css'),
new HtmlWebpackPlugin({ // 根据模板插入css/js等生成最终HTML
favicon: 'favicon.ico', // favicon路径,通过webpack引入同时可以生成hash值
filename: './index.html', // 生成的html存放路径,相对于path
template: './src/index.template', // html模板路径
inject: 'body', // js插入的位置,true/'head'/'body'/false
hash: !!isProd, // 为静态资源生成hash值
chunks: ['vendor', 'app'], // 需要引入的chunk,不配置就会引入所有页面的资源
}),
new ProgressBarPlugin()
],
resolve: {
extensions: ['', '.js', '.jsx', '.json'],
modulesDirectories: ['node_modules', 'assets/styles', 'assets/images']
},
externals: {
'jsdom': 'window',
'cheerio': 'window',
'react/lib/ExecutionEnvironment': true,
'react/lib/ReactContext': 'window',
// 'react/addons': true,
}
}
if (isProd) {
config.devtool = 'cheap-module-source-map'
config.plugins.push(
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new webpack.NoErrorsPlugin(),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
}),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.AggressiveMergingPlugin(), // Merge chunks
new SWPrecacheWebpackPlugin(SW_PRECACHE_CONFIG)
// new CompressionPlugin({
// asset: '[path].gz[query]',
// algorithm: 'gzip',
// test: /\.js$|\.css$|\.html$/,
// threshold: 10240,
// minRatio: 0.8
// })
)
} else {
config.entry.app = [
'webpack-dev-server/client?http://0.0.0.0:8088', // WebpackDevServer host and port
'webpack/hot/only-dev-server',
PATHS.app
]
config.devtool = 'cheap-module-eval-source-map'
config.devServer = {
contentBase: PATHS.build,
historyApiFallback: true,
hot: true,
inline: true,
progress: true,
stats: { children: false, colors: true, reasons: false },
port: 8088
}
config.plugins.push(
new DashboardPlugin(),
new webpack.HotModuleReplacementPlugin()
)
}
module.exports = config