ant-design-vue/antd-tools/replaceLib.js

28 lines
609 B
JavaScript
Raw Normal View History

2019-01-12 11:33:27 +08:00
'use strict';
2018-03-19 17:51:47 +08:00
2019-01-12 11:33:27 +08:00
const { join, dirname } = require('path');
const fs = require('fs');
2018-03-19 17:51:47 +08:00
2019-01-12 11:33:27 +08:00
const cwd = process.cwd();
2018-03-19 17:51:47 +08:00
2019-01-12 11:33:27 +08:00
function replacePath(path) {
2018-03-19 17:51:47 +08:00
if (path.node.source && /\/lib\//.test(path.node.source.value)) {
2019-01-12 11:33:27 +08:00
const esModule = path.node.source.value.replace('/lib/', '/es/');
const esPath = dirname(join(cwd, `node_modules/${esModule}`));
2018-03-19 17:51:47 +08:00
if (fs.existsSync(esPath)) {
2019-01-12 11:33:27 +08:00
path.node.source.value = esModule;
2018-03-19 17:51:47 +08:00
}
}
}
2019-01-12 11:33:27 +08:00
function replaceLib() {
2018-03-19 17:51:47 +08:00
return {
visitor: {
ImportDeclaration: replacePath,
ExportNamedDeclaration: replacePath,
},
2019-01-12 11:33:27 +08:00
};
2018-03-19 17:51:47 +08:00
}
2019-01-12 11:33:27 +08:00
module.exports = replaceLib;