UNPKG

@ant-design/tools

Version:
33 lines (32 loc) 1.05 kB
'use strict'; import { dirname } from 'path'; import fs from 'fs'; import { getProjectPath } from "./utils/projectHelper"; function replacePath(path) { const source = path.node.source; if (source && /\/lib\//.test(source.value)) { const esModule = source.value.replace('/lib/', '/es/'); const esPath = dirname(getProjectPath('node_modules', esModule)); if (fs.existsSync(esPath)) { source.value = esModule; } } // @ant-design/icons/xxx => @ant-design/icons/es/icons/xxx const antdIconMatcher = /@ant-design\/icons\/([^/]*)$/; if (source && antdIconMatcher.test(source.value)) { const esModule = source.value.replace(antdIconMatcher, (_, iconName) => `@ant-design/icons/es/icons/${iconName}`); const esPath = dirname(getProjectPath('node_modules', esModule)); if (fs.existsSync(esPath)) { source.value = esModule; } } } function replaceLib() { return { visitor: { ImportDeclaration: replacePath, ExportNamedDeclaration: replacePath } }; } export default replaceLib;