export default function resolveImport()

in modules/material-parser/src/parse/js/resolver/resolveImport.ts [101:179]


export default function resolveImport(path: any, callback: any) {
  let name;
  let mode: 'import' | 'require' = 'import';

  let importPath;
  if (path.name === 'local') {
    name = path.parentPath.parentPath.parentPath.node.source.value;
    importPath = path;
  } else {
    importPath = resolveToImport(path);
    if (!importPath) {
      return path;
    }
    if (isImportLike(importPath)) {
      name = importPath.node.source.value;
    } else if (isRequireLike(importPath)) {
      const moduleName = resolveToModule(importPath);
      if (typeof moduleName === 'string') {
        mode = 'require';
        name = moduleName;
      }
    } else {
      return path;
    }
  }

  if (name) {
    const __path = getPath(path, name);
    if (!__path) return path;
    let ast;
    if (!cache[__path]) {
      const fileContent = fs.readFileSync(__path, 'utf8');
      const parser = buildParser({ filename: __path });
      ast = parser.parse(fileContent);
      ast.__src = fileContent;
      ast.__path = __path;
      cache[__path] = ast;
    } else {
      ast = cache[__path];
    }

    const importMeta: any[] = [];
    if (mode === 'import') {
      if (t.ImportDeclaration.check(importPath.node)) {
        // @ts-ignore
        const specifiers = importPath.get('specifiers');
        specifiers.each((spec: any) => {
          const { node } = spec;
          importMeta.push({
            localName: node.local.name,
            importedName: node.imported ? node.imported.name : 'default',
          });
        });
      }
    } else {
      const idPath = importPath.parentPath.get('id');
      if (t.Identifier.check(idPath.node)) {
        importMeta.push({
          localName: 'default',
          importedName: idPath.node.name,
        });
      } else if (t.ObjectPattern.check(path.node)) {
        path.get('properties').each((propertyPath) => {
          const keyPath = propertyPath.get('key');
          const valuePath = propertyPath.get('value');
          if (t.Identifier.check(keyPath.node) && t.Identifier.check(valuePath.node)) {
            importMeta.push({
              localName: keyPath.node.name,
              importedName: valuePath.node.name,
            });
          }
        });
      }
    }

    return callback(ast, __path, importMeta, mode);
  }
  return path;
}