-
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathinject-client.ts
More file actions
164 lines (148 loc) · 5.12 KB
/
inject-client.ts
File metadata and controls
164 lines (148 loc) · 5.12 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import type { types as Babel } from "@babel/core"
import type { ParseResult } from "@babel/parser"
import type { NodePath } from "@babel/traverse"
import { gen, parse, t, trav } from "./babel"
const ALL_EXPORTS = ["links"]
const transform = (ast: ParseResult<Babel.File>, clientConfig: string) => {
const hocs: Array<[string, Babel.Identifier]> = []
function getHocUid(path: NodePath, hocName: string) {
const uid = path.scope.generateUidIdentifier(hocName)
hocs.push([hocName, uid])
return uid
}
const clientConfigObj = JSON.parse(clientConfig)
// convert plugins array to array of identifiers
if (clientConfigObj.plugins) {
clientConfigObj.plugins = clientConfigObj.plugins
.replace("[", "")
.replace("]", "")
.split(",")
.map((plugin: string) => t.identifier(plugin.trim()))
}
const clientConfigExpression = t.objectExpression(
Object.entries(clientConfigObj).map(([key, value]) =>
t.objectProperty(t.identifier(key), key === "plugins" ? t.arrayExpression(value as any) : t.valueToNode(value))
)
)
function uppercaseFirstLetter(str: string) {
return str.charAt(0).toUpperCase() + str.slice(1)
}
const transformations: Array<() => void> = []
trav(ast, {
ExportDeclaration(path) {
if (path.isExportDefaultDeclaration()) {
const declaration = path.get("declaration")
// prettier-ignore
const expr = declaration.isExpression()
? declaration.node
: declaration.isFunctionDeclaration()
? toFunctionExpression(declaration.node)
: undefined
if (expr) {
const uid = getHocUid(path, "withViteDevTools")
declaration.replaceWith(t.callExpression(uid, [expr, clientConfigExpression]))
}
return
}
if (path.isExportNamedDeclaration()) {
const decl = path.get("declaration")
if (decl.isVariableDeclaration()) {
for (const varDeclarator of decl.get("declarations")) {
const id = varDeclarator.get("id")
const init = varDeclarator.get("init")
const expr = init.node
if (!expr) return
if (!id.isIdentifier()) return
const { name } = id.node
if (!ALL_EXPORTS.includes(name)) return
const uid = getHocUid(path, `with${uppercaseFirstLetter(name)}Wrapper`)
init.replaceWith(t.callExpression(uid, [expr, t.identifier("rdtStylesheet")]))
}
return
}
if (decl.isFunctionDeclaration()) {
const { id } = decl.node
if (!id) return
const { name } = id
if (!ALL_EXPORTS.includes(name)) return
const uid = getHocUid(path, `with${uppercaseFirstLetter(name)}Wrapper`)
decl.replaceWith(
t.variableDeclaration("const", [
t.variableDeclarator(
t.identifier(name),
t.callExpression(uid, [toFunctionExpression(decl.node), t.identifier("rdtStylesheet")])
),
])
)
}
// Handle `export { App as default };`
const specifiers = path.node.specifiers
for (const specifier of specifiers) {
if (
t.isExportSpecifier(specifier) &&
t.isIdentifier(specifier.exported) &&
specifier.exported.name === "default" &&
t.isIdentifier(specifier.local)
) {
const localName = specifier.local.name
const uid = getHocUid(path, "withViteDevTools")
// Insert the wrapped default export
transformations.push(() => {
path.insertBefore(
t.exportDefaultDeclaration(t.callExpression(uid, [t.identifier(localName), clientConfigExpression]))
)
// Remove the original export specifier
const remainingSpecifiers = path.node.specifiers.filter(
(s) => !(t.isExportSpecifier(s) && t.isIdentifier(s.exported) && s.exported.name === "default")
)
if (remainingSpecifiers.length > 0) {
path.replaceWith(t.exportNamedDeclaration(null, remainingSpecifiers, path.node.source))
} else {
path.remove()
}
})
}
}
}
},
})
for (const transformation of transformations) {
transformation()
}
if (hocs.length > 0) {
ast.program.body.unshift(
t.importDeclaration(
hocs.map(([name, identifier]) => t.importSpecifier(identifier, t.identifier(name))),
t.stringLiteral("react-router-devtools/client")
),
t.importDeclaration(
[t.importDefaultSpecifier(t.identifier("rdtStylesheet"))],
t.stringLiteral("react-router-devtools/client.css?url")
)
)
}
return hocs.length > 0
}
function toFunctionExpression(decl: Babel.FunctionDeclaration) {
return t.functionExpression(decl.id, decl.params, decl.body, decl.generator, decl.async)
}
export function injectRdtClient(code: string, clientConfig: string, pluginImports: string, id: string) {
const [filePath] = id.split("?")
const ast = parse(code, { sourceType: "module" })
const didTransform = transform(ast, clientConfig)
if (!didTransform) {
return { code }
}
const generatedOutput = gen(ast, { sourceMaps: true, sourceFileName: filePath, filename: id })
const output = `${pluginImports}\n${generatedOutput.code}`
if (!output.includes("export const links")) {
return {
code: [output, "", `export const links = () => [{ rel: "stylesheet", href: rdtStylesheet }];`].join("\n"),
map: generatedOutput.map,
}
}
return {
code: output,
map: generatedOutput.map,
}
}