UNPKG

jotai

Version:

👻 Next gen state management that will spook you

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