UNPKG

2.18 kBJavaScriptView Raw
1!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@babel/template")):"function"==typeof define&&define.amd?define(["exports","@babel/template"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["jotaiPlugin-react-refresh"]={},e.templateBuilder)}(this,(function(e,t){"use strict";function a(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var i=a(t);function n(e,t){if(e.isIdentifier(t)&&o.includes(t.name))return!0;if(e.isMemberExpression(t)){var a=t.property;if(e.isIdentifier(a)&&o.includes(a.name))return!0}return!1}var o=["atom","atomFamily","atomWithDefault","atomWithObservable","atomWithReducer","atomWithReset","atomWithStorage","freezeAtom","loadable","selectAtom","splitAtom"];e.default=function(e){var t=e.types;return{pre:function(e){if(!e.opts.filename)throw new Error("Filename must be available")},visitor:{Program:{exit:function(e){var t=i.default("\n globalThis.jotaiAtomCache = globalThis.jotaiAtomCache || {\n cache: new Map(),\n get(name, inst) { \n if (this.cache.has(name)) {\n return this.cache.get(name)\n }\n this.cache.set(name, inst)\n return inst\n },\n }")();e.unshiftContainer("body",t)}},ExportDefaultDeclaration:function(e,a){var o=e.node;if(t.isCallExpression(o.declaration)&&n(t,o.declaration.callee)){var r=(a.filename||"unknown")+"/defaultExport",l=i.default("export default globalThis.jotaiAtomCache.get(%%atomKey%%, %%atom%%)")({atomKey:t.stringLiteral(r),atom:o.declaration});e.replaceWith(l)}},VariableDeclarator:function(e,a){var o,r;if(t.isIdentifier(e.node.id)&&t.isCallExpression(e.node.init)&&n(t,e.node.init.callee)&&(null!=(o=e.parentPath.parentPath)&&o.isProgram()||null!=(r=e.parentPath.parentPath)&&r.isExportNamedDeclaration())){var l=(a.filename||"unknown")+"/"+e.node.id.name,f=i.default("const %%atomIdentifier%% = globalThis.jotaiAtomCache.get(%%atomKey%%, %%atom%%)")({atomIdentifier:t.identifier(e.node.id.name),atomKey:t.stringLiteral(l),atom:e.node.init});e.parentPath.replaceWith(f)}}}}},Object.defineProperty(e,"__esModule",{value:!0})}));