UNPKG

4.04 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('@babel/template')) :
3 typeof define === 'function' && define.amd ? define(['@babel/template'], factory) :
4 (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global["jotaiPlugin-react-refresh"] = factory(global._templateBuilder));
5})(this, (function (_templateBuilder) { 'use strict';
6
7 function isAtom(t, callee, customAtomNames) {
8 if (customAtomNames === void 0) {
9 customAtomNames = [];
10 }
11 var atomNames = [].concat(atomFunctionNames, customAtomNames);
12 if (t.isIdentifier(callee) && atomNames.includes(callee.name)) {
13 return true;
14 }
15 if (t.isMemberExpression(callee)) {
16 var property = callee.property;
17 if (t.isIdentifier(property) && atomNames.includes(property.name)) {
18 return true;
19 }
20 }
21 return false;
22 }
23 var atomFunctionNames = ['abortableAtom', 'atom', 'atomFamily', 'atomWithDefault', 'atomWithHash', 'atomWithImmer', 'atomWithInfiniteQuery', 'atomWithMachine', 'atomWithMutation', 'atomWithObservable', 'atomWithProxy', 'atomWithQuery', 'atomWithReducer', 'atomWithReset', 'atomWithSubscription', 'atomWithStorage', 'atomWithStore', 'freezeAtom', 'loadable', 'selectAtom', 'splitAtom'];
24
25 var templateBuilder = _templateBuilder.default || _templateBuilder;
26 function reactRefreshPlugin(_ref, options) {
27 var t = _ref.types;
28 return {
29 pre: function pre(_ref2) {
30 var opts = _ref2.opts;
31 if (!opts.filename) {
32 throw new Error('Filename must be available');
33 }
34 },
35 visitor: {
36 Program: {
37 exit: function exit(path) {
38 var jotaiAtomCache = templateBuilder("\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 }")();
39 path.unshiftContainer('body', jotaiAtomCache);
40 }
41 },
42 ExportDefaultDeclaration: function ExportDefaultDeclaration(nodePath, state) {
43 var node = nodePath.node;
44 if (t.isCallExpression(node.declaration) && isAtom(t, node.declaration.callee, options == null ? void 0 : options.customAtomNames)) {
45 var filename = state.filename || 'unknown';
46 var atomKey = filename + "/defaultExport";
47 var buildExport = templateBuilder("export default globalThis.jotaiAtomCache.get(%%atomKey%%, %%atom%%)");
48 var ast = buildExport({
49 atomKey: t.stringLiteral(atomKey),
50 atom: node.declaration
51 });
52 nodePath.replaceWith(ast);
53 }
54 },
55 VariableDeclarator: function VariableDeclarator(nodePath, state) {
56 var _nodePath$parentPath$, _nodePath$parentPath$2;
57 if (t.isIdentifier(nodePath.node.id) && t.isCallExpression(nodePath.node.init) && isAtom(t, nodePath.node.init.callee, options == null ? void 0 : options.customAtomNames) && ((_nodePath$parentPath$ = nodePath.parentPath.parentPath) != null && _nodePath$parentPath$.isProgram() || (_nodePath$parentPath$2 = nodePath.parentPath.parentPath) != null && _nodePath$parentPath$2.isExportNamedDeclaration())) {
58 var filename = state.filename || 'unknown';
59 var atomKey = filename + "/" + nodePath.node.id.name;
60 var buildAtomDeclaration = templateBuilder("const %%atomIdentifier%% = globalThis.jotaiAtomCache.get(%%atomKey%%, %%atom%%)");
61 var ast = buildAtomDeclaration({
62 atomIdentifier: t.identifier(nodePath.node.id.name),
63 atomKey: t.stringLiteral(atomKey),
64 atom: nodePath.node.init
65 });
66 nodePath.parentPath.replaceWith(ast);
67 }
68 }
69 }
70 };
71 }
72
73 return reactRefreshPlugin;
74
75}));