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', 'atomWithHash', 'atomWithImmer', 'atomWithInfiniteQuery', 'atomWithMachine', 'atomWithMutation', 'atomWithObservable', 'atomWithProxy', 'atomWithQuery', 'atomWithReducer', 'atomWithReset', 'atomWithSubscription', 'atomWithStorage', 'atomWithStore', 'freezeAtom', 'loadable', 'selectAtom', 'splitAtom'];
|
27 |
|
28 | var templateBuilder = _templateBuilder__default["default"].default || _templateBuilder__default["default"];
|
29 | function reactRefreshPlugin(_ref) {
|
30 | var t = _ref.types;
|
31 | return {
|
32 | pre: function pre(_ref2) {
|
33 | var opts = _ref2.opts;
|
34 |
|
35 | if (!opts.filename) {
|
36 | throw new Error('Filename must be available');
|
37 | }
|
38 | },
|
39 | visitor: {
|
40 | Program: {
|
41 | exit: function exit(path) {
|
42 | 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 }")();
|
43 | path.unshiftContainer('body', jotaiAtomCache);
|
44 | }
|
45 | },
|
46 | ExportDefaultDeclaration: function ExportDefaultDeclaration(nodePath, state) {
|
47 | var node = nodePath.node;
|
48 |
|
49 | if (t.isCallExpression(node.declaration) && isAtom(t, node.declaration.callee)) {
|
50 | var filename = state.filename || 'unknown';
|
51 | var atomKey = filename + "/defaultExport";
|
52 | var buildExport = templateBuilder("export default globalThis.jotaiAtomCache.get(%%atomKey%%, %%atom%%)");
|
53 | var ast = buildExport({
|
54 | atomKey: t.stringLiteral(atomKey),
|
55 | atom: node.declaration
|
56 | });
|
57 | nodePath.replaceWith(ast);
|
58 | }
|
59 | },
|
60 | VariableDeclarator: function VariableDeclarator(nodePath, state) {
|
61 | var _nodePath$parentPath$, _nodePath$parentPath$2;
|
62 |
|
63 | 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())) {
|
64 | var filename = state.filename || 'unknown';
|
65 | var atomKey = filename + "/" + nodePath.node.id.name;
|
66 | var buildAtomDeclaration = templateBuilder("const %%atomIdentifier%% = globalThis.jotaiAtomCache.get(%%atomKey%%, %%atom%%)");
|
67 | var ast = buildAtomDeclaration({
|
68 | atomIdentifier: t.identifier(nodePath.node.id.name),
|
69 | atomKey: t.stringLiteral(atomKey),
|
70 | atom: nodePath.node.init
|
71 | });
|
72 | nodePath.parentPath.replaceWith(ast);
|
73 | }
|
74 | }
|
75 | }
|
76 | };
|
77 | }
|
78 |
|
79 | exports["default"] = reactRefreshPlugin;
|