-
Notifications
You must be signed in to change notification settings - Fork 2
/
webpack.config.js
165 lines (148 loc) · 4.46 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
// Generate schema.js
require('./schema.config');
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
// 独立打包
const SCRIPT_FORMAT = '[name].[chunkhash].js';
const STYLES_FORMAT = '[name].[contenthash:20].css';
// 整合打包
// const SCRIPT_FORMAT = '[name].bundle.js';
// const STYLES_FORMAT = '[name].bundle.css';
module.exports = function(env, args) {
env = env || process.env.NODE_ENV;
const config = Config(env);
return {
entry: {
app: './src/index.jsx',
},
output: {
filename: SCRIPT_FORMAT,
chunkFilename: SCRIPT_FORMAT,
path: path.resolve(__dirname, 'dist'),
publicPath: config.publicPath,
},
module: {
rules: [{
test: /\.less$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [{
loader: 'css-loader',
options: { sourceMap: true, minimize: config.minimize, localIdentName: '[hash:base64:8]' }
}, {
loader: 'postcss-loader',
options: { sourceMap: true }
}, {
loader: 'less-loader',
options: { sourceMap: true }
}],
})
}, {
test: /\.(jpg|jpeg|gif|png)$/,
use: ['file-loader'],
}, {
test: /\.(woff|woff2|eot|ttf|otf)$/,
use: ['file-loader'],
}, {
test: /\.(js|jsx)$/,
exclude: /(node_modules|bower_components)/,
use: [{
loader: 'babel-loader',
options: {
cacheDirectory: true,
}
}, {
loader: 'eslint-loader',
}],
}],
},
plugins: config.plugins,
devtool: config.devtool,
devServer: {
contentBase: './dist',
port: config.port,
host: '0.0.0.0',
// hot: true,
disableHostCheck: true,
historyApiFallback: true,
proxy: {
'/mock': {
target: 'http://localhost:3000',
pathRewrite: { '^/mock': '' },
}
}
},
resolve: {
extensions: ['.js', '.jsx'],
alias: {
library: path.resolve(__dirname, 'src/library'),
},
},
};
};
// 区分环境
function Config(env) {
return {
port: 8080,
get plugins() {
// 生成环境
if (env === 'production') {
return [
new CleanWebpackPlugin(['dist']),
new webpack.NamedChunksPlugin(),
new webpack.HashedModuleIdsPlugin({ hashDigestLength: 8 }),
new ExtractTextPlugin(STYLES_FORMAT),
new webpack.DefinePlugin({ 'process.env.NODE_ENV': JSON.stringify('production') }),
new webpack.optimize.UglifyJsPlugin({ sourceMap: true }),
new HtmlWebpackPlugin({ title: 'HEELO WORLD', template: 'index.html' }),
new webpack.ProvidePlugin({ axios: 'axios' }),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks: function(module) {
return module.context && module.context.indexOf('node_modules') !== -1;
},
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'manifest',
}),
];
}
// 开发环境
return [
new CleanWebpackPlugin(['dist']),
new ExtractTextPlugin(STYLES_FORMAT),
new HtmlWebpackPlugin({ title: 'HEELO WORLD', template: 'index.html' }),
new webpack.DefinePlugin({ 'process.env.NODE_ENV': JSON.stringify(env || 'development') }),
new webpack.ProvidePlugin({ axios: 'axios' }),
// new webpack.HotModuleReplacementPlugin(),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks: function(module) {
return module.context && module.context.indexOf('node_modules') !== -1;
},
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'manifest',
}),
];
},
get devtool() {
if (env === 'production') {
return 'source-map';
}
// BUG:https://github.com/webpack/webpack-dev-server/issues/1090
// return 'cheap-eval-source-map';
return 'source-map';
},
get minimize() {
return env === 'production';
},
// 动态计算的原因是为了确定动态加载脚本的路径
get publicPath() {
return env === 'simulation' ? '/' : '/';
},
};
};