2019-01-12 03:33:27 +00:00
|
|
|
'use strict';
|
2018-03-19 09:51:47 +00:00
|
|
|
|
2020-10-19 08:43:10 +00:00
|
|
|
const { dirname } = require('path');
|
2019-01-12 03:33:27 +00:00
|
|
|
const fs = require('fs');
|
2020-10-19 08:43:10 +00:00
|
|
|
const { getProjectPath } = require('./utils/projectHelper');
|
2018-03-19 09:51:47 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
function replacePath(path) {
|
2018-03-19 09:51:47 +00:00
|
|
|
if (path.node.source && /\/lib\//.test(path.node.source.value)) {
|
2019-01-12 03:33:27 +00:00
|
|
|
const esModule = path.node.source.value.replace('/lib/', '/es/');
|
2020-10-19 08:43:10 +00:00
|
|
|
const esPath = dirname(getProjectPath('node_modules', esModule));
|
2018-03-19 09:51:47 +00:00
|
|
|
if (fs.existsSync(esPath)) {
|
2019-01-12 03:33:27 +00:00
|
|
|
path.node.source.value = esModule;
|
2018-03-19 09:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-12 01:56:32 +00:00
|
|
|
|
|
|
|
// @ant-design/icons-vue/xxx => @ant-design/icons-vue/es/icons/xxx
|
|
|
|
const antdIconMatcher = /@ant-design\/icons-vue\/([^/]*)$/;
|
|
|
|
if (path.node.source && antdIconMatcher.test(path.node.source.value)) {
|
|
|
|
const esModule = path.node.source.value.replace(
|
|
|
|
antdIconMatcher,
|
|
|
|
(_, iconName) => `@ant-design/icons-vue/es/icons/${iconName}`,
|
|
|
|
);
|
|
|
|
const esPath = dirname(getProjectPath('node_modules', esModule));
|
|
|
|
if (fs.existsSync(esPath)) {
|
|
|
|
path.node.source.value = esModule;
|
|
|
|
}
|
|
|
|
}
|
2018-03-19 09:51:47 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
function replaceLib() {
|
2018-03-19 09:51:47 +00:00
|
|
|
return {
|
|
|
|
visitor: {
|
|
|
|
ImportDeclaration: replacePath,
|
|
|
|
ExportNamedDeclaration: replacePath,
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-19 09:51:47 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
module.exports = replaceLib;
|