forked from longlongago2/rm-calendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
220 lines (209 loc) · 5.68 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
const path = require('path');
const chalk = require('chalk');
const merge = require('webpack-merge');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const webpack = require('webpack');
const localIP = require('quick-local-ip').getLocalIP4();
/* eslint-disable prefer-destructuring, no-console */
const log = console.log;
module.exports = function webpackConfig(env, argv) {
// 打包方式
const buildType = argv['build-type'];
// 打印信息
log(`
${chalk.bgBlue(' 构建信息:')}
${chalk.green('mode')}: ${chalk.yellow(env.production ? 'production' : 'development')}
${chalk.green('build-type')}: ${chalk.yellow(buildType)}
`);
// 基础配置
const postcssPlugins = () => [
require('postcss-flexbugs-fixes'),
require('postcss-preset-env')({
autoprefixer: {
flexbox: 'no-2009',
},
stage: 3,
}),
];
const baseConfig = {
mode: env.production ? 'production' : 'development',
stats: {
// 统计信息
all: false, // 下文没有出现的设置均默认为false
assets: true,
builtAt: true,
colors: true,
errors: true,
errorDetails: true,
},
entry: {
'rm-calendar': './src/index.js',
},
module: {
rules: [
{
test: /\.html$/,
loader: 'html-loader',
},
{
test: /\.(js|jsx)$/,
loader: 'babel-loader',
options: {
cacheDirectory: true,
presets: [['react-app', { helpers: true }]],
plugins: buildType === 'dev' ? ['react-hot-loader/babel'] : [],
},
},
{
test: /\.css$/,
use: [
buildType !== 'dev' ? MiniCssExtractPlugin.loader : 'style-loader',
{
loader: 'css-loader',
options: { importLoaders: 1, modules: false },
},
{
loader: 'postcss-loader',
options: {
ident: 'postcss',
plugins: postcssPlugins,
},
},
],
},
{
test: /\.less$/,
use: [
buildType !== 'dev' ? MiniCssExtractPlugin.loader : 'style-loader',
{
loader: 'css-loader',
options: {
importLoaders: 2,
modules: true,
localIdentName: '[path][name]__[local]--[hash:base64:5]',
},
},
{
loader: 'postcss-loader',
options: {
ident: 'postcss',
plugins: postcssPlugins,
},
},
'less-loader',
],
},
],
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name].css',
}),
],
};
// dist 配置
if (buildType === 'dist') {
process.env.BABEL_ENV = 'production';
process.env.NODE_ENV = 'production';
return merge.smart(baseConfig, {
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js',
publicPath: '/',
library: 'RMCalendar',
libraryTarget: 'var',
},
externals: {
react: 'React',
},
});
}
// lib 配置
if (buildType === 'lib') {
process.env.BABEL_ENV = 'production';
process.env.NODE_ENV = 'production';
return merge.smart(baseConfig, {
output: {
path: path.resolve(__dirname, 'lib'),
filename: '[name].js',
publicPath: '/',
library: 'RMCalendar',
libraryTarget: 'umd',
},
externals: {
react: {
commonjs: 'react',
commonjs2: 'react',
amd: 'react',
root: 'React', // 指向全局变量
},
moment: {
commonjs: 'moment',
commonjs2: 'moment',
amd: 'moment',
root: 'moment', // 指向全局变量
},
},
plugins: [
new CopyWebpackPlugin([
{
from: './src/*.less',
to: './theme-less',
flatten: true,
},
]),
],
});
}
// dev 配置
log(`
${chalk.bgBlue(' Dev Server 已启动:')}
${chalk.green('Local')}:${chalk.yellow('http://localhost:8081')}
${chalk.green('Network')}:${chalk.yellow(`http://${localIP}:8081`)}
`);
process.env.BABEL_ENV = 'development';
process.env.NODE_ENV = 'development';
return merge.smart(baseConfig, {
entry: './example/index.js',
devtool: 'none',
devServer: {
contentBase: './example/dist',
host: '0.0.0.0',
useLocalIp: true,
port: 8081,
hot: true,
compress: false,
noInfo: true,
open: true,
overlay: true, // 当出现编译器错误或警告时,在浏览器中显示全屏覆盖层显示信息
disableHostCheck: true,
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('development'),
BABEL_ENV: JSON.stringify('development'),
},
}),
new CleanWebpackPlugin(['./example/dist'], { verbose: false }),
new HtmlWebpackPlugin({
template: 'example/index.html',
minify: false,
}),
new webpack.HotModuleReplacementPlugin(),
],
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'example/dist'),
},
resolve: {
modules: [
'node_modules',
path.join(__dirname, 'lib'), // 作为依赖modules导入:可简写模块加载路径 ./lib路径可省略
],
},
});
};