UNPKG

5.07 kBJavaScriptView Raw
1System.register(['path', '@babel/template'], (function (exports) {
2 'use strict';
3 var path, templateBuilder;
4 return {
5 setters: [function (module) {
6 path = module["default"];
7 }, function (module) {
8 templateBuilder = module["default"];
9 }],
10 execute: (function () {
11
12 exports('default', jotaiPreset);
13
14 function isAtom(t, callee) {
15 if (t.isIdentifier(callee) && atomFunctionNames.includes(callee.name)) {
16 return true;
17 }
18 if (t.isMemberExpression(callee)) {
19 const { property } = callee;
20 if (t.isIdentifier(property) && atomFunctionNames.includes(property.name)) {
21 return true;
22 }
23 }
24 return false;
25 }
26 const atomFunctionNames = [
27 "atom",
28 "atomFamily",
29 "atomWithDefault",
30 "atomWithObservable",
31 "atomWithReducer",
32 "atomWithReset",
33 "atomWithStorage",
34 "freezeAtom",
35 "loadable",
36 "selectAtom",
37 "splitAtom"
38 ];
39
40 function debugLabelPlugin({
41 types: t
42 }) {
43 return {
44 visitor: {
45 ExportDefaultDeclaration(nodePath, state) {
46 const { node } = nodePath;
47 if (t.isCallExpression(node.declaration) && isAtom(t, node.declaration.callee)) {
48 const filename = state.filename || "unknown";
49 let displayName = path.basename(filename, path.extname(filename));
50 if (displayName === "index") {
51 displayName = path.basename(path.dirname(filename));
52 }
53 const buildExport = templateBuilder(`
54 const %%atomIdentifier%% = %%atom%%;
55 export default %%atomIdentifier%%
56 `);
57 const ast = buildExport({
58 atomIdentifier: t.identifier(displayName),
59 atom: node.declaration
60 });
61 nodePath.replaceWithMultiple(ast);
62 }
63 },
64 VariableDeclarator(path2) {
65 if (t.isIdentifier(path2.node.id) && t.isCallExpression(path2.node.init) && isAtom(t, path2.node.init.callee)) {
66 path2.parentPath.insertAfter(t.expressionStatement(t.assignmentExpression("=", t.memberExpression(t.identifier(path2.node.id.name), t.identifier("debugLabel")), t.stringLiteral(path2.node.id.name))));
67 }
68 }
69 }
70 };
71 }
72
73 function reactRefreshPlugin({
74 types: t
75 }) {
76 return {
77 pre({ opts }) {
78 if (!opts.filename) {
79 throw new Error("Filename must be available");
80 }
81 },
82 visitor: {
83 Program: {
84 exit(path) {
85 const jotaiAtomCache = templateBuilder(`
86 globalThis.jotaiAtomCache = globalThis.jotaiAtomCache || {
87 cache: new Map(),
88 get(name, inst) {
89 if (this.cache.has(name)) {
90 return this.cache.get(name)
91 }
92 this.cache.set(name, inst)
93 return inst
94 },
95 }`)();
96 path.unshiftContainer("body", jotaiAtomCache);
97 }
98 },
99 ExportDefaultDeclaration(nodePath, state) {
100 const { node } = nodePath;
101 if (t.isCallExpression(node.declaration) && isAtom(t, node.declaration.callee)) {
102 const filename = state.filename || "unknown";
103 const atomKey = `${filename}/defaultExport`;
104 const buildExport = templateBuilder(`export default globalThis.jotaiAtomCache.get(%%atomKey%%, %%atom%%)`);
105 const ast = buildExport({
106 atomKey: t.stringLiteral(atomKey),
107 atom: node.declaration
108 });
109 nodePath.replaceWith(ast);
110 }
111 },
112 VariableDeclarator(nodePath, state) {
113 var _a, _b;
114 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()))) {
115 const filename = state.filename || "unknown";
116 const atomKey = `${filename}/${nodePath.node.id.name}`;
117 const buildAtomDeclaration = templateBuilder(`const %%atomIdentifier%% = globalThis.jotaiAtomCache.get(%%atomKey%%, %%atom%%)`);
118 const ast = buildAtomDeclaration({
119 atomIdentifier: t.identifier(nodePath.node.id.name),
120 atomKey: t.stringLiteral(atomKey),
121 atom: nodePath.node.init
122 });
123 nodePath.parentPath.replaceWith(ast);
124 }
125 }
126 }
127 };
128 }
129
130 function jotaiPreset() {
131 return {
132 plugins: [debugLabelPlugin, reactRefreshPlugin]
133 };
134 }
135
136 })
137 };
138}));