-
Notifications
You must be signed in to change notification settings - Fork 20
/
antd-alias-webpack-plugin.js
54 lines (51 loc) · 1.54 KB
/
antd-alias-webpack-plugin.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
const path = require('path');
const plugin = 'AntdAliasWebpackPlugin';
// 匹配 antd 或 antd/
const antdRegExp = /^antd[\/]*/;
const scopeRegExp = /antd-token-previewer/;
const updateResource = resource => {
resource.request = resource.request.replace(
/^antd$/,
path.join(process.cwd(), 'packages/design/es/index.js')
);
resource.request = resource.request.replace(
/^antd\/es/,
path.join(process.cwd(), 'packages/design/es')
);
resource.request = resource.request.replace(
/^antd\/lib/,
path.join(process.cwd(), 'packages/design/lib')
);
};
/* alias antd to @oceanbase/design for part of packages */
class Plugin {
constructor(options) {
this.options = options || {};
}
apply(compiler) {
compiler.hooks.normalModuleFactory.tap(plugin, factory => {
factory.hooks.beforeResolve.tap(plugin, result => {
if (
antdRegExp.test(result.request) &&
!result.request.includes('token.json') &&
!result.request.includes('token-meta.json') &&
scopeRegExp.test(result.context)
) {
updateResource(result);
}
});
factory.hooks.afterResolve.tap(plugin, result => {
const createData = result.createData;
if (
antdRegExp.test(createData.resource) &&
!createData.resource.includes('token.json') &&
!createData.resource.includes('token-meta.json') &&
scopeRegExp.test(createData.context)
) {
updateResource(result);
}
});
});
}
}
module.exports = Plugin;