-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
executable file
·150 lines (127 loc) · 3.66 KB
/
webpack.config.js
File metadata and controls
executable file
·150 lines (127 loc) · 3.66 KB
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
'use strict';
var ExtractTextPlugin = require("extract-text-webpack-plugin"); //css单独打包
var CommonsChunkPlugin = require("webpack/lib/optimize/CommonsChunkPlugin");
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var OpenBrowserPlugin = require('open-browser-webpack-plugin');
var WebpackMd5Hash = require('webpack-md5-hash');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var copy = require('quickly-copy-file');
var del = require('del');
// 开发环境
var isDev = function() {
return process.env.NODE_ENV.trim() === 'development';
};
// 生产环境
var isProd = function() {
return process.env.NODE_ENV.trim() === 'production';
};
copyAndDelFiles();
module.exports = {
devtool: isProd() ? false : 'inline-source-map',
entry: {
main: './src/entry.js', //唯一入口文件
vendor: [
'react',
'react-dom',
'react-router',
'react-redux',
'redux',
'redux-thunk',
]
},
output: {
path: './build',
filename: isProd() ? '[name].[chunkhash:8].js' : '[name].js',
chunkFilename: isProd() ? '[name].chunk.[chunkhash:8].js' : '[name].chunk.js',
publicPath: isProd() ? './build/' : '/build/'
},
module: {
loaders: [
{ test: /\.js$/, loader: "jsx!babel", include: /src/},
{ test: /\.css$/, loader: ExtractTextPlugin.extract("style", "css!postcss")},
{ test: /\.scss$/, loader: ExtractTextPlugin.extract("style", "css!postcss!sass")},
{ test: /\.(png|jpg|gif)$/, loader: 'url?limit=819200'}
]
},
babel: {
presets: ['es2015', 'stage-0', 'react'],
plugins: ['transform-runtime','transform-decorators-legacy',['import', {
libraryName: 'antd',
style: 'css',
}]]
},
postcss: [
require('autoprefixer') //调用autoprefixer插件,css3自动补全
],
// devServer: {
// port: 8888,
// colors: true, //终端中输出结果为彩色
// historyApiFallback: true, //不跳转
// inline: true //实时刷新
// },
// plugins: [
// new ExtractTextPlugin('main.css'),
// new CommonsChunkPlugin({
// name: 'vendor',
// filename: 'vendor.js'
// })
// ]
plugins: getPlugins()
}
// 复制和删除文件
function copyAndDelFiles() {
var copyFile = '';
// 复制文件
if (isDev()) {
copyFile = 'src/views/index_dev.html';
}
if (isProd()) {
copyFile = 'src/views/index.html';
}
copy(copyFile, 'index.html', function(error) {
if (error) {
return console.error(error);
}
});
if (isProd()) {
del(['dist']);
}
}
// 获取配置
function getPlugins() {
var plugins = [
new webpack.DefinePlugin({
__DEV__ : isDev(),
__PROD__: isProd(),
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV.trim())
}),
new webpack.optimize.CommonsChunkPlugin('vendor', isProd() ? 'vendor.[chunkhash:8].js' : 'vendor.js'),
new ExtractTextPlugin(isProd() ? '[name].[chunkhash:8].css' : '[name].css'),
];
if (isDev()) {
plugins.push(
new OpenBrowserPlugin({ url: 'http://localhost:8080/' })
);
}
if (isProd()) {
plugins.push(
new webpack.optimize.UglifyJsPlugin({
minimize: true,
output: {
comments: false,
},
compress: {
warnings: false
}
}),
new HtmlWebpackPlugin({
title: '空格',
filename: '../index.html',
template: './src/views/index.html'
}),
new WebpackMd5Hash()
);
}
return plugins
}