Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
at main 1.4 kB view raw
1const visited = 'visitedByDebugTargetTransformer'; 2 3const warningDevCheckTemplate = ` 4 process.env.NODE_ENV !== 'production' ? NODE : undefined 5`.trim(); 6 7const plugin = ({ template, types: t }) => { 8 const wrapWithDevCheck = template.expression(warningDevCheckTemplate, { 9 placeholderPattern: /^NODE$/, 10 }); 11 12 let name = 'unknownExchange'; 13 14 return { 15 visitor: { 16 ExportNamedDeclaration(path) { 17 if ( 18 path.node.declaration && 19 path.node.declaration.declarations && 20 path.node.declaration.declarations[0] && 21 path.node.declaration.declarations[0].id 22 ) { 23 const exportName = path.node.declaration.declarations[0].id.name; 24 if (/Exchange$/i.test(exportName)) name = exportName; 25 } 26 }, 27 CallExpression(path, meta) { 28 if (path.node[visited] || !path.node.callee) return; 29 30 if (path.node.callee.name === 'dispatchDebug') { 31 path.node[visited] = true; 32 if ( 33 t.isObjectExpression(path.node.arguments[0]) && 34 !meta.filename.endsWith('compose.ts') 35 ) { 36 path.node.arguments[0].properties.push( 37 t.objectProperty(t.stringLiteral('source'), t.stringLiteral(name)) 38 ); 39 } 40 41 path.replaceWith(wrapWithDevCheck({ NODE: path.node })); 42 } 43 }, 44 }, 45 }; 46}; 47 48export default plugin;