UNPKG

3.64 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', debugLabelPlugin);
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 // Core
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 // jotai-xstate
42 "atomWithMachine",
43 // jotai-immer
44 "atomWithImmer",
45 // jotai-valtio
46 "atomWithProxy",
47 // jotai-trpc + jotai-relay
48 "atomWithQuery",
49 "atomWithMutation",
50 "atomWithSubscription",
51 // jotai-redux + jotai-zustand
52 "atomWithStore",
53 // jotai-location
54 "atomWithHash",
55 "atomWithLocation",
56 // jotai-optics
57 "focusAtom",
58 // jotai-form
59 "atomWithValidate",
60 "validateAtoms",
61 // jotai-cache
62 "atomWithCache",
63 // jotai-recoil
64 "atomWithRecoilValue"
65 ];
66
67 const templateBuilder = _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(`
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 })
111 };
112}));