-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathcreate-registry.ts
More file actions
136 lines (130 loc) · 3.65 KB
/
create-registry.ts
File metadata and controls
136 lines (130 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import type * as babel from '@babel/core';
import * as t from '@babel/types';
import {
IMPORT_PATCH_REGISTRY,
IMPORT_REFRESH,
IMPORT_REGISTRY,
} from './constants';
import { getHotIdentifier } from './get-hot-identifier';
import { getImportIdentifier } from './get-import-identifier';
import { getRootStatementPath } from './get-root-statement-path';
import type { StateContext } from './types';
const REGISTRY = 'REGISTRY';
const SOLID_REFRESH = 'solid-refresh';
const SOLID_REFRESH_PREV = 'solid-refresh-prev';
function createBunInlineHMR(
state: StateContext,
path: babel.NodePath,
registryId: t.Identifier,
): t.Statement[] {
const hotMeta = getHotIdentifier(state);
const patchRegistryId = getImportIdentifier(
state,
path,
IMPORT_PATCH_REGISTRY,
);
const hotData = t.memberExpression(hotMeta, t.identifier('data'));
const hotDataRefresh = t.memberExpression(
hotData,
t.stringLiteral(SOLID_REFRESH),
true,
);
const hotDataPrev = t.memberExpression(
hotData,
t.stringLiteral(SOLID_REFRESH_PREV),
true,
);
const assignRefresh = t.expressionStatement(
t.assignmentExpression(
'=',
hotDataRefresh,
t.logicalExpression('||', hotDataRefresh, registryId),
),
);
const assignPrev = t.expressionStatement(
t.assignmentExpression('=', hotDataPrev, registryId),
);
const modParam = t.identifier('mod');
const acceptCallback = t.arrowFunctionExpression(
[modParam],
t.blockStatement([
t.ifStatement(
t.logicalExpression(
'||',
t.binaryExpression('==', modParam, t.nullLiteral()),
t.callExpression(patchRegistryId, [hotDataRefresh, hotDataPrev]),
),
t.blockStatement([
t.expressionStatement(
t.callExpression(
t.memberExpression(
t.memberExpression(
t.identifier('window'),
t.identifier('location'),
),
t.identifier('reload'),
),
[],
),
),
]),
),
]),
);
const acceptCall = t.expressionStatement(
t.callExpression(t.memberExpression(hotMeta, t.identifier('accept')), [
acceptCallback,
]),
);
return [assignRefresh, assignPrev, acceptCall];
}
export function createRegistry(
state: StateContext,
path: babel.NodePath,
): t.Identifier {
const current = state.imports.get(REGISTRY);
if (current) {
return current;
}
const root = getRootStatementPath(path);
const identifier = path.scope.generateUidIdentifier(REGISTRY);
root.scope.registerDeclaration(
root.insertBefore(
t.variableDeclaration('const', [
t.variableDeclarator(
identifier,
t.callExpression(
getImportIdentifier(state, path, IMPORT_REGISTRY),
[],
),
),
]),
)[0],
);
const pathToHot = getHotIdentifier(state);
const programPath = path.scope.getProgramParent()
.path as babel.NodePath<t.Program>;
if (state.bundler === 'bun') {
const bunHMRStatements = createBunInlineHMR(state, path, identifier);
programPath.pushContainer('body', [
t.ifStatement(pathToHot, t.blockStatement(bunHMRStatements)),
]);
} else {
programPath.pushContainer('body', [
t.ifStatement(
pathToHot,
t.blockStatement([
t.expressionStatement(
t.callExpression(getImportIdentifier(state, path, IMPORT_REFRESH), [
t.stringLiteral(state.bundler),
pathToHot,
identifier,
]),
),
]),
),
]);
}
state.imports.set(REGISTRY, identifier);
return identifier;
}