UNPKG

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