UNPKG

jotai

Version:

👻 Next gen state management that will spook you

12 lines (11 loc) • 1.81 kB
System.register(["@babel/template"],function(u){"use strict";var n;return{setters:[function(o){n=o.default}],execute:function(){u("default",f);function o(t,e){if(t.isIdentifier(e)&&s.includes(e.name))return!0;if(t.isMemberExpression(e)){const{property:a}=e;if(t.isIdentifier(a)&&s.includes(a.name))return!0}return!1}const s=["atom","atomFamily","atomWithDefault","atomWithObservable","atomWithReducer","atomWithReset","atomWithStorage","freezeAtom","loadable","selectAtom","splitAtom"];function f({types:t}){return{pre({opts:e}){if(!e.filename)throw new Error("Filename must be available")},visitor:{Program:{exit(e){const a=n(` globalThis.jotaiAtomCache = globalThis.jotaiAtomCache || { cache: new Map(), get(name, inst) { if (this.cache.has(name)) { return this.cache.get(name) } this.cache.set(name, inst) return inst }, }`)();e.unshiftContainer("body",a)}},ExportDefaultDeclaration(e,a){const{node:i}=e;if(t.isCallExpression(i.declaration)&&o(t,i.declaration.callee)){const l=`${a.filename||"unknown"}/defaultExport`,c=n("export default globalThis.jotaiAtomCache.get(%%atomKey%%, %%atom%%)")({atomKey:t.stringLiteral(l),atom:i.declaration});e.replaceWith(c)}},VariableDeclarator(e,a){var i,r;if(t.isIdentifier(e.node.id)&&t.isCallExpression(e.node.init)&&o(t,e.node.init.callee)&&(((i=e.parentPath.parentPath)==null?void 0:i.isProgram())||((r=e.parentPath.parentPath)==null?void 0:r.isExportNamedDeclaration()))){const m=`${a.filename||"unknown"}/${e.node.id.name}`,d=n("const %%atomIdentifier%% = globalThis.jotaiAtomCache.get(%%atomKey%%, %%atom%%)")({atomIdentifier:t.identifier(e.node.id.name),atomKey:t.stringLiteral(m),atom:e.node.init});e.parentPath.replaceWith(d)}}}}}}}});