forked from react-love/react-latest-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
97 lines (92 loc) · 3.1 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
var path = require('path')
var webpack = require('webpack')
var autoprefixer = require('autoprefixer')
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var argv = require('yargs').argv
const webpackServerConfig = require('./webpackServerConfig')
//判断当前运行环境是开发模式还是生产模式
const nodeEnv = process.env.NODE_ENV || 'development'
const isPro = nodeEnv === 'production'
console.log("当前运行环境:", isPro ? 'production' : 'development')
var plugins = [
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor'
}),
new webpack.DefinePlugin({
// 定义全局变量
'process.env':{
'NODE_ENV': JSON.stringify(nodeEnv)
}
})
]
var app = ['./entry']
if (isPro) {
plugins.push(
new ExtractTextPlugin({
filename: 'styles.css'
}),
new webpack.LoaderOptionsPlugin({
minimize: true,
debug: false
}),
new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
comments: false,
ie8: true
})
)
} else {
app.unshift('react-hot-loader/patch', `webpack-dev-server/client?http://${webpackServerConfig.host}:${webpackServerConfig.port}`, 'webpack/hot/only-dev-server')
plugins.push(
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(),
new webpack.NoEmitOnErrorsPlugin()
)
}
module.exports = {
context: path.resolve(__dirname, 'src'),
devtool: isPro ? 'source-map' : 'inline-source-map',
entry: {
vendor: ['react', 'react-dom'],
app: app
},
output: {
filename: '[name].js',
path: path.join(__dirname, 'build'),
publicPath: isPro ? './build/' : '/build/',
chunkFilename: '[name].js'
},
// BASE_URL是全局的api接口访问地址
plugins,
// alias是配置全局的路径入口名称,只要涉及到下面配置的文件路径,可以直接用定义的单个字母表示整个路径
resolve: {
extensions: ['.js', '.jsx', '.less', '.scss', '.css'],
modules: [
path.resolve(__dirname, 'node_modules'),
path.join(__dirname, './src')
],
alias: {
"actions": path.resolve(__dirname, "src/actions"),
"components": path.resolve(__dirname, "src/components"),
"containers": path.resolve(__dirname, "src/containers"),
"reducers": path.resolve(__dirname, "src/reducers"),
"utils": path.resolve(__dirname, "src/utils")
}
},
module: {
rules: [{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: 'babel-loader'
}, {
test: /\.(less|css)$/,
use: isPro ? ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ["css-loader", "less-loader"]
}) : ["style-loader", "css-loader", "less-loader"]
}, {
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
use: ['file-loader?limit=1000&name=files/[md5:hash:base64:10].[ext]']
}]
}
};