forked from 54sword/admin.xiaoduyu.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.development.config.js
165 lines (142 loc) · 4.15 KB
/
webpack.development.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
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].css",
disable: true,
allChunks: true,
ignoreOrder: true
})
module.exports = {
devtool: 'source-map',
entry: {
app: [
// 让客户端支持 async 和 await
'babel-polyfill',
'bootstrap/dist/css/bootstrap.min.css',
'./client/index',
// 热更新
'webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000&reload=true'
],
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].bundle.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']
}
},
{
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"
})
},
{
test: /\.css$/,
use: extractSass.extract({
use: [{
loader: `css`,
}],
// use style-loader in development
fallback: "style"
})
},
{ 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')
}),
extractSass,
// 将多个入口 chunk 的公共模块。通过将公共模块拆出来
new webpack.optimize.CommonsChunkPlugin({
name: 'common',
filename: 'common.bundle.js'
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'common-child',
filename: 'common-child.bundle.js',
children: true,
deepChildren: true
}),
// 创建index.ejs模版文件
new HtmlwebpackPlugin({
filename: path.resolve(__dirname, 'dist/index.ejs'),
template: 'src/view/index.html',
public_path: config.public_path + '/',
// cdn: config.qiniu.url + '/',
meta: '<%- meta %>',
htmlDom: '<%- html %>',
reduxState: '<%- reduxState %>'
}),
// 当开启 HMR 的时候使用该插件会显示模块的相对路径,建议用于开发环境。
new webpack.NamedModulesPlugin(),
// 开发环境的热更新
new webpack.HotModuleReplacementPlugin(),
// 启用此插件后,webpack 进程遇到错误代码将不会退出
new webpack.NoEmitOnErrorsPlugin()
// new ServiceWorkerWebpackPlugin({
// entry: path.join(__dirname, 'client/sw.js'),
// })
]
}