All files / lib index.js

86.33% Statements 120/139
75.86% Branches 22/29
100% Functions 5/5
86.33% Lines 120/139

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 1401x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 18x 18x 5x 5x 5x 5x 18x 18x 18x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 18x 18x 18x 18x 18x 18x 18x 5x 17x 13x 13x 18x 18x 18x 18x 18x 18x 1x 1x 1x 1x 1x 18x 18x 18x 18x 18x       1x 1x 1x 5x 5x 5x 18x 18x 18x 18x 18x 18x 18x 18x 5x 18x 14x                           14x 14x   14x     14x 14x 14x 13x 13x 13x 13x 13x 13x 13x 13x 12x 12x 12x 12x 2x 2x 2x 13x 13x 13x 13x 13x 13x 13x 13x 13x 14x 5x 18x 18x 5x 5x 5x  
'use strict';
 
const util = require('util');
const babel = require('@babel/core');
const getModuleExports = require('./internal/getModuleExports');
const prepareModuleExports = require('./internal/prepareModuleExports');
 
const { types: t } = babel;
 
/** @type {Map<string, string | undefined>} */
const modulesCache = new Map();
 
/**
 * @param {unknown[]} modules
 * @returns {Map<string, string | undefined>}
 */
function getCachedModules(modules) {
  if (modulesCache.size !== modules.length) {
    for (const { name, indexFile } of prepareModuleExports(modules)) {
      modulesCache.set(name, indexFile);
    }
  }
 
  return modulesCache;
}
 
/**
 * @type {Map<
 *   string,
 *   Map<string, import('./internal/getModuleExports').ModuleExport>
 * >}
 */
const moduleExportsCache = new Map();
 
/**
 * @param {string} name
 * @param {unknown[]} modules
 * @returns {Map<string, import('./internal/getModuleExports').ModuleExport>}
 */
function getCachedModuleExports(name, modules) {
  let moduleExports = moduleExportsCache.get(name);
 
  if (!moduleExports) {
    const cachedModules = getCachedModules(modules);
 
    if (cachedModules.has(name)) {
      moduleExports = getModuleExports(name, cachedModules.get(name));
    } else {
      moduleExports = new Map();
    }
 
    moduleExportsCache.set(name, moduleExports);
  }
 
  return moduleExports;
}
 
/**
 * @param {import('@babel/core').PluginOptions} opts
 * @returns {unknown[]}
 */
function parseOptions(opts) {
  if (opts && 'modules' in opts) {
    const { modules } = /** @type {{ modules?: unknown }} */ (opts);
    if (modules) return Array.isArray(modules) ? modules : [modules];
  }

  return [];
}
 
/** @returns {import('@babel/core').PluginObj} */
module.exports = function plugin() {
  return {
    visitor: {
      ImportDeclaration(declaration, { opts }) {
        const modules = parseOptions(opts);
        if (!modules.length) return;
 
        const { source, specifiers, importKind } = declaration.node;
        if (!specifiers.length || importKind === 'type') return;
 
        const moduleExports = getCachedModuleExports(source.value, modules);
        if (!moduleExports.size) return;
 
        for (const specifier of specifiers) {
          if (t.isImportNamespaceSpecifier(specifier)) {
            console.warn(
              util.format(
                [
                  'babel-plugin-direct-import: Can not optimize `import * as %s from "%s"`.',
                  'See plugin limitations https://git.io/vFDOO for more details.',
                ].join('\n'),
                specifier.local.name,
                source.value,
              ),
            );

            continue;
          }
 
          const moduleName = t.isImportDefaultSpecifier(specifier)
            ? 'default'
            : t.isIdentifier(specifier.imported)
            ? specifier.imported.name
            : specifier.imported.value;
          const exports = moduleExports.get(moduleName);
 
          if (exports) {
            declaration.node.specifiers = declaration.node.specifiers.filter(
              (x) => x !== specifier,
            );
 
            declaration.insertBefore(
              t.importDeclaration(
                [
                  exports.internal === '*'
                    ? t.importNamespaceSpecifier(
                        t.identifier(specifier.local.name),
                      )
                    : exports.internal === 'default'
                    ? t.importDefaultSpecifier(
                        t.identifier(specifier.local.name),
                      )
                    : t.importSpecifier(
                        t.identifier(specifier.local.name),
                        t.identifier(exports.external),
                      ),
                ],
                t.stringLiteral(exports.source),
              ),
            );
          }
        }
 
        if (!declaration.node.specifiers.length) declaration.remove();
      },
    },
  };
};