tachybase_todo/packages/father-build/src/importLibToEs.js
2021-05-23 08:38:08 +08:00

27 lines
666 B
JavaScript
Executable File

import { join, dirname } from 'path';
import fs from 'fs';
const cwd = process.cwd();
function replacePath(path) {
if (path.node.source && /\/lib\//.test(path.node.source.value)) {
const esModule = path.node.source.value.replace('/lib/', '/es/');
const esPath = dirname(join(cwd, `node_modules/${esModule}`));
if (fs.existsSync(esPath)) {
console.log(`[es build] replace ${path.node.source.value} with ${esModule}`);
path.node.source.value = esModule;
}
}
}
function replaceLib() {
return {
visitor: {
ImportDeclaration: replacePath,
ExportNamedDeclaration: replacePath,
},
};
}
export default replaceLib;