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