-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathwebpack.config.js
More file actions
173 lines (157 loc) · 5.12 KB
/
webpack.config.js
File metadata and controls
173 lines (157 loc) · 5.12 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* Created by shiyanlin
* 810975746@qq.com
*/
const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const uglifyJsPlugin = webpack.optimize.UglifyJsPlugin;
const optimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const pxtorem = require('postcss-pxtorem');
//判断当前运行环境是开发模式还是生产模式
const nodeEnv = process.env.NODE_ENV || 'development';
const isPro = nodeEnv === 'production';
console.log("当前运行环境:", isPro ? 'production' : 'development')
// 引入页面配置文件
const pageConfig = require('./config/config.page.js');
// 引入接口代理配置文件
const proxyConfig = require('./config/config.proxy.js');
// 引入dev-server配置文件
const serverConfig = require('./config/config.server.js');
// ant 使用Icon需要
const svgDirs = [
require.resolve('antd-mobile').replace(/warn\.js$/, ''), // 1. 属于 antd-mobile 内置 svg 文件
// path.resolve(__dirname, 'src/my-project-svg-foler'), // 2. 自己私人的 svg 存放目录
];
const plugins = [
// new ExtractTextPlugin('style.css'), // 指定css文件名 打包成一个css
// 分开打包多个css
new ExtractTextPlugin({
filename: '[name].[contenthash:8].bundle.css',
allChunks: true,
}),
new webpack.LoaderOptionsPlugin({
options: {
postcss: function(){// 高清方案,将px转换为rem
return [
require('postcss-pxtorem')({
rootValue: 100,
propWhiteList: [],
})
]
}
}
}),
new webpack.ProvidePlugin({
$:"jquery",
jQuery:"jquery",
"window.jQuery":"jquery"
}),
new webpack.optimize.ModuleConcatenationPlugin() // 3.0新功能 范围提升 (Scope Hoisting )
]
if (isPro) {
plugins.push(
// css压缩
new optimizeCssAssetsPlugin({}),
// js压缩
new uglifyJsPlugin({
compress: {
warnings: false,
}
})
)
}
// entry配置
const entryConfig = {}
pageConfig.list.map(function(item, index) {
// entryConfig[item.name] = item.entry
let _obj = {
[item.name]: item.entry
}
Object.assign(entryConfig, _obj)
})
// 生成html配置
pageConfig.list.map(function(item, index) {
plugins.push(
new HtmlWebpackPlugin({
template: item.template,
title: item.title,
filename: item.filename,
chunks: [item.chunks]
})
)
})
module.exports = {
context: path.resolve(__dirname, './src'),
// 配置服务器
devServer: {
contentBase: path.resolve(__dirname, './'), // New
port: serverConfig.port,
host: serverConfig.host,
proxy: proxyConfig
},
entry: entryConfig,
output: {
path: path.resolve(__dirname, './output'),
filename: '[name].[chunkhash:8].bundle.js', // 推荐使用 ,但是--hot会报错,
// filename: '[name].[hash:8].bundle.js', // --hot时使用,不推荐
chunkFilename: '[name]-[id].[chunkhash:8].bundle.js', // 代码分割
},
module: {
rules: [{
test: /\.less$/,
// use: ['style-loader', 'css-loader', 'less-loader'], // 将css打包到js里面
// 将css单独打包,需要plugins
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
//resolve-url-loader may be chained before lesss-loader if necessary
use: [
'css-loader',
'less-loader',
]
})
}, {
test: /\.js[x]?$/,
enforce: 'pre',
use: [{
loader: 'eslint-loader',
options: { fix: true }
}],
include: path.resolve(__dirname, './src/**/*.js'),
exclude: /node_modules/
}, {
test: /\.js[x]?$/,
use: [{
loader: 'babel-loader',
options: { presets: ['es2015', 'stage-0', 'react'] }
}],
exclude: /node_modules/
}, {
test: /\.css$/,
use: ['style-loader', 'css-loader']
}, {
test: /\.(woff|woff2|eot|ttf)(\?.*$|$)/,
use: ['url-loader']
}, {
test: /\.(svg)$/i,
use: ['svg-sprite-loader'],
include: svgDirs, // 把 svgDirs 路径下的所有 svg 文件交给 svg-sprite-loader 插件处理
}, {
test: /\.(png|jpg)$/,
use: ['url-loader?limit=8192&name=images/[hash:8].[name].[ext]']
}],
},
// ant需要
resolve: {
modules: ['node_modules', path.join(__dirname, './node_modules')],
extensions: ['.web.js', '.js', '.json'], // webpack2 不再需要一个空的字符串
},
// 不需要打包的模块
externals: {
"react": 'React',
"react-dom": "ReactDOM",
"zepto": "Zepto"
},
plugins: plugins
};