UNPKG

1.81 kBJavaScriptView Raw
1System.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(`
2 globalThis.jotaiAtomCache = globalThis.jotaiAtomCache || {
3 cache: new Map(),
4 get(name, inst) {
5 if (this.cache.has(name)) {
6 return this.cache.get(name)
7 }
8 this.cache.set(name, inst)
9 return inst
10 },
11 }`)();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)}}}}}}}});