-
Notifications
You must be signed in to change notification settings - Fork 69
/
build.after.js
227 lines (194 loc) · 6.52 KB
/
build.after.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
221
222
223
224
225
226
227
const path = require('path');
const fs = require('fs');
const webpack = require('webpack');
// const Config = require('webpack-chain');
const { getWebpackConfig } = require('build-scripts-config');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const packageInfo = require('./package.json');
/** 自定义构建脚本 - 后置 */
module.exports = ({ context, onGetWebpackConfig, registerTask, registerCliOption }) => {
const { command, commandArgs, rootDir, userConfig } = context;
const { library, libraryTarget = 'umd', libraryExport } = userConfig;
function setBuildConfig(config, theme = 'index') {
// 调整构建目录
config.output
.path(path.resolve(rootDir, 'build'))
.filename('[name].js')
.publicPath('./build/')
// 去除默认字体资源配置
config.module.rules
.delete('woff2')
.delete('ttf')
.delete('eot')
.delete('svg');
// 自定义字体资源配置
config.module
.rule('font').test(/\.(woff|woff2|eot|ttf|otf|svg)((\?|#).*)?$/)
.use('file').loader('file-loader').options({
name: '[name].[ext]',
publicPath: './',
});
// 全局变量
config
.plugin('DefinePlugin')
.use(webpack.DefinePlugin, [{
__VERSION__: JSON.stringify(packageInfo.version),
__THEME__: JSON.stringify(theme),
}]);
// 插件部分,需要 externals @alicloud/cloud-charts 本身
config.externals({
react: {
root: 'React',
commonjs: 'react',
commonjs2: 'react',
amd: 'react',
},
'react-dom': {
root: 'ReactDOM',
commonjs: 'react-dom',
commonjs2: 'react-dom',
amd: 'react-dom',
},
'@alicloud/cloud-charts': {
root: userConfig.library,
commonjs2: '@alicloud/cloud-charts',
commonjs: '@alicloud/cloud-charts',
amd: '@alicloud/cloud-charts'
},
});
if (commandArgs.development) {
config.optimization.minimize(false);
config.output
.path(path.resolve(rootDir, 'buildDev'))
.filename('[name].js')
.publicPath('./buildDev/')
}
}
registerCliOption({
name: 'online',
commands: ['start'],
});
registerCliOption({
name: 'analyzer',
commands: ['build'],
});
registerCliOption({
name: 'production',
commands: ['build'],
});
registerCliOption({
name: 'development',
commands: ['build'],
});
// 在线代理
if (commandArgs.online) {
const onlineConfig = getWebpackConfig('development');
onlineConfig.target('web');
onlineConfig.context(rootDir);
setBuildConfig(onlineConfig);
onlineConfig.output
.library(library)
.libraryExport(libraryExport)
.libraryTarget(libraryTarget);
onlineConfig
.entry('index')
.add(path.resolve(rootDir, 'src/index.scss'))
.add(path.resolve(rootDir, 'src/index.ts'));
registerTask('online-web', onlineConfig);
}
// 插件部分
if (!commandArgs.analyzer) {
const mode = command === 'start' ? 'development' : 'production';
const pluginsConfig = getWebpackConfig(mode);
pluginsConfig.target('web');
pluginsConfig.context(rootDir);
setBuildConfig(pluginsConfig);
pluginsConfig.output
.library(`${library}[name]`)
.libraryExport('default')
.libraryTarget(libraryTarget);
const pluginPath = path.resolve(rootDir, './src/plugins');
const plugins = fs.readdirSync(pluginPath);
plugins.forEach(function (plugin) {
var componentStat = fs.lstatSync(pluginPath + '/' + plugin);
if (!componentStat.isDirectory()) {
return;
}
pluginsConfig
.entry(plugin)
.add('./src/plugins/' + plugin + '/index.tsx');
});
registerTask('plugins', pluginsConfig);
}
onGetWebpackConfig(config => {
if (commandArgs.production) {
config.module
.rule('tsx')
.test(/\.tsx?$/)
.before('babel-loader')
.use('custom-loader')
.loader(path.resolve('./loader/teamix-debugger-loader.js'))
.end()
}
});
// 调整 umd 包 webpack 配置
// config 为 webpack-chain 实例
onGetWebpackConfig('component-dist', config => {
setBuildConfig(config);
if (commandArgs.analyzer) {
config
.plugin('analyzer')
.use(BundleAnalyzerPlugin);
}
});
// onGetWebpackConfig(config => {
// console.log(Config.toString(config.toConfig()));
// });
// 主题包
if (!commandArgs.online && !commandArgs.analyzer) {
const themeConfig = getWebpackConfig('production');
themeConfig.target('web');
themeConfig.context(rootDir);
setBuildConfig(themeConfig, 'dark');
themeConfig.module.rule('scss').use('sass-loader').tap(options => {
return {
...options,
// additionalData: (content, loaderContext) => {
// // More information about available properties https://webpack.js.org/api/loaders/
// const { resourcePath, rootContext } = loaderContext;
// const relativePath = path.relative(rootContext, resourcePath);
// console.log('relativePath', relativePath);
// return content;
// // if (relativePath === "styles/foo.scss") {
// // return "$value: 100px;" + content;
// // }
// //
// // return "$value: 200px;" + content;
// },
// 由于 build-plugin-component -> build-scripts-config -> sass-loader 版本是 8.x,所以使用 prependData。
// 如果更新了依赖版本,可以用 additionalData
// prependData: (loaderContext) => {
additionalData: (content, loaderContext) => {
// More information about available properties https://webpack.js.org/api/loaders/
const { resourcePath, rootContext } = loaderContext;
// const relativePath = path.relative(rootContext, resourcePath);
const themePath = path.relative(resourcePath, path.join(rootContext, './src/themes/dark.scss'));
// console.log('relativePath', relativePath, resourcePath);
// if (relativePath === 'src/index.scss') {
// return '@import "themes/dark";'
// }
return `@import "${themePath.slice(3)}"; `;
},
}
})
themeConfig.output
.library(library)
.libraryExport(libraryExport)
.libraryTarget(libraryTarget);
themeConfig
.entry('dark')
.add(path.resolve(rootDir, 'src/index.scss'))
.add(path.resolve(rootDir, 'src/index.ts'));
registerTask('theme-dark', themeConfig);
}
};