Something went wrong. Try again.
Mirror: Modular GraphQL.js import paths without the hassle.
Something went wrong. Try again.
babel-plugin-modular-graphql index.js
2.2 kB · 60 lines
JavaScript
at main
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061module.exports = function babelPluginModularGraphql({ types: t }, options = {}) { let extension = (options.extension || '').trim(); if (extension && extension[0] !== '.') { extension = '.' + extension; }
const importMap = require('./import-map.json'); const indexRe = /[\\/]index$/; const PKG_NAME = 'graphql';
return { visitor: { ImportDeclaration: { exit(path) { const { node } = path; if (node.source.value !== PKG_NAME || !node.specifiers.length) return;
const imports = node.specifiers.reduce((acc, specifier) => { if (t.isImportSpecifier(specifier)) { const imported = specifier.imported.name;
let declaration = importMap[imported]; if (!declaration) { console.warn( `The export "${imported}" could not be found. It may not be known, or may not be available consistently between graphql@14|15|16.\n` + 'Try using an alternative method or check whether this method is present in the provided range of graphql major releases.' ); }
let from = declaration ? declaration.from : PKG_NAME; if (!acc[from]) { if (from !== PKG_NAME && extension) { from += extension; } else if (from !== PKG_NAME && from.endsWith('')) { from = from.replace(indexRe, ''); }
acc[from] = t.importDeclaration([], t.stringLiteral(from)); }
const localName = specifier.local.name; const newImportedName = declaration ? declaration.local : imported;
acc[from].specifiers.push( t.importSpecifier(t.identifier(localName), t.identifier(newImportedName)) ); }
return acc; }, {});
const importFiles = Object.keys(imports); if (importFiles.length && (importFiles.length !== 1 || importFiles[0] !== PKG_NAME)) { path.replaceWithMultiple(importFiles.map((key) => imports[key])); } }, }, }, };};