UNPKG

jotai

Version:

👻 Next gen state management that will spook you

2 lines (1 loc) • 2.37 kB
!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=["abortableAtom","atom","atomFamily","atomWithDefault","atomWithHash","atomWithImmer","atomWithInfiniteQuery","atomWithMachine","atomWithMutation","atomWithObservable","atomWithProxy","atomWithQuery","atomWithReducer","atomWithReset","atomWithSubscription","atomWithStorage","atomWithStore","freezeAtom","loadable","selectAtom","splitAtom"],r=i.default.default||i.default;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=r("\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 i=e.node;if(t.isCallExpression(i.declaration)&&n(t,i.declaration.callee)){var o=(a.filename||"unknown")+"/defaultExport",l=r("export default globalThis.jotaiAtomCache.get(%%atomKey%%, %%atom%%)")({atomKey:t.stringLiteral(o),atom:i.declaration});e.replaceWith(l)}},VariableDeclarator:function(e,a){var i,o;if(t.isIdentifier(e.node.id)&&t.isCallExpression(e.node.init)&&n(t,e.node.init.callee)&&(null!=(i=e.parentPath.parentPath)&&i.isProgram()||null!=(o=e.parentPath.parentPath)&&o.isExportNamedDeclaration())){var l=(a.filename||"unknown")+"/"+e.node.id.name,m=r("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(m)}}}}},Object.defineProperty(e,"__esModule",{value:!0})}));