1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | var templateBuilder = require('@babel/template');
|
6 |
|
7 | function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
8 |
|
9 | var templateBuilder__default = _interopDefaultLegacy(templateBuilder);
|
10 |
|
11 | function isAtom(t, callee) {
|
12 | if (t.isIdentifier(callee) && atomFunctionNames.includes(callee.name)) {
|
13 | return true;
|
14 | }
|
15 |
|
16 | if (t.isMemberExpression(callee)) {
|
17 | var property = callee.property;
|
18 |
|
19 | if (t.isIdentifier(property) && atomFunctionNames.includes(property.name)) {
|
20 | return true;
|
21 | }
|
22 | }
|
23 |
|
24 | return false;
|
25 | }
|
26 | var atomFunctionNames = ['atom', 'atomFamily', 'atomWithDefault', 'atomWithObservable', 'atomWithReducer', 'atomWithReset', 'atomWithStorage', 'freezeAtom', 'loadable', 'selectAtom', 'splitAtom'];
|
27 |
|
28 | function reactRefreshPlugin(_ref) {
|
29 | var t = _ref.types;
|
30 | return {
|
31 | pre: function pre(_ref2) {
|
32 | var opts = _ref2.opts;
|
33 |
|
34 | if (!opts.filename) {
|
35 | throw new Error('Filename must be available');
|
36 | }
|
37 | },
|
38 | visitor: {
|
39 | Program: {
|
40 | exit: function exit(path) {
|
41 | 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 }")();
|
42 | path.unshiftContainer('body', jotaiAtomCache);
|
43 | }
|
44 | },
|
45 | ExportDefaultDeclaration: function ExportDefaultDeclaration(nodePath, state) {
|
46 | var node = nodePath.node;
|
47 |
|
48 | if (t.isCallExpression(node.declaration) && isAtom(t, node.declaration.callee)) {
|
49 | var filename = state.filename || 'unknown';
|
50 | var atomKey = filename + "/defaultExport";
|
51 | var buildExport = templateBuilder__default["default"]("export default globalThis.jotaiAtomCache.get(%%atomKey%%, %%atom%%)");
|
52 | var ast = buildExport({
|
53 | atomKey: t.stringLiteral(atomKey),
|
54 | atom: node.declaration
|
55 | });
|
56 | nodePath.replaceWith(ast);
|
57 | }
|
58 | },
|
59 | VariableDeclarator: function VariableDeclarator(nodePath, state) {
|
60 | var _nodePath$parentPath$, _nodePath$parentPath$2;
|
61 |
|
62 | 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())) {
|
63 | var filename = state.filename || 'unknown';
|
64 | var atomKey = filename + "/" + nodePath.node.id.name;
|
65 | var buildAtomDeclaration = templateBuilder__default["default"]("const %%atomIdentifier%% = globalThis.jotaiAtomCache.get(%%atomKey%%, %%atom%%)");
|
66 | var ast = buildAtomDeclaration({
|
67 | atomIdentifier: t.identifier(nodePath.node.id.name),
|
68 | atomKey: t.stringLiteral(atomKey),
|
69 | atom: nodePath.node.init
|
70 | });
|
71 | nodePath.parentPath.replaceWith(ast);
|
72 | }
|
73 | }
|
74 | }
|
75 | };
|
76 | }
|
77 |
|
78 | exports["default"] = reactRefreshPlugin;
|