All files / src/utils getConfigExports.ts

86.34% Statements 139/161
79.07% Branches 34/43
90.91% Functions 10/11
86.34% Lines 139/161

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 1629x 9x 9x 9x 9x 9x 9x 9x 36x 36x 9x 9x 9x 9x 9x 9x 9x 27x 27x 27x 27x 27x 27x 27x 27x 27x 1350x 1350x 1350x 40612x 1341x 1341x 27x 9x 9x 9x 27x 27x 27x 27x 27x 27x     27x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 6343x 6343x 6343x 6343x 6343x 6343x 9x 9x 9x 9x 9x 9x 9x 9x 9x                 9x 343x 343x 343x 343x 343x 343x 343x 343x 343x 343x 343x 343x 343x 6763x 82x 82x 82x 80x 74x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 72x 72x 18x 76x 6763x 6681x 6299x 6299x 6299x 6299x 12x 12x 12x 12x 12x 12x                         12x 12x 6299x 6299x 6299x 6299x 6299x 6299x 6291x 6291x 6291x 6291x 6291x 6291x 6291x 6291x 6291x 6291x 6291x 6267x 6267x 6267x  
import { parse, types } from '@babel/core';
import { readFileSync } from 'fs';
import { dirname, join, sep } from 'path';
import { PluginConfig } from './prepareConfig';
import assert = require('assert');
import resolveModule = require('../../lib/internal/resolveModule');
 
export interface PluginConfigExports {
  source: string;
  internal: string;
  external: string;
}
 
function toRelativeSource(filePath: string): string {
  const pathChunks = filePath.split(sep);
 
  const relativeChunks = pathChunks.slice(
    pathChunks.lastIndexOf('node_modules') + 1,
  );
 
  return relativeChunks.join('/');
}
 
function resolveIndexPath(
  name: string,
  indexFile: null | undefined | string,
): string {
  if (!indexFile) {
    const indexFilePath = resolveModule(name);
 
    assert.ok(indexFilePath, `failed to find 'indexFile' of '${name}'.`);
 
    return indexFilePath;
  }
 
  const pkgJSON = resolveModule(`${name}/package.json`);
 
  assert.ok(pkgJSON, `failed to resolve 'package.json' for '${name}'`);
 
  const pkgDir = dirname(pkgJSON);
 
  const indexFilePath = resolveModule(join(pkgDir, indexFile));

  assert.ok(
    indexFilePath,
    `failed to resolve 'indexFile' specified for '${name}'.`,
  );
 
  return indexFilePath;
}
 
function fulfillExports(
  exports: Map<string, PluginConfigExports>,
  filePath: string,
): void {
  const content = readFileSync(filePath, 'utf-8');
  const ast = parse(content, { filename: filePath, sourceType: 'module' });
  const program = !ast ? null : types.isFile(ast) ? ast.program : ast;
 
  assert.ok(program, `failed to parse '${filePath}'.`);
 
  const fileDir = dirname(filePath);
  const imports = new Map<string, { imported: string; source: string }>();
 
  for (const node of program.body) {
    if (types.isImportDeclaration(node)) {
      const sourcePath = resolveModule(node.source.value, fileDir);
 
      if (!sourcePath) {
        continue;
      }
 
      for (const specifier of node.specifiers) {
        imports.set(specifier.local.name, {
          source: toRelativeSource(sourcePath),
          imported: types.isImportNamespaceSpecifier(specifier)
            ? '*'
            : types.isImportDefaultSpecifier(specifier)
            ? 'default'
            : types.isIdentifier(specifier.imported)
            ? specifier.imported.name
            : specifier.imported.value,
        });
      }
    } else if (types.isExportNamedDeclaration(node)) {
      const nodeDeclaration = node.declaration;
 
      if (types.isVariableDeclaration(nodeDeclaration)) {
        for (const declaration of nodeDeclaration.declarations) {
          if (
            types.isIdentifier(declaration.id) &&
            types.isIdentifier(declaration.init)
          ) {
            const external = declaration.id.name;
            const internal = declaration.init.name;
            const imported = imports.get(internal);
 
            if (imported) {
              exports.set(external, {
                external,
                source: imported.source,
                internal: imported.imported,
              });
            }
          }
        }
      }
 
      const sourcePath = !node.source
        ? null
        : resolveModule(node.source.value, fileDir);
 
      for (const specifier of node.specifiers) {
        if (types.isExportSpecifier(specifier)) {
          const internal = specifier.local.name;
          const external = types.isIdentifier(specifier.exported)
            ? specifier.exported.name
            : specifier.exported.value;
 
          if (sourcePath) {
            exports.set(external, {
              internal,
              external,
              source: toRelativeSource(sourcePath),
            });
          } else {
            const imported = imports.get(internal);

            if (imported) {
              exports.set(external, {
                external,
                source: imported.source,
                internal: imported.imported,
              });
            }
          }
        }
      }
    } else if (types.isExportAllDeclaration(node)) {
      const sourcePath = resolveModule(node.source.value, fileDir);
 
      assert.ok(
        sourcePath,
        `failed to resolve '${node.source.value}' from '${fileDir}'`,
      );
 
      fulfillExports(exports, sourcePath);
    }
  }
}
 
export function getConfigExports(
  config: PluginConfig,
): Map<string, PluginConfigExports> {
  const indexPath = resolveIndexPath(config.name, config.indexFile);
  const exports = new Map<string, PluginConfigExports>();
 
  fulfillExports(exports, indexPath);
 
  return exports;
}