UNPKG

jotai

Version:

👻 Primitive and flexible state management for React

2 lines (1 loc) • 2.58 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("@babel/template")):"function"==typeof define&&define.amd?define(["@babel/template"],t):(e="undefined"!=typeof globalThis?globalThis:e||self).jotaiBabelPluginReactRefresh=t(e._templateBuilder)}(this,function(e){"use strict";function t(e,t,i){void 0===i&&(i=[]);var o=[].concat(a,i);if(e.isIdentifier(t)&&o.includes(t.name))return!0;if(e.isMemberExpression(t)){var n=t.property;if(e.isIdentifier(n)&&o.includes(n.name))return!0}return!1}var a=["atom","atomFamily","atomWithDefault","atomWithObservable","atomWithReducer","atomWithReset","atomWithStorage","freezeAtom","loadable","selectAtom","splitAtom","unwrap","atomWithMachine","atomWithImmer","atomWithProxy","atomWithQuery","atomWithMutation","atomWithSubscription","atomWithStore","atomWithHash","atomWithLocation","focusAtom","atomWithValidate","validateAtoms","atomWithCache","atomWithRecoilValue"],i=e.default||e;return function(e,a){var o=e.types;return console.warn("[DEPRECATED] jotai/babel/plugin-react-refresh is deprecated and will be removed in v3.\nPlease use the `jotai-babel` package instead: https://github.com/jotaijs/jotai-babel"),{pre:function(e){if(!e.opts.filename)throw new Error("Filename must be available")},visitor:{Program:{exit:function(e){var t=i("\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,n){var r=e.node;if(o.isCallExpression(r.declaration)&&t(o,r.declaration.callee,null==a?void 0:a.customAtomNames)){var l=(n.filename||"unknown")+"/defaultExport",m=i("export default globalThis.jotaiAtomCache.get(%%atomKey%%, %%atom%%)")({atomKey:o.stringLiteral(l),atom:r.declaration});e.replaceWith(m)}},VariableDeclarator:function(e,n){var r,l;if(o.isIdentifier(e.node.id)&&o.isCallExpression(e.node.init)&&t(o,e.node.init.callee,null==a?void 0:a.customAtomNames)&&(null!=(r=e.parentPath.parentPath)&&r.isProgram()||null!=(l=e.parentPath.parentPath)&&l.isExportNamedDeclaration())){var m=(n.filename||"unknown")+"/"+e.node.id.name,s=i("const %%atomIdentifier%% = globalThis.jotaiAtomCache.get(%%atomKey%%, %%atom%%)")({atomIdentifier:o.identifier(e.node.id.name),atomKey:o.stringLiteral(m),atom:e.node.init});e.parentPath.replaceWith(s)}}}}}});