1 | System.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 | "abortableAtom",
|
28 | "atom",
|
29 | "atomFamily",
|
30 | "atomWithDefault",
|
31 | "atomWithHash",
|
32 | "atomWithImmer",
|
33 | "atomWithInfiniteQuery",
|
34 | "atomWithMachine",
|
35 | "atomWithMutation",
|
36 | "atomWithObservable",
|
37 | "atomWithProxy",
|
38 | "atomWithQuery",
|
39 | "atomWithReducer",
|
40 | "atomWithReset",
|
41 | "atomWithSubscription",
|
42 | "atomWithStorage",
|
43 | "atomWithStore",
|
44 | "freezeAtom",
|
45 | "loadable",
|
46 | "selectAtom",
|
47 | "splitAtom"
|
48 | ];
|
49 |
|
50 | const templateBuilder$1 = _templateBuilder.default || _templateBuilder;
|
51 | function debugLabelPlugin({
|
52 | types: t
|
53 | }) {
|
54 | return {
|
55 | visitor: {
|
56 | ExportDefaultDeclaration(nodePath, state) {
|
57 | const { node } = nodePath;
|
58 | if (t.isCallExpression(node.declaration) && isAtom(t, node.declaration.callee)) {
|
59 | const filename = state.filename || "unknown";
|
60 | let displayName = path.basename(filename, path.extname(filename));
|
61 | if (displayName === "index") {
|
62 | displayName = path.basename(path.dirname(filename));
|
63 | }
|
64 | const buildExport = templateBuilder$1(`
|
65 | const %%atomIdentifier%% = %%atom%%;
|
66 | export default %%atomIdentifier%%
|
67 | `);
|
68 | const ast = buildExport({
|
69 | atomIdentifier: t.identifier(displayName),
|
70 | atom: node.declaration
|
71 | });
|
72 | nodePath.replaceWithMultiple(ast);
|
73 | }
|
74 | },
|
75 | VariableDeclarator(path2) {
|
76 | if (t.isIdentifier(path2.node.id) && t.isCallExpression(path2.node.init) && isAtom(t, path2.node.init.callee)) {
|
77 | path2.parentPath.insertAfter(
|
78 | t.expressionStatement(
|
79 | t.assignmentExpression(
|
80 | "=",
|
81 | t.memberExpression(
|
82 | t.identifier(path2.node.id.name),
|
83 | t.identifier("debugLabel")
|
84 | ),
|
85 | t.stringLiteral(path2.node.id.name)
|
86 | )
|
87 | )
|
88 | );
|
89 | }
|
90 | }
|
91 | }
|
92 | };
|
93 | }
|
94 |
|
95 | const templateBuilder = _templateBuilder.default || _templateBuilder;
|
96 | function reactRefreshPlugin({
|
97 | types: t
|
98 | }) {
|
99 | return {
|
100 | pre({ opts }) {
|
101 | if (!opts.filename) {
|
102 | throw new Error("Filename must be available");
|
103 | }
|
104 | },
|
105 | visitor: {
|
106 | Program: {
|
107 | exit(path) {
|
108 | const jotaiAtomCache = templateBuilder(`
|
109 | globalThis.jotaiAtomCache = globalThis.jotaiAtomCache || {
|
110 | cache: new Map(),
|
111 | get(name, inst) {
|
112 | if (this.cache.has(name)) {
|
113 | return this.cache.get(name)
|
114 | }
|
115 | this.cache.set(name, inst)
|
116 | return inst
|
117 | },
|
118 | }`)();
|
119 | path.unshiftContainer("body", jotaiAtomCache);
|
120 | }
|
121 | },
|
122 | ExportDefaultDeclaration(nodePath, state) {
|
123 | const { node } = nodePath;
|
124 | if (t.isCallExpression(node.declaration) && isAtom(t, node.declaration.callee)) {
|
125 | const filename = state.filename || "unknown";
|
126 | const atomKey = `${filename}/defaultExport`;
|
127 | const buildExport = templateBuilder(
|
128 | `export default globalThis.jotaiAtomCache.get(%%atomKey%%, %%atom%%)`
|
129 | );
|
130 | const ast = buildExport({
|
131 | atomKey: t.stringLiteral(atomKey),
|
132 | atom: node.declaration
|
133 | });
|
134 | nodePath.replaceWith(ast);
|
135 | }
|
136 | },
|
137 | VariableDeclarator(nodePath, state) {
|
138 | var _a, _b;
|
139 | 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()))) {
|
140 | const filename = state.filename || "unknown";
|
141 | const atomKey = `${filename}/${nodePath.node.id.name}`;
|
142 | const buildAtomDeclaration = templateBuilder(
|
143 | `const %%atomIdentifier%% = globalThis.jotaiAtomCache.get(%%atomKey%%, %%atom%%)`
|
144 | );
|
145 | const ast = buildAtomDeclaration({
|
146 | atomIdentifier: t.identifier(nodePath.node.id.name),
|
147 | atomKey: t.stringLiteral(atomKey),
|
148 | atom: nodePath.node.init
|
149 | });
|
150 | nodePath.parentPath.replaceWith(ast);
|
151 | }
|
152 | }
|
153 | }
|
154 | };
|
155 | }
|
156 |
|
157 | function jotaiPreset() {
|
158 | return {
|
159 | plugins: [debugLabelPlugin, reactRefreshPlugin]
|
160 | };
|
161 | }
|
162 |
|
163 | })
|
164 | };
|
165 | }));
|