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