-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.config.js
163 lines (159 loc) · 4.91 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
const webpack = require('webpack');
const path = require('path');
const fs = require('fs');
const autoprefixer = require('autoprefixer');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const nodeModulesDir = path.join(__dirname, 'node_modules');
// 热替换中间件暂时不用
const hotMiddleWareScript = 'webpack-hot-middleware/client?' +
'path=/__webpack_hmr&timeout=20000&reload=true';
const assetsPath = path.join(__dirname, 'dist', 'assets');
const publicPathConfig = {
production: 'url', // 这里配置cdn 地址
testing: '/assets',
default: 'http://127.0.0.1:4000/antcms/assets'
};
function makeConfig(env) {
var config = {};
var envStr = env || 'development';
var publicPath = publicPathConfig[envStr] || publicPathConfig.default;
config = {
context: __dirname,
// 入口配置
entry: {
app: (envStr === 'development') ? ['webpack-dev-server/client?http://127.0.0.1:4000/',
'webpack/hot/dev-server', './client/src/app.tsx'] : './client/src/app.tsx',
// login: (envStr === 'development') ?
// ['./client/src/login.jsx'] : './client/src/login.jsx',
vendor: [
'antd',
path.join('antd', 'lib', 'style', 'index.less'),
'react'
]
},
output: {
publicPath: publicPath, // 打包发布路径
path: assetsPath, // 源目录
filename: (envStr === 'production') ? '[name]-[chunkhash].js' : '[name].js', // 目标文件名
chunkFilename: (envStr === 'production') ? '[name]-[chunkhash].js' : '[name].js'
},
resolve: {
alias: {
antd: path.resolve(nodeModulesDir, 'antd'),
react: path.resolve(nodeModulesDir, 'react'),
style: path.resolve(__dirname, 'client', 'styles')
},
extensions: ['', '.js', '.jsx', '.ts', 'tsx']
},
module: {
noParse: [],
loaders: [{
test: /\.js?$/,
loader: 'babel',
include: path.join(__dirname, 'server'),
query: {
presets: ['stage-0', 'es2015-node5', 'stage-3'],
compact: false
},
exclude: /node_modules/
},
{
test: /\.tsx?$/,
loader: 'ts'
},
{
test: /\.jsx?$/,
loader: 'babel',
include: path.join(__dirname, 'client'),
plugins: [
'transform-class-properties'
],
query: {
presets: ['es2015', 'stage-0', 'react'],
env: {
development: {
presets: ['react-hmre']
}
}
},
exclude: /node_modules/
},
{
test: /\.css$/,
loader: ExtractTextPlugin.extract('style', 'css!postcss')
},
{
test: /\.less$/,
loader: ExtractTextPlugin.extract('style', 'css!postcss!less')
},
{
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'url-loader?limit=10000&minetype=application/font-woff'
},
{
test: /\.(jpg|jpeg|gif|png)$/i,
loader: 'file-loader'
}]
},
postcss: [autoprefixer({
browsers: ['last 2 versions']
})],
plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor'
}),
// new webpack.optimize.OccurenceOrderPlugin(),
new webpack.NoErrorsPlugin(),
new ExtractTextPlugin((envStr === 'production') ?
'[name]-[chunkhash].css' : '[name].css'),
new webpack.ProvidePlugin({
react: 'exports?window.react!react'
})
// new webpack.optimize.DedupePlugin(),
// new webpack.NoErrorsPlugin(),
// new webpack.ContextReplacementPlugin(/.*$/, /a^/)
],
devtool: 'inline-source-map'
// Server Configuration options
// devServer: {
// contentBase: 'client', // Relative directory for base of server
// devtool: 'eval',
// hot: true, // Live-reload
// inline: true,
// port: 3001, // Port Number
// host: '127.0.0.1' // Change to '0.0.0.0' for external facing server
// }
};
// generate manifest.json
config.plugins.push(function () {
this.plugin('done', function (stats) {
var assets = stats.toJson().assetsByChunkName;
var assetName;
var vendors;
var i;
for (i in assets) {
if (assets.hasOwnProperty(i)) {
assetName = i;
vendors = assets[i];
console.log('资源', assets[i]);
if (typeof assets[i] === 'object' ||
Object.prototype.toString.call(assets[i]) === '[object Array]') {
console.log(assets[i]);
vendors.forEach(function (src, index) {
vendors[index] = [publicPath, '/', src].join('');
console.log(src);
});
} else {
assets[i] = [publicPath, '/', assets[i]].join('');
}
}
}
fs.writeFileSync(
path.join(__dirname, 'manifest.json'),
JSON.stringify(assets)
);
});
});
return config;
}
module.exports = makeConfig;