-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
165 lines (155 loc) · 4.83 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
'use strict';
const path = require('path');
const webpack = require('webpack');
const util = require('./util');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const ManifestPlugin = require('webpack-manifest-plugin');
const autoprefixer = require('autoprefixer');
const frontBase = './front';
let buildPath = path.resolve(__dirname, 'static/build');
let buildEnv = util.isDEV || util.isLOCAL ? '/dev' :
util.isYZ || util.isRelease ? '/release' :
util.isQA ? '/qa' :
'/dev';
let plugins = [],
entry = {};
/**
* ==============
* plugins object
* ==============
*/
let packPlugins = {
//order: new webpack.optimize.OccurrenceOrderPlugin(), //wepack2 中默认加载
// 全局变量
global: new webpack.DefinePlugin({
__DEV__: JSON.stringify(util.isDEV),
__LOCAL__: JSON.stringify(util.isLOCAL),
__QA__: JSON.stringify(util.isQA),
__YZ__: JSON.stringify(util.isYZ),
__RELEASE__: JSON.stringify(util.isRelease)
}),
hot: new webpack.HotModuleReplacementPlugin(),
namedModule: new webpack.NamedModulesPlugin(),
noError: new webpack.NoEmitOnErrorsPlugin(),
manifest: new ManifestPlugin(),
css: new ExtractTextPlugin({
filename: util.isDEV || util.isLOCAL ? '[name].css' : '[name].[hash:8].css',
allChunks: false
}),
uglify: new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
compress: {
warnings: false
}
}),
loaderOption: new webpack.LoaderOptionsPlugin({
context: __dirname
})
}
/**
* =======================================
* Custom config for different environement
* =======================================
*/
if (util.isLOCAL || util.isDEV) {
//plugins
plugins.push(packPlugins.hot);
plugins.push(packPlugins.namedModule);
plugins.push(packPlugins.noError);
} else {
plugins.push(packPlugins.manifest);
}
if (util.isYZ || util.isRelease) {
plugins.push(packPlugins.uglify);
}
plugins.push(packPlugins.css);
plugins.push(packPlugins.loaderOption);
plugins.push(packPlugins.global);
/**
* webpak config
* 'webpack-hot-middleware/client?reload=true', './src/index.js'
*/
let packConfig = {
entry: {},
output: {
path: buildPath + buildEnv,
filename: util.isDEV ? '[name].js' : '[name].[hash:8].js',
/**
* publicPath: 针对css等文件中的相对路径,如背景图片做处理
* 使用publicPath给定的路径来替换图片相对路径
* 比如css中引用背景图片地址为’./assets/xx.png‘ 则打包后css中背景图片地址变为 publicPath + ’xx.png‘
* 验证生产环境建议使用 静态服务器地址 比如:http://static.example.com/
*/
publicPath: util.getPublicPath()
},
plugins: plugins,
/** 不推荐忽略 -loader */
resolveLoader: {
moduleExtensions: ["-loader"],
},
resolve: {
modules: ['node_modules'],
extensions: ['.js', '.json', '.css', '.scss'],
},
devtool: false,//util.isDEV || util.isQA || utili.isLOCAL ? 'source-map' : false,
//watch: util.isDEV,
module: {
rules: [
//babel
{
test: /\.js$/,
loader: 'babel',
include: __dirname + '/src'
},
// css
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style',
use: ['css']
})
},
//scss
{
test: /\.scss$/,
use: ExtractTextPlugin.extract({
fallback: 'style',
use: [
{ loader: 'css' },
{
loader: 'postcss',
options: {
plugins: [ autoprefixer({ browsers: ['> 5%'] }) ]
}
},
{ loader: 'sass' }
]
})
},
// img
{
test: /\.(png|jpg|jpeg|gif)$/,
loader: 'url',
options: {
limit: 4096,
name: util.isDEV ? '[name].[ext]' : '[name].[ext]?[hash:16]'
}
}
]
}
}
/**
* 动态配置webpack入口
*/
function addEntry(config, name) {
config.entry[name] = [path.resolve(frontBase, name)];
if (util.isLOCAL || util.isDEV) {
config.entry[name].push('webpack-hot-middleware/client?reload=true');
}
}
/**
* 添加入口文件
*/
addEntry(packConfig, 'home');
addEntry(packConfig, 'detail');
module.exports = packConfig;