UNPKG

2.63 kBJavaScriptView Raw
1System.register(["path","@babel/template"],function(d){"use strict";var s,l;return{setters:[function(a){s=a.default},function(a){l=a.default}],execute:function(){d("default",p);function a(t,e){if(t.isIdentifier(e)&&c.includes(e.name))return!0;if(t.isMemberExpression(e)){const{property:i}=e;if(t.isIdentifier(i)&&c.includes(i.name))return!0}return!1}const c=["atom","atomFamily","atomWithDefault","atomWithObservable","atomWithReducer","atomWithReset","atomWithStorage","freezeAtom","loadable","selectAtom","splitAtom"];function f({types:t}){return{visitor:{ExportDefaultDeclaration(e,i){const{node:n}=e;if(t.isCallExpression(n.declaration)&&a(t,n.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(`
2 const %%atomIdentifier%% = %%atom%%;
3 export default %%atomIdentifier%%
4 `)({atomIdentifier:t.identifier(r),atom:n.declaration});e.replaceWithMultiple(m)}},VariableDeclarator(e){t.isIdentifier(e.node.id)&&t.isCallExpression(e.node.init)&&a(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 u({types:t}){return{pre({opts:e}){if(!e.filename)throw new Error("Filename must be available")},visitor:{Program:{exit(e){const i=l(`
5 globalThis.jotaiAtomCache = globalThis.jotaiAtomCache || {
6 cache: new Map(),
7 get(name, inst) {
8 if (this.cache.has(name)) {
9 return this.cache.get(name)
10 }
11 this.cache.set(name, inst)
12 return inst
13 },
14 }`)();e.unshiftContainer("body",i)}},ExportDefaultDeclaration(e,i){const{node:n}=e;if(t.isCallExpression(n.declaration)&&a(t,n.declaration.callee)){const o=`${i.filename||"unknown"}/defaultExport`,r=l("export default globalThis.jotaiAtomCache.get(%%atomKey%%, %%atom%%)")({atomKey:t.stringLiteral(o),atom:n.declaration});e.replaceWith(r)}},VariableDeclarator(e,i){var n,o;if(t.isIdentifier(e.node.id)&&t.isCallExpression(e.node.init)&&a(t,e.node.init.callee)&&(((n=e.parentPath.parentPath)==null?void 0:n.isProgram())||((o=e.parentPath.parentPath)==null?void 0:o.isExportNamedDeclaration()))){const r=`${i.filename||"unknown"}/${e.node.id.name}`,m=l("const %%atomIdentifier%% = globalThis.jotaiAtomCache.get(%%atomKey%%, %%atom%%)")({atomIdentifier:t.identifier(e.node.id.name),atomKey:t.stringLiteral(r),atom:e.node.init});e.parentPath.replaceWith(m)}}}}}function p(){return{plugins:[f,u]}}}}});