2019-01-12 11:33:27 +08:00
|
|
|
'use strict';
|
2018-03-19 17:51:47 +08:00
|
|
|
|
2020-10-19 16:43:10 +08:00
|
|
|
const { dirname } = require('path');
|
2019-01-12 11:33:27 +08:00
|
|
|
const fs = require('fs');
|
2020-10-19 16:43:10 +08:00
|
|
|
const { getProjectPath } = require('./utils/projectHelper');
|
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/');
|
2020-10-19 16:43:10 +08:00
|
|
|
const esPath = dirname(getProjectPath('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;
|