UNPKG

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