UNPKG

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