-
Notifications
You must be signed in to change notification settings - Fork 2
/
webpack.config.lib.js
203 lines (195 loc) · 4.58 KB
/
webpack.config.lib.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const webpack = require('webpack');
const path = require('path');
const fs = require('fs');
const autoprefixer = require('autoprefixer');
// const ExtractTextWebpackPlugin = require('extract-text-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const { join } = path;
const { readdirSync, existsSync } = fs;
const jsSourcePath = join(__dirname, 'example');
const libSourcePath = join(__dirname, 'src');
const buildLib = join(__dirname, 'build/lib');
const buildDist = join(__dirname, 'build/dist');
// Lib common plugins
const plugins = [
// 与组件按需加载冲突,不拆分node__modules, 该功能由项目实现中完成拆分
// new webpack.optimize.CommonsChunkPlugin({
// name: 'vendor',
// // filename: 'vendor-[hash].js',
// filename: 'verdor.js',
// minChunks(module) {
// const context = module.context;
// return context && context.indexOf('node_modules') >= 0;
// },
// }),
new webpack.NamedModulesPlugin(),
new CleanWebpackPlugin(['build/dist', 'build/lib']),
new BundleAnalyzerPlugin(),
// new ExtractTextWebpackPlugin('react-mobile-ui.css')
];
const distPlugins = [
...plugins,
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production'),
},
}),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false,
screw_ie8: true,
conditionals: true,
unused: true,
comparisons: true,
sequences: true,
dead_code: true,
evaluate: true,
if_return: true,
join_vars: true,
},
output: {
comments: false,
},
}),
];
// Rules
const rules = [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: [
'babel-loader',
],
}, {
test: /\.css/,
// use: ExtractTextWebpackPlugin.extract({
// fallback: 'style-loader',
use: [
'style-loader',
{ loader: 'css-loader', options: { sourceMap: true } },
{
loader: 'postcss-loader',
options: {
sourceMap: true,
plugins: () => [
autoprefixer({
browsers: [
'last 3 version',
'ie >= 10',
'iOS >= 7',
'Android >= 4.1'
],
})
]
}
}]
// })
}, {
test: /\.less$/,
exclude: /node_modules/,
// use: ExtractTextWebpackPlugin.extract({
// fallback: '',
use: [
'style-loader',
{ loader: 'css-loader', options: { sourceMap: true } },
{
loader: 'postcss-loader',
options: {
sourceMap: true,
plugins: () => [
autoprefixer({
browsers: [
'last 3 version',
'ie >= 10',
'iOS >= 7',
'Android >= 4.1'
],
})
]
}
},
{
loader: 'less-loader',
options: {
sourceMap: true
}
}
]
// })
},
{
test: /\.(png|gif|jpg|svg)$/,
use: 'url-loader?limit=20480&name=assets/[name]-[hash].[ext]',
}
];
// Components path
const libEntry = (() => {
const result = {};
const componentsPath = join(__dirname, './src/components');
readdirSync(componentsPath).forEach((name) => {
const dir = join(componentsPath, name);
const jsFilePath = join(dir, './index.js');
if (existsSync(jsFilePath)) {
result[name] = jsFilePath;
}
});
return result;
})();
// Common config exclude entry and output
const webpackConfig = {
devtool: 'source-map',
context: libSourcePath,
module: {
rules,
},
resolve: {
extensions: ['.loader.js', '.js', '.jsx', '.less'],
modules: [
path.resolve(__dirname, 'node_modules'),
jsSourcePath,
],
},
plugins,
};
module.exports = [
// dist uglify
{
...webpackConfig,
entry: ['./index.js'],
output: {
path: buildDist,
publicPath: '',
filename: 'react-mobile-ui.min.js',
library: 'react-mobile-ui',
libraryTarget: 'umd',
},
plugins: distPlugins,
},
// dist
{
...webpackConfig,
entry: ['./index.js'],
output: {
path: buildDist,
publicPath: '',
filename: 'react-mobile-ui.js',
library: 'react-mobile-ui',
libraryTarget: 'umd',
},
plugins,
},
// lib
{
...webpackConfig,
entry: libEntry,
output: {
path: buildLib,
publicPath: '',
filename: '[name]/index.js',
library: 'react-mobile-ui',
libraryTarget: 'umd',
},
plugins: distPlugins,
},
];