forked from ibigbug/antd-build
-
Notifications
You must be signed in to change notification settings - Fork 75
/
getWebpackCommonConfig.js
166 lines (147 loc) · 4.84 KB
/
getWebpackCommonConfig.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
166
import webpack from 'webpack';
import ExtractTextPlugin from 'extract-text-webpack-plugin';
import getBabelCommonConfig from './getBabelCommonConfig';
import getTSCommonConfig from './getTSCommonConfig';
import { existsSync } from 'fs';
import { join, resolve } from 'path';
import rucksack from 'rucksack-css';
import autoprefixer from 'autoprefixer';
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 babelQuery = getBabelCommonConfig();
const tsQuery = getTSCommonConfig();
tsQuery.declaration = false;
let theme = {};
if (pkg.theme && typeof(pkg.theme) === 'string') {
let cfgPath = pkg.theme;
// relative path
if (cfgPath.charAt(0) === '.') {
cfgPath = resolve(args.cwd, cfgPath);
}
const getThemeConfig = require(cfgPath);
theme = getThemeConfig();
} else if (pkg.theme && typeof(pkg.theme) === 'object') {
theme = pkg.theme;
}
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;
}, {});
return {
babel: babelQuery,
ts: {
transpileOnly: true,
compilerOptions: tsQuery,
},
output: {
path: join(process.cwd(), './dist/'),
filename: jsFileName,
chunkFilename: jsFileName,
},
devtool: args.devtool,
resolve: {
modulesDirectories: ['node_modules', join(__dirname, '../node_modules')],
extensions: ['', '.web.tsx', '.web.ts', '.web.jsx', '.web.js', '.ts', '.tsx', '.js', '.jsx', '.json'],
},
resolveLoader: {
modulesDirectories: ['node_modules', join(__dirname, '../node_modules')],
},
entry: pkg.entry,
node,
module: {
noParse: [/moment.js/],
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel',
query: babelQuery,
},
{
test: /\.jsx$/,
loader: 'babel',
query: babelQuery,
},
{
test: /\.tsx?$/,
loaders: ['babel', 'ts'],
},
{
test(filePath) {
return /\.css$/.test(filePath) && !/\.module\.css$/.test(filePath);
},
loader: ExtractTextPlugin.extract(
'css?sourceMap&-restructuring&-autoprefixer!' +
'postcss-loader'
),
},
{
test: /\.module\.css$/,
loader: ExtractTextPlugin.extract(
'css?sourceMap&-restructuring&modules&localIdentName=[local]___[hash:base64:5]&-autoprefixer!' +
'postcss-loader'
),
},
{
test(filePath) {
return /\.less$/.test(filePath) && !/\.module\.less$/.test(filePath);
},
loader: ExtractTextPlugin.extract(
'css?sourceMap&-autoprefixer!' +
'postcss-loader!' +
`less-loader?{"sourceMap":true,"modifyVars":${JSON.stringify(theme)}}`
),
},
{
test: /\.module\.less$/,
loader: ExtractTextPlugin.extract(
'css?sourceMap&modules&localIdentName=[local]___[hash:base64:5]&-autoprefixer!' +
'postcss-loader!' +
`less-loader?{"sourceMap":true,"modifyVars":${JSON.stringify(theme)}}`
),
},
{ test: /\.woff(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&minetype=application/font-woff' },
{ test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&minetype=application/font-woff' },
{ test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&minetype=application/octet-stream' },
{ test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: 'file' },
{ test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&minetype=image/svg+xml' },
{ test: /\.(png|jpg|jpeg|gif)(\?v=\d+\.\d+\.\d+)?$/i, loader: 'url?limit=10000' },
{ test: /\.json$/, loader: 'json' },
{ test: /\.html?$/, loader: 'file?name=[name].[ext]' },
],
},
postcss: [
rucksack(),
autoprefixer({
browsers: ['last 2 versions', 'Firefox ESR', '> 1%', 'ie >= 8', 'iOS >= 8', 'Android >= 4'],
}),
],
plugins: [
new webpack.optimize.CommonsChunkPlugin('common', commonName),
new ExtractTextPlugin(cssFileName, {
disable: false,
allChunks: true,
}),
new webpack.optimize.OccurenceOrderPlugin(),
],
};
}