UNPKG

2.32 kBJavaScriptView Raw
1!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("@babel/template")):"function"==typeof define&&define.amd?define(["@babel/template"],e):(t="undefined"!=typeof globalThis?globalThis:t||self)["jotaiPlugin-react-refresh"]=e(t._templateBuilder)}(this,(function(t){"use strict";function e(t,e,i){void 0===i&&(i=[]);var n=[].concat(a,i);if(t.isIdentifier(e)&&n.includes(e.name))return!0;if(t.isMemberExpression(e)){var o=e.property;if(t.isIdentifier(o)&&n.includes(o.name))return!0}return!1}var a=["abortableAtom","atom","atomFamily","atomWithDefault","atomWithHash","atomWithImmer","atomWithInfiniteQuery","atomWithMachine","atomWithMutation","atomWithObservable","atomWithProxy","atomWithQuery","atomWithReducer","atomWithReset","atomWithSubscription","atomWithStorage","atomWithStore","freezeAtom","loadable","selectAtom","splitAtom"],i=t.default||t;return function(t,a){var n=t.types;return{pre:function(t){if(!t.opts.filename)throw new Error("Filename must be available")},visitor:{Program:{exit:function(t){var e=i("\n globalThis.jotaiAtomCache = globalThis.jotaiAtomCache || {\n cache: new Map(),\n get(name, inst) { \n if (this.cache.has(name)) {\n return this.cache.get(name)\n }\n this.cache.set(name, inst)\n return inst\n },\n }")();t.unshiftContainer("body",e)}},ExportDefaultDeclaration:function(t,o){var r=t.node;if(n.isCallExpression(r.declaration)&&e(n,r.declaration.callee,null==a?void 0:a.customAtomNames)){var l=(o.filename||"unknown")+"/defaultExport",m=i("export default globalThis.jotaiAtomCache.get(%%atomKey%%, %%atom%%)")({atomKey:n.stringLiteral(l),atom:r.declaration});t.replaceWith(m)}},VariableDeclarator:function(t,o){var r,l;if(n.isIdentifier(t.node.id)&&n.isCallExpression(t.node.init)&&e(n,t.node.init.callee,null==a?void 0:a.customAtomNames)&&(null!=(r=t.parentPath.parentPath)&&r.isProgram()||null!=(l=t.parentPath.parentPath)&&l.isExportNamedDeclaration())){var m=(o.filename||"unknown")+"/"+t.node.id.name,s=i("const %%atomIdentifier%% = globalThis.jotaiAtomCache.get(%%atomKey%%, %%atom%%)")({atomIdentifier:n.identifier(t.node.id.name),atomKey:n.stringLiteral(m),atom:t.node.init});t.parentPath.replaceWith(s)}}}}}}));