UNPKG

jotai

Version:

👻 Next gen state management that will spook you

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