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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 25x 25x 25x 25x 9x 9x 9x 9x 9x 9x 25x 25x 16x 24x 9x 9x 25x 25x 25x 25x 25x 25x 9x 9x 9x 9x 9x 26x 26x 25x 25x 9x 26x 18x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 17x 18x 16x 16x 18x 18x 18x 16x 16x 16x 16x 16x 16x 16x 16x 15x 15x 15x 15x 2x 2x 2x 16x 16x 16x 16x 16x 16x 16x 16x 16x 18x 9x 26x 26x 9x 9x 9x | "use strict"; const util = require("util"); const babel = require("@babel/core"); const getModuleExports = require("./internal/getModuleExports"); const parsePluginOptions = require("./internal/parsePluginOptions"); const { types: t } = babel; /** @returns {import("@babel/core").PluginObj} */ module.exports = function plugin() { /** @type {Map<string, string | undefined>} */ const modulesCache = new Map(); /** * @type {Map< * string, * Map<string, import("./internal/getModuleExports").ModuleExport> * >} */ const moduleExportsCache = new Map(); /** * @param {string} name * @param {import("@babel/core").PluginOptions} opts * @returns {Map< * string, * import("./internal/getModuleExports").ModuleExport * >} */ function getCachedModuleExports(name, opts) { let moduleExports = moduleExportsCache.get(name); if (!moduleExports) { if (!modulesCache.size) { const { modules } = parsePluginOptions(opts); for (const module of modules) { modulesCache.set(module.name, module.indexFile); } } if (!modulesCache.has(name)) { moduleExports = new Map(); } else { moduleExports = getModuleExports(name, modulesCache.get(name)); } moduleExportsCache.set(name, moduleExports); } return moduleExports; } return { name: "babel-plugin-direct-import", visitor: { ImportDeclaration(declaration, { opts }) { const { source, specifiers, importKind } = declaration.node; if (!specifiers.length || importKind === "type") return; const moduleExports = getCachedModuleExports(source.value, opts); 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(); }, }, }; }; |