UNPKG

5.3 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5var path = require('path');
6var _templateBuilder = require('@babel/template');
7
8function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
10var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
11var _templateBuilder__default = /*#__PURE__*/_interopDefaultLegacy(_templateBuilder);
12
13function isAtom(t, callee) {
14 if (t.isIdentifier(callee) && atomFunctionNames.includes(callee.name)) {
15 return true;
16 }
17
18 if (t.isMemberExpression(callee)) {
19 var property = callee.property;
20
21 if (t.isIdentifier(property) && atomFunctionNames.includes(property.name)) {
22 return true;
23 }
24 }
25
26 return false;
27}
28var atomFunctionNames = ['abortableAtom', 'atom', 'atomFamily', 'atomWithDefault', 'atomWithHash', 'atomWithImmer', 'atomWithInfiniteQuery', 'atomWithMachine', 'atomWithMutation', 'atomWithObservable', 'atomWithProxy', 'atomWithQuery', 'atomWithReducer', 'atomWithReset', 'atomWithSubscription', 'atomWithStorage', 'atomWithStore', 'freezeAtom', 'loadable', 'selectAtom', 'splitAtom'];
29
30var templateBuilder$1 = _templateBuilder__default["default"].default || _templateBuilder__default["default"];
31function debugLabelPlugin(_ref) {
32 var t = _ref.types;
33 return {
34 visitor: {
35 ExportDefaultDeclaration: function ExportDefaultDeclaration(nodePath, state) {
36 var node = nodePath.node;
37
38 if (t.isCallExpression(node.declaration) && isAtom(t, node.declaration.callee)) {
39 var filename = state.filename || 'unknown';
40 var displayName = path__default["default"].basename(filename, path__default["default"].extname(filename));
41
42 if (displayName === 'index') {
43 displayName = path__default["default"].basename(path__default["default"].dirname(filename));
44 }
45
46 var buildExport = templateBuilder$1("\n const %%atomIdentifier%% = %%atom%%;\n export default %%atomIdentifier%%\n ");
47 var ast = buildExport({
48 atomIdentifier: t.identifier(displayName),
49 atom: node.declaration
50 });
51 nodePath.replaceWithMultiple(ast);
52 }
53 },
54 VariableDeclarator: function VariableDeclarator(path) {
55 if (t.isIdentifier(path.node.id) && t.isCallExpression(path.node.init) && isAtom(t, path.node.init.callee)) {
56 path.parentPath.insertAfter(t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.identifier(path.node.id.name), t.identifier('debugLabel')), t.stringLiteral(path.node.id.name))));
57 }
58 }
59 }
60 };
61}
62
63var templateBuilder = _templateBuilder__default["default"].default || _templateBuilder__default["default"];
64function reactRefreshPlugin(_ref) {
65 var t = _ref.types;
66 return {
67 pre: function pre(_ref2) {
68 var opts = _ref2.opts;
69
70 if (!opts.filename) {
71 throw new Error('Filename must be available');
72 }
73 },
74 visitor: {
75 Program: {
76 exit: function exit(path) {
77 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 }")();
78 path.unshiftContainer('body', jotaiAtomCache);
79 }
80 },
81 ExportDefaultDeclaration: function ExportDefaultDeclaration(nodePath, state) {
82 var node = nodePath.node;
83
84 if (t.isCallExpression(node.declaration) && isAtom(t, node.declaration.callee)) {
85 var filename = state.filename || 'unknown';
86 var atomKey = filename + "/defaultExport";
87 var buildExport = templateBuilder("export default globalThis.jotaiAtomCache.get(%%atomKey%%, %%atom%%)");
88 var ast = buildExport({
89 atomKey: t.stringLiteral(atomKey),
90 atom: node.declaration
91 });
92 nodePath.replaceWith(ast);
93 }
94 },
95 VariableDeclarator: function VariableDeclarator(nodePath, state) {
96 var _nodePath$parentPath$, _nodePath$parentPath$2;
97
98 if (t.isIdentifier(nodePath.node.id) && t.isCallExpression(nodePath.node.init) && isAtom(t, nodePath.node.init.callee) && ((_nodePath$parentPath$ = nodePath.parentPath.parentPath) != null && _nodePath$parentPath$.isProgram() || (_nodePath$parentPath$2 = nodePath.parentPath.parentPath) != null && _nodePath$parentPath$2.isExportNamedDeclaration())) {
99 var filename = state.filename || 'unknown';
100 var atomKey = filename + "/" + nodePath.node.id.name;
101 var buildAtomDeclaration = templateBuilder("const %%atomIdentifier%% = globalThis.jotaiAtomCache.get(%%atomKey%%, %%atom%%)");
102 var ast = buildAtomDeclaration({
103 atomIdentifier: t.identifier(nodePath.node.id.name),
104 atomKey: t.stringLiteral(atomKey),
105 atom: nodePath.node.init
106 });
107 nodePath.parentPath.replaceWith(ast);
108 }
109 }
110 }
111 };
112}
113
114function jotaiPreset() {
115 return {
116 plugins: [debugLabelPlugin, reactRefreshPlugin]
117 };
118}
119
120exports["default"] = jotaiPreset;