UNPKG

4.97 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 = ['atom', 'atomFamily', 'atomWithDefault', 'atomWithObservable', 'atomWithReducer', 'atomWithReset', 'atomWithStorage', 'freezeAtom', 'loadable', 'selectAtom', 'splitAtom'];
29
30function debugLabelPlugin(_ref) {
31 var t = _ref.types;
32 return {
33 visitor: {
34 ExportDefaultDeclaration: function ExportDefaultDeclaration(nodePath, state) {
35 var node = nodePath.node;
36
37 if (t.isCallExpression(node.declaration) && isAtom(t, node.declaration.callee)) {
38 var filename = state.filename || 'unknown';
39 var displayName = path__default["default"].basename(filename, path__default["default"].extname(filename));
40
41 if (displayName === 'index') {
42 displayName = path__default["default"].basename(path__default["default"].dirname(filename));
43 }
44
45 var buildExport = templateBuilder__default["default"]("\n const %%atomIdentifier%% = %%atom%%;\n export default %%atomIdentifier%%\n ");
46 var ast = buildExport({
47 atomIdentifier: t.identifier(displayName),
48 atom: node.declaration
49 });
50 nodePath.replaceWithMultiple(ast);
51 }
52 },
53 VariableDeclarator: function VariableDeclarator(path) {
54 if (t.isIdentifier(path.node.id) && t.isCallExpression(path.node.init) && isAtom(t, path.node.init.callee)) {
55 path.parentPath.insertAfter(t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.identifier(path.node.id.name), t.identifier('debugLabel')), t.stringLiteral(path.node.id.name))));
56 }
57 }
58 }
59 };
60}
61
62function reactRefreshPlugin(_ref) {
63 var t = _ref.types;
64 return {
65 pre: function pre(_ref2) {
66 var opts = _ref2.opts;
67
68 if (!opts.filename) {
69 throw new Error('Filename must be available');
70 }
71 },
72 visitor: {
73 Program: {
74 exit: function exit(path) {
75 var jotaiAtomCache = templateBuilder__default["default"]("\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 }")();
76 path.unshiftContainer('body', jotaiAtomCache);
77 }
78 },
79 ExportDefaultDeclaration: function ExportDefaultDeclaration(nodePath, state) {
80 var node = nodePath.node;
81
82 if (t.isCallExpression(node.declaration) && isAtom(t, node.declaration.callee)) {
83 var filename = state.filename || 'unknown';
84 var atomKey = filename + "/defaultExport";
85 var buildExport = templateBuilder__default["default"]("export default globalThis.jotaiAtomCache.get(%%atomKey%%, %%atom%%)");
86 var ast = buildExport({
87 atomKey: t.stringLiteral(atomKey),
88 atom: node.declaration
89 });
90 nodePath.replaceWith(ast);
91 }
92 },
93 VariableDeclarator: function VariableDeclarator(nodePath, state) {
94 var _nodePath$parentPath$, _nodePath$parentPath$2;
95
96 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())) {
97 var filename = state.filename || 'unknown';
98 var atomKey = filename + "/" + nodePath.node.id.name;
99 var buildAtomDeclaration = templateBuilder__default["default"]("const %%atomIdentifier%% = globalThis.jotaiAtomCache.get(%%atomKey%%, %%atom%%)");
100 var ast = buildAtomDeclaration({
101 atomIdentifier: t.identifier(nodePath.node.id.name),
102 atomKey: t.stringLiteral(atomKey),
103 atom: nodePath.node.init
104 });
105 nodePath.parentPath.replaceWith(ast);
106 }
107 }
108 }
109 };
110}
111
112function jotaiPreset() {
113 return {
114 plugins: [debugLabelPlugin, reactRefreshPlugin]
115 };
116}
117
118exports["default"] = jotaiPreset;