-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmake-webpack.config.js
158 lines (140 loc) · 4.78 KB
/
make-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
/**
* @file make-webpack.config.js
* @author [email protected]
*
* Webpack 配置
*/
var path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var CopyPlugin = require('copy-webpack-plugin');
var ObjectAssign = require('object-assign');
var webpackUtils = require('./tool/webpack/util');
// 项目根路径;
var ROOT_PATH = process.cwd();
/**
* 返回 webpack 配置
*
* @param {Object} config, 配置信息
* @return {Object} webpack config
*/
module.exports = function (config) {
var getPager = webpackUtils.getPager(ROOT_PATH + '/demo');
var entry = getPager.entry;
var vendor = ['react', 'react-dom'];
var babelLoader = 'babel?plugins[]=transform-runtime,presets[]=react,presets[]=es2015,presets[]=stage-0,plugins[]=transform-decorators-legacy';
var jsxLoaders = [];
if (config.devServer) {
vendor = vendor.concat([
'webpack-dev-server/client?http://' + config.devServer.host + ':' + config.devServer.port,
'webpack/hot/only-dev-server'
]);
jsxLoaders.push('react-hot');
}
jsxLoaders.push(babelLoader);
var webpackConfig = {
/**
* 配置 js 入口
*/
entry: ObjectAssign({
vendor: vendor
}, entry),
resolve: {
extensions: ['', '.js', '.jsx']
},
/**
* 打包目录配置
*/
output: {
path: path.join(process.cwd(), 'dist'),
filename: '[name].js',
chunkFilename: 'chunk.js'
},
/**
* webpack 插件集合
*/
plugins: [
// 提取样式组件
new ExtractTextPlugin('[name].css'),
// 提供共用插件
new webpack.optimize.CommonsChunkPlugin({
names: 'common'
})
],
/**
* 模块加载器
*/
module: {
preLoaders: [],
loaders: [
// js|jsx 加载器
// Reference: https://github.com/webpack/babel
{
test: /\.js$/,
loader: babelLoader,
exclude: /node_modules/
},
{
test: /\.jsx$/,
loaders: jsxLoaders,
exclude: /node_modules/
},
// sass 加载器
// Reference: https://github.com/webpack/style-loader
// Reference: https://github.com/webpack/css-loader
// Reference: https://github.com/webpack/sass-loader
// Reference: https://github.com/webpack/extract-text-webpack-plugin
{
test: /\.scss/,
loader: ExtractTextPlugin.extract('style-loader', 'css-loader!autoprefixer-loader!sass-loader', {
publicPath: '../'
})
},
// css 加载器
// Reference: https://github.com/webpack/style-loader
// Reference: https://github.com/webpack/css-loader
// Reference: https://github.com/webpack/autoprefixer-loader
// Reference: https://github.com/webpack/extract-text-webpack-plugin
{
test: /\.css$/,
loader: ExtractTextPlugin.extract('style-loader', 'css-loader', 'autoprefixer-loader?minimize', {
publicPath: '../'
})
},
// 模板 加载器
// Reference: https://github.com/webpack/html-loader
{
test: /\.html$/,
loader: 'html-loader'
},
{
// 图片加载器
test: /\.(png|jpg|gif|ttf|eot|svg|woff(2)?)(\?[=a-z0-9]+)?$/,
loader: 'url-loader?limit=10000&name=images/[hash].[ext]'
},
{
// JSON资源文件加载器
// Reference: https://github.com/webpack/json-loader
test: /\.json$/,
loader: 'json'
}
]
}
};
// 添加页面入口
webpackConfig.plugins = webpackConfig.plugins.concat(getPager.htmlPlugins);
if (config.production) {
webpackConfig.plugins.push(new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production')
}
}))
}
else {
webpackConfig.plugins = webpackConfig.plugins.concat([
new webpack.HotModuleReplacementPlugin()
]);
webpackConfig.devtool = 'cheap-module-source-map';
}
return webpackConfig;
};