UNPKG

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