UNPKG

jotai

Version:

👻 Primitive and flexible state management for React

2 lines (1 loc) • 3.85 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("@babel/template")):"function"==typeof define&&define.amd?define(["@babel/template"],t):(e="undefined"!=typeof globalThis?globalThis:e||self).jotaiBabelPreset=t(e._templateBuilder)}(this,function(e){"use strict";function t(e,t,i){void 0===i&&(i=[]);var n=[].concat(a,i);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 a=["atom","atomFamily","atomWithDefault","atomWithObservable","atomWithReducer","atomWithReset","atomWithStorage","freezeAtom","loadable","selectAtom","splitAtom","unwrap","atomWithMachine","atomWithImmer","atomWithProxy","atomWithQuery","atomWithMutation","atomWithSubscription","atomWithStore","atomWithHash","atomWithLocation","focusAtom","atomWithValidate","validateAtoms","atomWithCache","atomWithRecoilValue"],i=e.default||e;function n(e,a){var n=e.types;return console.warn("[DEPRECATED] jotai/babel/plugin-debug-label is deprecated and will be removed in v3.\nPlease use the `jotai-babel` package instead: https://github.com/jotaijs/jotai-babel"),{visitor:{ExportDefaultDeclaration:function(e,o){var r=e.node;if(n.isCallExpression(r.declaration)&&t(n,r.declaration.callee,null==a?void 0:a.customAtomNames)){var l=(o.filename||"unknown").replace(/\.\w+$/,""),s=l.split("/").pop();"index"===s&&(s=l.slice(0,-6).split("/").pop()||"unknown");var m=i("\n const %%atomIdentifier%% = %%atom%%;\n export default %%atomIdentifier%%\n ")({atomIdentifier:n.identifier(s),atom:r.declaration});e.replaceWithMultiple(m)}},VariableDeclarator:function(e){n.isIdentifier(e.node.id)&&n.isCallExpression(e.node.init)&&t(n,e.node.init.callee,null==a?void 0:a.customAtomNames)&&e.parentPath.insertAfter(n.expressionStatement(n.assignmentExpression("=",n.memberExpression(n.identifier(e.node.id.name),n.identifier("debugLabel")),n.stringLiteral(e.node.id.name))))}}}}var o=e.default||e;function r(e,a){var i=e.types;return console.warn("[DEPRECATED] jotai/babel/plugin-react-refresh is deprecated and will be removed in v3.\nPlease use the `jotai-babel` package instead: https://github.com/jotaijs/jotai-babel"),{pre:function(e){if(!e.opts.filename)throw new Error("Filename must be available")},visitor:{Program:{exit:function(e){var t=o("\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 r=e.node;if(i.isCallExpression(r.declaration)&&t(i,r.declaration.callee,null==a?void 0:a.customAtomNames)){var l=(n.filename||"unknown")+"/defaultExport",s=o("export default globalThis.jotaiAtomCache.get(%%atomKey%%, %%atom%%)")({atomKey:i.stringLiteral(l),atom:r.declaration});e.replaceWith(s)}},VariableDeclarator:function(e,n){var r,l;if(i.isIdentifier(e.node.id)&&i.isCallExpression(e.node.init)&&t(i,e.node.init.callee,null==a?void 0:a.customAtomNames)&&(null!=(r=e.parentPath.parentPath)&&r.isProgram()||null!=(l=e.parentPath.parentPath)&&l.isExportNamedDeclaration())){var s=(n.filename||"unknown")+"/"+e.node.id.name,m=o("const %%atomIdentifier%% = globalThis.jotaiAtomCache.get(%%atomKey%%, %%atom%%)")({atomIdentifier:i.identifier(e.node.id.name),atomKey:i.stringLiteral(s),atom:e.node.init});e.parentPath.replaceWith(m)}}}}}return function(e,t){return console.warn("[DEPRECATED] jotai/babel/preset is deprecated and will be removed in v3.\nPlease use the `jotai-babel` package instead: https://github.com/jotaijs/jotai-babel"),{plugins:[[n,t],[r,t]]}}});