forked from ibigbug/antd-build
-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathgetWebpackCommonConfig.js
More file actions
175 lines (157 loc) · 4.37 KB
/
getWebpackCommonConfig.js
File metadata and controls
175 lines (157 loc) · 4.37 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
174
175
import webpack from 'webpack';
import ExtractTextPlugin from 'extract-text-webpack-plugin';
import CaseSensitivePathsPlugin from 'case-sensitive-paths-webpack-plugin';
import { existsSync } from 'fs';
import { join } from 'path';
import rucksack from 'rucksack-css';
import autoprefixer from 'autoprefixer';
import FriendlyErrorsWebpackPlugin from 'friendly-errors-webpack-plugin';
import notifier from 'node-notifier';
import getBabelCommonConfig from './getBabelCommonConfig';
/* eslint quotes:0 */
export default function getWebpackCommonConfig(args) {
const pkgPath = join(args.cwd, 'package.json');
const pkg = existsSync(pkgPath) ? require(pkgPath) : {};
const jsFileName = args.hash ? '[name]-[chunkhash].js' : '[name].js';
const cssFileName = args.hash ? '[name]-[chunkhash].css' : '[name].css';
const commonName = args.hash ? 'common-[chunkhash].js' : 'common.js';
const silent = args.silent === true;
const babelOptions = getBabelCommonConfig();
const postcssOptions = {
sourceMap: true,
plugins: [
rucksack(),
autoprefixer({
browsers: ['last 2 versions', 'Firefox ESR', '> 1%', 'ie >= 8', 'iOS >= 8', 'Android >= 4'],
}),
],
};
const emptyBuildins = [
'child_process',
'cluster',
'dgram',
'dns',
'fs',
'module',
'net',
'readline',
'repl',
'tls',
];
const browser = pkg.browser || {};
const node = emptyBuildins.reduce((obj, name) => {
if (!(name in browser)) {
return { ...obj, ...{ [name]: 'empty' } };
}
return obj;
}, {});
const config = {
babel: babelOptions,
postcss: postcssOptions,
output: {
path: join(process.cwd(), './dist/'),
filename: jsFileName,
chunkFilename: jsFileName,
},
devtool: args.devtool,
resolve: {
modules: ['node_modules', join(__dirname, '../node_modules')],
extensions: ['.web.tsx', '.web.ts', '.web.jsx', '.web.js', '.ts', '.tsx', '.js', '.jsx', '.json'],
},
entry: pkg.entry,
node,
module: {
noParse: [/moment.js/],
rules: [
{
test: /\.woff(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url-loader',
options: {
limit: 10000,
minetype: 'application/font-woff',
},
},
{
test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url-loader',
options: {
limit: 10000,
minetype: 'application/font-woff',
},
},
{
test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url-loader',
options: {
limit: 10000,
minetype: 'application/octet-stream',
},
},
{ test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url-loader',
options: {
limit: 10000,
minetype: 'application/vnd.ms-fontobject',
},
},
{
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url-loader',
options: {
limit: 10000,
minetype: 'image/svg+xml',
},
},
{
test: /\.(png|jpg|jpeg|gif)(\?v=\d+\.\d+\.\d+)?$/i,
loader: 'url-loader',
options: {
limit: 10000,
},
},
{
test: /\.html?$/,
loader: 'file-loader',
options: {
name: '[name].[ext]',
},
},
],
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: 'common',
filename: commonName,
}),
new ExtractTextPlugin({
filename: cssFileName,
disable: false,
allChunks: true,
}),
new CaseSensitivePathsPlugin(),
new FriendlyErrorsWebpackPlugin({
onErrors: (severity, errors) => {
if (silent) return;
if (severity !== 'error') {
notifier.notify({
title: 'ant tool',
message: 'warn',
contentImage: join(__dirname, '../assets/warn.png'),
sound: 'Glass',
});
return;
}
const error = errors[0];
notifier.notify({
title: 'ant tool',
message: `${severity} : ${error.name}`,
subtitle: error.file || '',
contentImage: join(__dirname, '../assets/fail.png'),
sound: 'Glass',
});
},
}),
],
};
return config;
}