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