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