@tanstack/router-plugin 1.39.9 → 1.39.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/compilers.cjs +144 -105
- package/dist/cjs/compilers.cjs.map +1 -1
- package/dist/esm/compilers.js +144 -105
- package/dist/esm/compilers.js.map +1 -1
- package/package.json +1 -1
- package/src/compilers.ts +233 -144
package/dist/cjs/compilers.cjs
CHANGED
|
@@ -34,97 +34,107 @@ async function compileFile(opts) {
|
|
|
34
34
|
Program: {
|
|
35
35
|
enter(programPath, state) {
|
|
36
36
|
const splitUrl = `${constants.splitPrefix}:${opts.filename}?${constants.splitPrefix}`;
|
|
37
|
+
let existingCompImportPath = null;
|
|
38
|
+
let existingLoaderImportPath = null;
|
|
37
39
|
programPath.traverse(
|
|
38
40
|
{
|
|
39
41
|
CallExpression: (path) => {
|
|
40
|
-
if (path.node.callee
|
|
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
|
-
programPath.unshiftContainer("body", [
|
|
105
|
-
template__namespace.smart(
|
|
106
|
-
`const $$splitLoaderImporter = () => import('${splitUrl}')`
|
|
107
|
-
)()
|
|
108
|
-
]);
|
|
109
|
-
}
|
|
110
|
-
prop.value = template__namespace.expression(
|
|
111
|
-
`lazyFn($$splitLoaderImporter, 'loader')`
|
|
112
|
-
)();
|
|
113
|
-
found = true;
|
|
114
|
-
}
|
|
42
|
+
if (!t__namespace.isIdentifier(path.node.callee)) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
if (!(path.node.callee.name === "createRoute" || path.node.callee.name === "createFileRoute")) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
if (t__namespace.isCallExpression(path.parentPath.node)) {
|
|
49
|
+
const options = resolveIdentifier(
|
|
50
|
+
path,
|
|
51
|
+
path.parentPath.node.arguments[0]
|
|
52
|
+
);
|
|
53
|
+
let found = false;
|
|
54
|
+
const hasImportedOrDefinedIdentifier = (name) => {
|
|
55
|
+
return programPath.scope.hasBinding(name);
|
|
56
|
+
};
|
|
57
|
+
if (t__namespace.isObjectExpression(options)) {
|
|
58
|
+
options.properties.forEach((prop) => {
|
|
59
|
+
if (t__namespace.isObjectProperty(prop)) {
|
|
60
|
+
if (t__namespace.isIdentifier(prop.key)) {
|
|
61
|
+
if (prop.key.name === "component") {
|
|
62
|
+
const value = prop.value;
|
|
63
|
+
if (t__namespace.isIdentifier(value)) {
|
|
64
|
+
existingCompImportPath = getImportSpecifierAndPathFromLocalName(
|
|
65
|
+
programPath,
|
|
66
|
+
value.name
|
|
67
|
+
).path;
|
|
68
|
+
removeIdentifierLiteral(path, value);
|
|
69
|
+
}
|
|
70
|
+
if (!hasImportedOrDefinedIdentifier(
|
|
71
|
+
"lazyRouteComponent"
|
|
72
|
+
)) {
|
|
73
|
+
programPath.unshiftContainer("body", [
|
|
74
|
+
template__namespace.statement(
|
|
75
|
+
`import { lazyRouteComponent } from '@tanstack/react-router'`
|
|
76
|
+
)()
|
|
77
|
+
]);
|
|
78
|
+
}
|
|
79
|
+
if (!hasImportedOrDefinedIdentifier(
|
|
80
|
+
"$$splitComponentImporter"
|
|
81
|
+
)) {
|
|
82
|
+
programPath.unshiftContainer("body", [
|
|
83
|
+
template__namespace.statement(
|
|
84
|
+
`const $$splitComponentImporter = () => import('${splitUrl}')`
|
|
85
|
+
)()
|
|
86
|
+
]);
|
|
87
|
+
}
|
|
88
|
+
prop.value = template__namespace.expression(
|
|
89
|
+
`lazyRouteComponent($$splitComponentImporter, 'component')`
|
|
90
|
+
)();
|
|
91
|
+
programPath.pushContainer("body", [
|
|
92
|
+
template__namespace.statement(
|
|
93
|
+
`function DummyComponent() { return null }`
|
|
94
|
+
)()
|
|
95
|
+
]);
|
|
96
|
+
found = true;
|
|
97
|
+
} else if (prop.key.name === "loader") {
|
|
98
|
+
const value = prop.value;
|
|
99
|
+
if (t__namespace.isIdentifier(value)) {
|
|
100
|
+
existingLoaderImportPath = getImportSpecifierAndPathFromLocalName(
|
|
101
|
+
programPath,
|
|
102
|
+
value.name
|
|
103
|
+
).path;
|
|
104
|
+
removeIdentifierLiteral(path, value);
|
|
115
105
|
}
|
|
106
|
+
if (!hasImportedOrDefinedIdentifier("lazyFn")) {
|
|
107
|
+
programPath.unshiftContainer("body", [
|
|
108
|
+
template__namespace.smart(
|
|
109
|
+
`import { lazyFn } from '@tanstack/react-router'`
|
|
110
|
+
)()
|
|
111
|
+
]);
|
|
112
|
+
}
|
|
113
|
+
if (!hasImportedOrDefinedIdentifier(
|
|
114
|
+
"$$splitLoaderImporter"
|
|
115
|
+
)) {
|
|
116
|
+
programPath.unshiftContainer("body", [
|
|
117
|
+
template__namespace.statement(
|
|
118
|
+
`const $$splitLoaderImporter = () => import('${splitUrl}')`
|
|
119
|
+
)()
|
|
120
|
+
]);
|
|
121
|
+
}
|
|
122
|
+
prop.value = template__namespace.expression(
|
|
123
|
+
`lazyFn($$splitLoaderImporter, 'loader')`
|
|
124
|
+
)();
|
|
125
|
+
found = true;
|
|
116
126
|
}
|
|
117
|
-
|
|
118
|
-
});
|
|
127
|
+
}
|
|
119
128
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
129
|
+
programPath.scope.crawl();
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
if (found) {
|
|
133
|
+
programPath.pushContainer("body", [
|
|
134
|
+
template__namespace.statement(
|
|
135
|
+
`function TSR_Dummy_Component() {}`
|
|
136
|
+
)()
|
|
137
|
+
]);
|
|
128
138
|
}
|
|
129
139
|
}
|
|
130
140
|
}
|
|
@@ -132,6 +142,17 @@ async function compileFile(opts) {
|
|
|
132
142
|
state
|
|
133
143
|
);
|
|
134
144
|
eliminateUnreferencedIdentifiers.eliminateUnreferencedIdentifiers(programPath);
|
|
145
|
+
if (existingCompImportPath || existingLoaderImportPath) {
|
|
146
|
+
programPath.traverse({
|
|
147
|
+
ImportDeclaration(path) {
|
|
148
|
+
if (path.node.specifiers.length > 0)
|
|
149
|
+
return;
|
|
150
|
+
if (path.node.source.value === existingCompImportPath || path.node.source.value === existingLoaderImportPath) {
|
|
151
|
+
path.remove();
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
}
|
|
135
156
|
}
|
|
136
157
|
}
|
|
137
158
|
}
|
|
@@ -145,6 +166,22 @@ async function compileFile(opts) {
|
|
|
145
166
|
})
|
|
146
167
|
});
|
|
147
168
|
}
|
|
169
|
+
function getImportSpecifierAndPathFromLocalName(programPath, name) {
|
|
170
|
+
let specifier = null;
|
|
171
|
+
let path = null;
|
|
172
|
+
programPath.traverse({
|
|
173
|
+
ImportDeclaration(importPath) {
|
|
174
|
+
const found = importPath.node.specifiers.find(
|
|
175
|
+
(targetSpecifier) => targetSpecifier.local.name === name
|
|
176
|
+
);
|
|
177
|
+
if (found) {
|
|
178
|
+
specifier = found;
|
|
179
|
+
path = importPath.node.source.value;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
return { specifier, path };
|
|
184
|
+
}
|
|
148
185
|
function resolveIdentifier(path, node) {
|
|
149
186
|
if (t__namespace.isIdentifier(node)) {
|
|
150
187
|
const binding = path.scope.getBinding(node.name);
|
|
@@ -187,28 +224,30 @@ async function splitFile(opts) {
|
|
|
187
224
|
programPath.traverse(
|
|
188
225
|
{
|
|
189
226
|
CallExpression: (path) => {
|
|
190
|
-
if (path.node.callee
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
227
|
+
if (!t__namespace.isIdentifier(path.node.callee)) {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
if (!(path.node.callee.name === "createRoute" || path.node.callee.name === "createFileRoute")) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
if (t__namespace.isCallExpression(path.parentPath.node)) {
|
|
234
|
+
const options = resolveIdentifier(
|
|
235
|
+
path,
|
|
236
|
+
path.parentPath.node.arguments[0]
|
|
237
|
+
);
|
|
238
|
+
if (t__namespace.isObjectExpression(options)) {
|
|
239
|
+
options.properties.forEach((prop) => {
|
|
240
|
+
if (t__namespace.isObjectProperty(prop)) {
|
|
241
|
+
splitNodeTypes.forEach((type) => {
|
|
242
|
+
if (t__namespace.isIdentifier(prop.key)) {
|
|
243
|
+
if (prop.key.name === type) {
|
|
244
|
+
splitNodesByType[type] = prop.value;
|
|
245
|
+
}
|
|
207
246
|
}
|
|
208
247
|
});
|
|
209
|
-
options.properties = [];
|
|
210
248
|
}
|
|
211
|
-
}
|
|
249
|
+
});
|
|
250
|
+
options.properties = [];
|
|
212
251
|
}
|
|
213
252
|
}
|
|
214
253
|
}
|
|
@@ -254,7 +293,7 @@ async function splitFile(opts) {
|
|
|
254
293
|
)
|
|
255
294
|
])
|
|
256
295
|
);
|
|
257
|
-
} else if (t__namespace.isImportSpecifier(splitNode)) {
|
|
296
|
+
} else if (t__namespace.isImportSpecifier(splitNode) || t__namespace.isImportDefaultSpecifier(splitNode)) {
|
|
258
297
|
programPath.pushContainer(
|
|
259
298
|
"body",
|
|
260
299
|
t__namespace.variableDeclaration("const", [
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compilers.cjs","sources":["../../src/compilers.ts"],"sourcesContent":["import * as t from '@babel/types'\nimport * as template from '@babel/template'\nimport { splitPrefix } from './constants'\nimport { eliminateUnreferencedIdentifiers } from './eliminateUnreferencedIdentifiers'\nimport type * as babel from '@babel/core'\nimport type { CompileAstFn } from './ast'\n\ntype SplitModulesById = Record<\n string,\n { id: string; node: t.FunctionExpression }\n>\n\ninterface State {\n filename: string\n opts: {\n minify: boolean\n root: string\n }\n imported: Record<string, boolean>\n refs: Set<any>\n serverIndex: number\n splitIndex: number\n splitModulesById: SplitModulesById\n}\n\nexport async function compileFile(opts: {\n code: string\n compileAst: CompileAstFn\n filename: string\n}) {\n return await opts.compileAst({\n code: opts.code,\n filename: opts.filename,\n getBabelConfig: () => ({\n plugins: [\n [\n {\n visitor: {\n Program: {\n enter(programPath: babel.NodePath<t.Program>, state: State) {\n const splitUrl = `${splitPrefix}:${opts.filename}?${splitPrefix}`\n\n programPath.traverse(\n {\n CallExpression: (path) => {\n if (path.node.callee.type === 'Identifier') {\n if (\n path.node.callee.name === 'createRoute' ||\n path.node.callee.name === 'createFileRoute'\n ) {\n if (\n path.parentPath.node.type === 'CallExpression'\n ) {\n const options = resolveIdentifier(\n path,\n path.parentPath.node.arguments[0],\n )\n\n let found = false\n\n const hasImportedOrDefinedIdentifier = (\n name: string,\n ) => {\n return programPath.scope.hasBinding(name)\n }\n\n if (t.isObjectExpression(options)) {\n options.properties.forEach((prop) => {\n if (t.isObjectProperty(prop)) {\n if (t.isIdentifier(prop.key)) {\n if (prop.key.name === 'component') {\n const value = prop.value\n\n if (t.isIdentifier(value)) {\n removeIdentifierLiteral(path, value)\n }\n\n // Prepend the import statement to the program along with the importer function\n // Check to see if lazyRouteComponent is already imported before attempting\n // to import it again\n\n if (\n !hasImportedOrDefinedIdentifier(\n 'lazyRouteComponent',\n )\n ) {\n programPath.unshiftContainer('body', [\n template.smart(\n `import { lazyRouteComponent } from '@tanstack/react-router'`,\n )() as t.Statement,\n ])\n }\n\n if (\n !hasImportedOrDefinedIdentifier(\n '$$splitComponentImporter',\n )\n ) {\n programPath.unshiftContainer('body', [\n template.smart(\n `const $$splitComponentImporter = () => import('${splitUrl}')`,\n )() as t.Statement,\n ])\n }\n\n prop.value = template.expression(\n `lazyRouteComponent($$splitComponentImporter, 'component')`,\n )() as any\n\n programPath.pushContainer('body', [\n template.smart(\n `function DummyComponent() { return null }`,\n )() as t.Statement,\n ])\n\n found = true\n } else if (prop.key.name === 'loader') {\n const value = prop.value\n\n if (t.isIdentifier(value)) {\n removeIdentifierLiteral(path, value)\n }\n\n // Prepend the import statement to the program along with the importer function\n\n if (\n !hasImportedOrDefinedIdentifier(\n 'lazyFn',\n )\n ) {\n programPath.unshiftContainer('body', [\n template.smart(\n `import { lazyFn } from '@tanstack/react-router'`,\n )() as t.Statement,\n ])\n }\n\n if (\n !hasImportedOrDefinedIdentifier(\n '$$splitLoaderImporter',\n )\n ) {\n programPath.unshiftContainer('body', [\n template.smart(\n `const $$splitLoaderImporter = () => import('${splitUrl}')`,\n )() as t.Statement,\n ])\n }\n\n prop.value = template.expression(\n `lazyFn($$splitLoaderImporter, 'loader')`,\n )() as any\n\n found = true\n }\n }\n }\n\n programPath.scope.crawl()\n })\n }\n\n if (found as boolean) {\n programPath.pushContainer('body', [\n template.smart(\n `function TSR_Dummy_Component() {}`,\n )() as t.Statement,\n ])\n }\n }\n }\n }\n },\n },\n state,\n )\n\n eliminateUnreferencedIdentifiers(programPath)\n },\n },\n },\n },\n {\n root: process.cwd(),\n minify: process.env.NODE_ENV === 'production',\n },\n ],\n ].filter(Boolean),\n }),\n })\n}\n\n// Reusable function to get literal value or resolve variable to literal\nfunction resolveIdentifier(path: any, node: any) {\n if (t.isIdentifier(node)) {\n const binding = path.scope.getBinding(node.name)\n if (\n binding\n // && binding.kind === 'const'\n ) {\n const declarator = binding.path.node\n if (t.isObjectExpression(declarator.init)) {\n return declarator.init\n } else if (t.isFunctionDeclaration(declarator.init)) {\n return declarator.init\n }\n }\n return undefined\n }\n\n return node\n}\n\nfunction removeIdentifierLiteral(path: any, node: any) {\n if (t.isIdentifier(node)) {\n const binding = path.scope.getBinding(node.name)\n if (binding) {\n binding.path.remove()\n }\n }\n}\n\nconst splitNodeTypes = ['component', 'loader'] as const\ntype SplitNodeType = (typeof splitNodeTypes)[number]\n\nexport async function splitFile(opts: {\n code: string\n compileAst: CompileAstFn\n filename: string\n}) {\n return await opts.compileAst({\n code: opts.code,\n filename: opts.filename,\n getBabelConfig: () => ({\n plugins: [\n [\n {\n visitor: {\n Program: {\n enter(programPath: babel.NodePath<t.Program>, state: State) {\n const splitNodesByType: Record<\n SplitNodeType,\n t.Node | undefined\n > = {\n component: undefined,\n loader: undefined,\n }\n\n // Find the node\n programPath.traverse(\n {\n CallExpression: (path) => {\n if (path.node.callee.type === 'Identifier') {\n if (\n path.node.callee.name === 'createRoute' ||\n path.node.callee.name === 'createFileRoute'\n ) {\n if (\n path.parentPath.node.type === 'CallExpression'\n ) {\n const options = resolveIdentifier(\n path,\n path.parentPath.node.arguments[0],\n )\n\n if (t.isObjectExpression(options)) {\n options.properties.forEach((prop) => {\n if (t.isObjectProperty(prop)) {\n splitNodeTypes.forEach((type) => {\n if (t.isIdentifier(prop.key)) {\n if (prop.key.name === type) {\n splitNodesByType[type] = prop.value\n }\n }\n })\n }\n })\n\n // Remove all of the options\n options.properties = []\n }\n }\n }\n }\n },\n },\n state,\n )\n\n splitNodeTypes.forEach((splitType) => {\n let splitNode = splitNodesByType[splitType]\n\n if (!splitNode) {\n return\n }\n\n while (t.isIdentifier(splitNode)) {\n const binding = programPath.scope.getBinding(\n splitNode.name,\n )\n splitNode = binding?.path.node\n }\n\n // Add the node to the program\n if (splitNode) {\n if (t.isFunctionDeclaration(splitNode)) {\n programPath.pushContainer(\n 'body',\n t.variableDeclaration('const', [\n t.variableDeclarator(\n t.identifier(splitType),\n t.functionExpression(\n splitNode.id || null, // Anonymize the function expression\n splitNode.params,\n splitNode.body,\n splitNode.generator,\n splitNode.async,\n ),\n ),\n ]),\n )\n } else if (\n t.isFunctionExpression(splitNode) ||\n t.isArrowFunctionExpression(splitNode)\n ) {\n programPath.pushContainer(\n 'body',\n t.variableDeclaration('const', [\n t.variableDeclarator(\n t.identifier(splitType),\n splitNode as any,\n ),\n ]),\n )\n } else if (t.isImportSpecifier(splitNode)) {\n programPath.pushContainer(\n 'body',\n t.variableDeclaration('const', [\n t.variableDeclarator(\n t.identifier(splitType),\n splitNode.local,\n ),\n ]),\n )\n } else {\n console.info(splitNode)\n throw new Error(\n `Unexpected splitNode type ☝️: ${splitNode.type}`,\n )\n }\n }\n\n // If the splitNode exists at the top of the program\n // then we need to remove that copy\n programPath.node.body = programPath.node.body.filter(\n (node) => {\n return node !== splitNode\n },\n )\n\n // Export the node\n programPath.pushContainer('body', [\n t.exportNamedDeclaration(null, [\n t.exportSpecifier(\n t.identifier(splitType),\n t.identifier(splitType),\n ),\n ]),\n ])\n })\n\n // convert exports to imports from the original file\n programPath.traverse({\n ExportNamedDeclaration(path) {\n // e.g. export const x = 1 or export { x }\n // becomes\n // import { x } from '${opts.id}'\n\n if (path.node.declaration) {\n if (t.isVariableDeclaration(path.node.declaration)) {\n path.replaceWith(\n t.importDeclaration(\n path.node.declaration.declarations.map((decl) =>\n t.importSpecifier(\n t.identifier((decl.id as any).name),\n t.identifier((decl.id as any).name),\n ),\n ),\n t.stringLiteral(\n opts.filename.split(\n `?${splitPrefix}`,\n )[0] as string,\n ),\n ),\n )\n }\n }\n },\n })\n\n eliminateUnreferencedIdentifiers(programPath)\n },\n },\n },\n },\n {\n root: process.cwd(),\n minify: process.env.NODE_ENV === 'production',\n },\n ],\n ].filter(Boolean),\n }),\n })\n}\n"],"names":["splitPrefix","t","template","eliminateUnreferencedIdentifiers"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAyBA,eAAsB,YAAY,MAI/B;AACM,SAAA,MAAM,KAAK,WAAW;AAAA,IAC3B,MAAM,KAAK;AAAA,IACX,UAAU,KAAK;AAAA,IACf,gBAAgB,OAAO;AAAA,MACrB,SAAS;AAAA,QACP;AAAA,UACE;AAAA,YACE,SAAS;AAAA,cACP,SAAS;AAAA,gBACP,MAAM,aAAwC,OAAc;AAC1D,wBAAM,WAAW,GAAGA,UAAAA,WAAW,IAAI,KAAK,QAAQ,IAAIA,UAAW,WAAA;AAEnD,8BAAA;AAAA,oBACV;AAAA,sBACE,gBAAgB,CAAC,SAAS;AACxB,4BAAI,KAAK,KAAK,OAAO,SAAS,cAAc;AAExC,8BAAA,KAAK,KAAK,OAAO,SAAS,iBAC1B,KAAK,KAAK,OAAO,SAAS,mBAC1B;AACA,gCACE,KAAK,WAAW,KAAK,SAAS,kBAC9B;AACA,oCAAM,UAAU;AAAA,gCACd;AAAA,gCACA,KAAK,WAAW,KAAK,UAAU,CAAC;AAAA,8BAAA;AAGlC,kCAAI,QAAQ;AAEN,oCAAA,iCAAiC,CACrC,SACG;AACI,uCAAA,YAAY,MAAM,WAAW,IAAI;AAAA,8BAAA;AAGtC,kCAAAC,aAAE,mBAAmB,OAAO,GAAG;AACzB,wCAAA,WAAW,QAAQ,CAAC,SAAS;AAC/B,sCAAAA,aAAE,iBAAiB,IAAI,GAAG;AAC5B,wCAAIA,aAAE,aAAa,KAAK,GAAG,GAAG;A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oBACnB,uBAAuB,MAAM;AAKvB,0BAAA,KAAK,KAAK,aAAa;AACzB,4BAAIA,aAAE,sBAAsB,KAAK,KAAK,WAAW,GAAG;AAC7C,+BAAA;AAAA,4BACHA,aAAE;AAAA,8BACA,KAAK,KAAK,YAAY,aAAa;AAAA,gCAAI,CAAC,SACtCA,aAAE;AAAA,kCACAA,aAAE,WAAY,KAAK,GAAW,IAAI;AAAA,kCAClCA,aAAE,WAAY,KAAK,GAAW,IAAI;AAAA,gCACpC;AAAA,8BACF;AAAA,8BACAA,aAAE;AAAA,gCACA,KAAK,SAAS;AAAA,kCACZ,IAAID,UAAAA,WAAW;AAAA,kCACf,CAAC;AAAA,8BACL;AAAA,4BACF;AAAA,0BAAA;AAAA,wBAEJ;AAAA,sBACF;AAAA,oBACF;AAAA,kBAAA,CACD;AAEDG,mDAAA,iCAAiC,WAAW;AAAA,gBAC9C;AAAA,cACF;AAAA,YACF;AAAA,UACF;AAAA,UACA;AAAA,YACE,MAAM,QAAQ,IAAI;AAAA,YAClB,QAAQ,QAAQ,IAAI,aAAa;AAAA,UACnC;AAAA,QACF;AAAA,MAAA,EACA,OAAO,OAAO;AAAA,IAAA;AAAA,EAClB,CACD;AACH;;;"}
|
|
1
|
+
{"version":3,"file":"compilers.cjs","sources":["../../src/compilers.ts"],"sourcesContent":["import * as t from '@babel/types'\nimport * as template from '@babel/template'\nimport { splitPrefix } from './constants'\nimport { eliminateUnreferencedIdentifiers } from './eliminateUnreferencedIdentifiers'\nimport type * as babel from '@babel/core'\nimport type { CompileAstFn } from './ast'\n\ntype SplitModulesById = Record<\n string,\n { id: string; node: t.FunctionExpression }\n>\n\ninterface State {\n filename: string\n opts: {\n minify: boolean\n root: string\n }\n imported: Record<string, boolean>\n refs: Set<any>\n serverIndex: number\n splitIndex: number\n splitModulesById: SplitModulesById\n}\n\nexport async function compileFile(opts: {\n code: string\n compileAst: CompileAstFn\n filename: string\n}) {\n return await opts.compileAst({\n code: opts.code,\n filename: opts.filename,\n getBabelConfig: () => ({\n plugins: [\n [\n {\n visitor: {\n Program: {\n enter(programPath: babel.NodePath<t.Program>, state: State) {\n const splitUrl = `${splitPrefix}:${opts.filename}?${splitPrefix}`\n\n /**\n * If the component for the route is being imported from\n * another file, this is to track the path to that file\n * the path itself doesn't matter, we just need to keep\n * track of it so that we can remove it from the imports\n * list if it's not being used like:\n *\n * `import '../shared/imported'`\n */\n let existingCompImportPath: string | null = null\n let existingLoaderImportPath: string | null = null\n\n programPath.traverse(\n {\n CallExpression: (path) => {\n if (!t.isIdentifier(path.node.callee)) {\n return\n }\n\n if (\n !(\n path.node.callee.name === 'createRoute' ||\n path.node.callee.name === 'createFileRoute'\n )\n ) {\n return\n }\n\n if (t.isCallExpression(path.parentPath.node)) {\n const options = resolveIdentifier(\n path,\n path.parentPath.node.arguments[0],\n )\n\n let found = false\n\n const hasImportedOrDefinedIdentifier = (\n name: string,\n ) => {\n return programPath.scope.hasBinding(name)\n }\n\n if (t.isObjectExpression(options)) {\n options.properties.forEach((prop) => {\n if (t.isObjectProperty(prop)) {\n if (t.isIdentifier(prop.key)) {\n if (prop.key.name === 'component') {\n const value = prop.value\n\n if (t.isIdentifier(value)) {\n existingCompImportPath =\n getImportSpecifierAndPathFromLocalName(\n programPath,\n value.name,\n ).path\n\n removeIdentifierLiteral(path, value)\n }\n\n // Prepend the import statement to the program along with the importer function\n // Check to see if lazyRouteComponent is already imported before attempting\n // to import it again\n\n if (\n !hasImportedOrDefinedIdentifier(\n 'lazyRouteComponent',\n )\n ) {\n programPath.unshiftContainer('body', [\n template.statement(\n `import { lazyRouteComponent } from '@tanstack/react-router'`,\n )(),\n ])\n }\n\n if (\n !hasImportedOrDefinedIdentifier(\n '$$splitComponentImporter',\n )\n ) {\n programPath.unshiftContainer('body', [\n template.statement(\n `const $$splitComponentImporter = () => import('${splitUrl}')`,\n )(),\n ])\n }\n\n prop.value = template.expression(\n `lazyRouteComponent($$splitComponentImporter, 'component')`,\n )()\n\n programPath.pushContainer('body', [\n template.statement(\n `function DummyComponent() { return null }`,\n )(),\n ])\n\n found = true\n } else if (prop.key.name === 'loader') {\n const value = prop.value\n\n if (t.isIdentifier(value)) {\n existingLoaderImportPath =\n getImportSpecifierAndPathFromLocalName(\n programPath,\n value.name,\n ).path\n\n removeIdentifierLiteral(path, value)\n }\n\n // Prepend the import statement to the program along with the importer function\n\n if (\n !hasImportedOrDefinedIdentifier('lazyFn')\n ) {\n programPath.unshiftContainer('body', [\n template.smart(\n `import { lazyFn } from '@tanstack/react-router'`,\n )() as t.Statement,\n ])\n }\n\n if (\n !hasImportedOrDefinedIdentifier(\n '$$splitLoaderImporter',\n )\n ) {\n programPath.unshiftContainer('body', [\n template.statement(\n `const $$splitLoaderImporter = () => import('${splitUrl}')`,\n )(),\n ])\n }\n\n prop.value = template.expression(\n `lazyFn($$splitLoaderImporter, 'loader')`,\n )()\n\n found = true\n }\n }\n }\n\n programPath.scope.crawl()\n })\n }\n\n if (found as boolean) {\n programPath.pushContainer('body', [\n template.statement(\n `function TSR_Dummy_Component() {}`,\n )(),\n ])\n }\n }\n },\n },\n state,\n )\n\n eliminateUnreferencedIdentifiers(programPath)\n\n /**\n * If the component for the route is being imported,\n * and it's not being used, remove the import statement\n * from the program, by checking that the import has no\n * specifiers\n */\n if (\n (existingCompImportPath as string | null) ||\n (existingLoaderImportPath as string | null)\n ) {\n programPath.traverse({\n ImportDeclaration(path) {\n if (path.node.specifiers.length > 0) return\n if (\n path.node.source.value === existingCompImportPath ||\n path.node.source.value === existingLoaderImportPath\n ) {\n path.remove()\n }\n },\n })\n }\n },\n },\n },\n },\n {\n root: process.cwd(),\n minify: process.env.NODE_ENV === 'production',\n },\n ],\n ].filter(Boolean),\n }),\n })\n}\n\nfunction getImportSpecifierAndPathFromLocalName(\n programPath: babel.NodePath<t.Program>,\n name: string,\n): {\n specifier:\n | t.ImportSpecifier\n | t.ImportDefaultSpecifier\n | t.ImportNamespaceSpecifier\n | null\n path: string | null\n} {\n let specifier:\n | t.ImportSpecifier\n | t.ImportDefaultSpecifier\n | t.ImportNamespaceSpecifier\n | null = null\n let path: string | null = null\n\n programPath.traverse({\n ImportDeclaration(importPath) {\n const found = importPath.node.specifiers.find(\n (targetSpecifier) => targetSpecifier.local.name === name,\n )\n if (found) {\n specifier = found\n path = importPath.node.source.value\n }\n },\n })\n\n return { specifier, path }\n}\n\n// Reusable function to get literal value or resolve variable to literal\nfunction resolveIdentifier(path: any, node: any) {\n if (t.isIdentifier(node)) {\n const binding = path.scope.getBinding(node.name)\n if (\n binding\n // && binding.kind === 'const'\n ) {\n const declarator = binding.path.node\n if (t.isObjectExpression(declarator.init)) {\n return declarator.init\n } else if (t.isFunctionDeclaration(declarator.init)) {\n return declarator.init\n }\n }\n return undefined\n }\n\n return node\n}\n\nfunction removeIdentifierLiteral(path: any, node: any) {\n if (t.isIdentifier(node)) {\n const binding = path.scope.getBinding(node.name)\n if (binding) {\n binding.path.remove()\n }\n }\n}\n\nconst splitNodeTypes = ['component', 'loader'] as const\ntype SplitNodeType = (typeof splitNodeTypes)[number]\n\nexport async function splitFile(opts: {\n code: string\n compileAst: CompileAstFn\n filename: string\n}) {\n return await opts.compileAst({\n code: opts.code,\n filename: opts.filename,\n getBabelConfig: () => ({\n plugins: [\n [\n {\n visitor: {\n Program: {\n enter(programPath: babel.NodePath<t.Program>, state: State) {\n const splitNodesByType: Record<\n SplitNodeType,\n t.Node | undefined\n > = {\n component: undefined,\n loader: undefined,\n }\n\n // Find the node\n programPath.traverse(\n {\n CallExpression: (path) => {\n if (!t.isIdentifier(path.node.callee)) {\n return\n }\n\n if (\n !(\n path.node.callee.name === 'createRoute' ||\n path.node.callee.name === 'createFileRoute'\n )\n ) {\n return\n }\n\n if (t.isCallExpression(path.parentPath.node)) {\n const options = resolveIdentifier(\n path,\n path.parentPath.node.arguments[0],\n )\n\n if (t.isObjectExpression(options)) {\n options.properties.forEach((prop) => {\n if (t.isObjectProperty(prop)) {\n splitNodeTypes.forEach((type) => {\n if (t.isIdentifier(prop.key)) {\n if (prop.key.name === type) {\n splitNodesByType[type] = prop.value\n }\n }\n })\n }\n })\n\n // Remove all of the options\n options.properties = []\n }\n }\n },\n },\n state,\n )\n\n splitNodeTypes.forEach((splitType) => {\n let splitNode = splitNodesByType[splitType]\n\n if (!splitNode) {\n return\n }\n\n while (t.isIdentifier(splitNode)) {\n const binding = programPath.scope.getBinding(\n splitNode.name,\n )\n splitNode = binding?.path.node\n }\n\n // Add the node to the program\n if (splitNode) {\n if (t.isFunctionDeclaration(splitNode)) {\n programPath.pushContainer(\n 'body',\n t.variableDeclaration('const', [\n t.variableDeclarator(\n t.identifier(splitType),\n t.functionExpression(\n splitNode.id || null, // Anonymize the function expression\n splitNode.params,\n splitNode.body,\n splitNode.generator,\n splitNode.async,\n ),\n ),\n ]),\n )\n } else if (\n t.isFunctionExpression(splitNode) ||\n t.isArrowFunctionExpression(splitNode)\n ) {\n programPath.pushContainer(\n 'body',\n t.variableDeclaration('const', [\n t.variableDeclarator(\n t.identifier(splitType),\n splitNode as any,\n ),\n ]),\n )\n } else if (\n t.isImportSpecifier(splitNode) ||\n t.isImportDefaultSpecifier(splitNode)\n ) {\n programPath.pushContainer(\n 'body',\n t.variableDeclaration('const', [\n t.variableDeclarator(\n t.identifier(splitType),\n splitNode.local,\n ),\n ]),\n )\n } else {\n console.info(splitNode)\n throw new Error(\n `Unexpected splitNode type ☝️: ${splitNode.type}`,\n )\n }\n }\n\n // If the splitNode exists at the top of the program\n // then we need to remove that copy\n programPath.node.body = programPath.node.body.filter(\n (node) => {\n return node !== splitNode\n },\n )\n\n // Export the node\n programPath.pushContainer('body', [\n t.exportNamedDeclaration(null, [\n t.exportSpecifier(\n t.identifier(splitType),\n t.identifier(splitType),\n ),\n ]),\n ])\n })\n\n // convert exports to imports from the original file\n programPath.traverse({\n ExportNamedDeclaration(path) {\n // e.g. export const x = 1 or export { x }\n // becomes\n // import { x } from '${opts.id}'\n\n if (path.node.declaration) {\n if (t.isVariableDeclaration(path.node.declaration)) {\n path.replaceWith(\n t.importDeclaration(\n path.node.declaration.declarations.map((decl) =>\n t.importSpecifier(\n t.identifier((decl.id as any).name),\n t.identifier((decl.id as any).name),\n ),\n ),\n t.stringLiteral(\n opts.filename.split(\n `?${splitPrefix}`,\n )[0] as string,\n ),\n ),\n )\n }\n }\n },\n })\n\n eliminateUnreferencedIdentifiers(programPath)\n },\n },\n },\n },\n {\n root: process.cwd(),\n minify: process.env.NODE_ENV === 'production',\n },\n ],\n ].filter(Boolean),\n }),\n })\n}\n"],"names":["splitPrefix","t","template","eliminateUnreferencedIdentifiers"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAyBA,eAAsB,YAAY,MAI/B;AACM,SAAA,MAAM,KAAK,WAAW;AAAA,IAC3B,MAAM,KAAK;AAAA,IACX,UAAU,KAAK;AAAA,IACf,gBAAgB,OAAO;AAAA,MACrB,SAAS;AAAA,QACP;AAAA,UACE;AAAA,YACE,SAAS;AAAA,cACP,SAAS;AAAA,gBACP,MAAM,aAAwC,OAAc;AAC1D,wBAAM,WAAW,GAAGA,UAAAA,WAAW,IAAI,KAAK,QAAQ,IAAIA,UAAW,WAAA;AAW/D,sBAAI,yBAAwC;AAC5C,sBAAI,2BAA0C;AAElC,8BAAA;AAAA,oBACV;AAAA,sBACE,gBAAgB,CAAC,SAAS;AACxB,4BAAI,CAACC,aAAE,aAAa,KAAK,KAAK,MAAM,GAAG;AACrC;AAAA,wBACF;AAGE,4BAAA,EACE,KAAK,KAAK,OAAO,SAAS,iBAC1B,KAAK,KAAK,OAAO,SAAS,oBAE5B;AACA;AAAA,wBACF;AAEA,4BAAIA,aAAE,iBAAiB,KAAK,WAAW,IAAI,GAAG;AAC5C,gCAAM,UAAU;AAAA,4BACd;AAAA,4BACA,KAAK,WAAW,KAAK,UAAU,CAAC;AAAA,0BAAA;AAGlC,8BAAI,QAAQ;AAEN,gCAAA,iCAAiC,CACrC,SACG;AACI,mCAAA,YAAY,MAAM,WAAW,IAAI;AA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IAAI;AAAA,gCACpC;AAAA,8BACF;AAAA,8BACAA,aAAE;AAAA,gCACA,KAAK,SAAS;AAAA,kCACZ,IAAID,UAAAA,WAAW;AAAA,kCACf,CAAC;AAAA,8BACL;AAAA,4BACF;AAAA,0BAAA;AAAA,wBAEJ;AAAA,sBACF;AAAA,oBACF;AAAA,kBAAA,CACD;AAEDG,mDAAA,iCAAiC,WAAW;AAAA,gBAC9C;AAAA,cACF;AAAA,YACF;AAAA,UACF;AAAA,UACA;AAAA,YACE,MAAM,QAAQ,IAAI;AAAA,YAClB,QAAQ,QAAQ,IAAI,aAAa;AAAA,UACnC;AAAA,QACF;AAAA,MAAA,EACA,OAAO,OAAO;AAAA,IAAA;AAAA,EAClB,CACD;AACH;;;"}
|
package/dist/esm/compilers.js
CHANGED
|
@@ -14,97 +14,107 @@ async function compileFile(opts) {
|
|
|
14
14
|
Program: {
|
|
15
15
|
enter(programPath, state) {
|
|
16
16
|
const splitUrl = `${splitPrefix}:${opts.filename}?${splitPrefix}`;
|
|
17
|
+
let existingCompImportPath = null;
|
|
18
|
+
let existingLoaderImportPath = null;
|
|
17
19
|
programPath.traverse(
|
|
18
20
|
{
|
|
19
21
|
CallExpression: (path) => {
|
|
20
|
-
if (path.node.callee
|
|
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
|
-
programPath.unshiftContainer("body", [
|
|
85
|
-
template.smart(
|
|
86
|
-
`const $$splitLoaderImporter = () => import('${splitUrl}')`
|
|
87
|
-
)()
|
|
88
|
-
]);
|
|
89
|
-
}
|
|
90
|
-
prop.value = template.expression(
|
|
91
|
-
`lazyFn($$splitLoaderImporter, 'loader')`
|
|
92
|
-
)();
|
|
93
|
-
found = true;
|
|
94
|
-
}
|
|
22
|
+
if (!t.isIdentifier(path.node.callee)) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
if (!(path.node.callee.name === "createRoute" || path.node.callee.name === "createFileRoute")) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (t.isCallExpression(path.parentPath.node)) {
|
|
29
|
+
const options = resolveIdentifier(
|
|
30
|
+
path,
|
|
31
|
+
path.parentPath.node.arguments[0]
|
|
32
|
+
);
|
|
33
|
+
let found = false;
|
|
34
|
+
const hasImportedOrDefinedIdentifier = (name) => {
|
|
35
|
+
return programPath.scope.hasBinding(name);
|
|
36
|
+
};
|
|
37
|
+
if (t.isObjectExpression(options)) {
|
|
38
|
+
options.properties.forEach((prop) => {
|
|
39
|
+
if (t.isObjectProperty(prop)) {
|
|
40
|
+
if (t.isIdentifier(prop.key)) {
|
|
41
|
+
if (prop.key.name === "component") {
|
|
42
|
+
const value = prop.value;
|
|
43
|
+
if (t.isIdentifier(value)) {
|
|
44
|
+
existingCompImportPath = getImportSpecifierAndPathFromLocalName(
|
|
45
|
+
programPath,
|
|
46
|
+
value.name
|
|
47
|
+
).path;
|
|
48
|
+
removeIdentifierLiteral(path, value);
|
|
49
|
+
}
|
|
50
|
+
if (!hasImportedOrDefinedIdentifier(
|
|
51
|
+
"lazyRouteComponent"
|
|
52
|
+
)) {
|
|
53
|
+
programPath.unshiftContainer("body", [
|
|
54
|
+
template.statement(
|
|
55
|
+
`import { lazyRouteComponent } from '@tanstack/react-router'`
|
|
56
|
+
)()
|
|
57
|
+
]);
|
|
58
|
+
}
|
|
59
|
+
if (!hasImportedOrDefinedIdentifier(
|
|
60
|
+
"$$splitComponentImporter"
|
|
61
|
+
)) {
|
|
62
|
+
programPath.unshiftContainer("body", [
|
|
63
|
+
template.statement(
|
|
64
|
+
`const $$splitComponentImporter = () => import('${splitUrl}')`
|
|
65
|
+
)()
|
|
66
|
+
]);
|
|
67
|
+
}
|
|
68
|
+
prop.value = template.expression(
|
|
69
|
+
`lazyRouteComponent($$splitComponentImporter, 'component')`
|
|
70
|
+
)();
|
|
71
|
+
programPath.pushContainer("body", [
|
|
72
|
+
template.statement(
|
|
73
|
+
`function DummyComponent() { return null }`
|
|
74
|
+
)()
|
|
75
|
+
]);
|
|
76
|
+
found = true;
|
|
77
|
+
} else if (prop.key.name === "loader") {
|
|
78
|
+
const value = prop.value;
|
|
79
|
+
if (t.isIdentifier(value)) {
|
|
80
|
+
existingLoaderImportPath = getImportSpecifierAndPathFromLocalName(
|
|
81
|
+
programPath,
|
|
82
|
+
value.name
|
|
83
|
+
).path;
|
|
84
|
+
removeIdentifierLiteral(path, value);
|
|
95
85
|
}
|
|
86
|
+
if (!hasImportedOrDefinedIdentifier("lazyFn")) {
|
|
87
|
+
programPath.unshiftContainer("body", [
|
|
88
|
+
template.smart(
|
|
89
|
+
`import { lazyFn } from '@tanstack/react-router'`
|
|
90
|
+
)()
|
|
91
|
+
]);
|
|
92
|
+
}
|
|
93
|
+
if (!hasImportedOrDefinedIdentifier(
|
|
94
|
+
"$$splitLoaderImporter"
|
|
95
|
+
)) {
|
|
96
|
+
programPath.unshiftContainer("body", [
|
|
97
|
+
template.statement(
|
|
98
|
+
`const $$splitLoaderImporter = () => import('${splitUrl}')`
|
|
99
|
+
)()
|
|
100
|
+
]);
|
|
101
|
+
}
|
|
102
|
+
prop.value = template.expression(
|
|
103
|
+
`lazyFn($$splitLoaderImporter, 'loader')`
|
|
104
|
+
)();
|
|
105
|
+
found = true;
|
|
96
106
|
}
|
|
97
|
-
|
|
98
|
-
});
|
|
107
|
+
}
|
|
99
108
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
109
|
+
programPath.scope.crawl();
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
if (found) {
|
|
113
|
+
programPath.pushContainer("body", [
|
|
114
|
+
template.statement(
|
|
115
|
+
`function TSR_Dummy_Component() {}`
|
|
116
|
+
)()
|
|
117
|
+
]);
|
|
108
118
|
}
|
|
109
119
|
}
|
|
110
120
|
}
|
|
@@ -112,6 +122,17 @@ async function compileFile(opts) {
|
|
|
112
122
|
state
|
|
113
123
|
);
|
|
114
124
|
eliminateUnreferencedIdentifiers(programPath);
|
|
125
|
+
if (existingCompImportPath || existingLoaderImportPath) {
|
|
126
|
+
programPath.traverse({
|
|
127
|
+
ImportDeclaration(path) {
|
|
128
|
+
if (path.node.specifiers.length > 0)
|
|
129
|
+
return;
|
|
130
|
+
if (path.node.source.value === existingCompImportPath || path.node.source.value === existingLoaderImportPath) {
|
|
131
|
+
path.remove();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
}
|
|
115
136
|
}
|
|
116
137
|
}
|
|
117
138
|
}
|
|
@@ -125,6 +146,22 @@ async function compileFile(opts) {
|
|
|
125
146
|
})
|
|
126
147
|
});
|
|
127
148
|
}
|
|
149
|
+
function getImportSpecifierAndPathFromLocalName(programPath, name) {
|
|
150
|
+
let specifier = null;
|
|
151
|
+
let path = null;
|
|
152
|
+
programPath.traverse({
|
|
153
|
+
ImportDeclaration(importPath) {
|
|
154
|
+
const found = importPath.node.specifiers.find(
|
|
155
|
+
(targetSpecifier) => targetSpecifier.local.name === name
|
|
156
|
+
);
|
|
157
|
+
if (found) {
|
|
158
|
+
specifier = found;
|
|
159
|
+
path = importPath.node.source.value;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
return { specifier, path };
|
|
164
|
+
}
|
|
128
165
|
function resolveIdentifier(path, node) {
|
|
129
166
|
if (t.isIdentifier(node)) {
|
|
130
167
|
const binding = path.scope.getBinding(node.name);
|
|
@@ -167,28 +204,30 @@ async function splitFile(opts) {
|
|
|
167
204
|
programPath.traverse(
|
|
168
205
|
{
|
|
169
206
|
CallExpression: (path) => {
|
|
170
|
-
if (path.node.callee
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
207
|
+
if (!t.isIdentifier(path.node.callee)) {
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
if (!(path.node.callee.name === "createRoute" || path.node.callee.name === "createFileRoute")) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
if (t.isCallExpression(path.parentPath.node)) {
|
|
214
|
+
const options = resolveIdentifier(
|
|
215
|
+
path,
|
|
216
|
+
path.parentPath.node.arguments[0]
|
|
217
|
+
);
|
|
218
|
+
if (t.isObjectExpression(options)) {
|
|
219
|
+
options.properties.forEach((prop) => {
|
|
220
|
+
if (t.isObjectProperty(prop)) {
|
|
221
|
+
splitNodeTypes.forEach((type) => {
|
|
222
|
+
if (t.isIdentifier(prop.key)) {
|
|
223
|
+
if (prop.key.name === type) {
|
|
224
|
+
splitNodesByType[type] = prop.value;
|
|
225
|
+
}
|
|
187
226
|
}
|
|
188
227
|
});
|
|
189
|
-
options.properties = [];
|
|
190
228
|
}
|
|
191
|
-
}
|
|
229
|
+
});
|
|
230
|
+
options.properties = [];
|
|
192
231
|
}
|
|
193
232
|
}
|
|
194
233
|
}
|
|
@@ -234,7 +273,7 @@ async function splitFile(opts) {
|
|
|
234
273
|
)
|
|
235
274
|
])
|
|
236
275
|
);
|
|
237
|
-
} else if (t.isImportSpecifier(splitNode)) {
|
|
276
|
+
} else if (t.isImportSpecifier(splitNode) || t.isImportDefaultSpecifier(splitNode)) {
|
|
238
277
|
programPath.pushContainer(
|
|
239
278
|
"body",
|
|
240
279
|
t.variableDeclaration("const", [
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compilers.js","sources":["../../src/compilers.ts"],"sourcesContent":["import * as t from '@babel/types'\nimport * as template from '@babel/template'\nimport { splitPrefix } from './constants'\nimport { eliminateUnreferencedIdentifiers } from './eliminateUnreferencedIdentifiers'\nimport type * as babel from '@babel/core'\nimport type { CompileAstFn } from './ast'\n\ntype SplitModulesById = Record<\n string,\n { id: string; node: t.FunctionExpression }\n>\n\ninterface State {\n filename: string\n opts: {\n minify: boolean\n root: string\n }\n imported: Record<string, boolean>\n refs: Set<any>\n serverIndex: number\n splitIndex: number\n splitModulesById: SplitModulesById\n}\n\nexport async function compileFile(opts: {\n code: string\n compileAst: CompileAstFn\n filename: string\n}) {\n return await opts.compileAst({\n code: opts.code,\n filename: opts.filename,\n getBabelConfig: () => ({\n plugins: [\n [\n {\n visitor: {\n Program: {\n enter(programPath: babel.NodePath<t.Program>, state: State) {\n const splitUrl = `${splitPrefix}:${opts.filename}?${splitPrefix}`\n\n programPath.traverse(\n {\n CallExpression: (path) => {\n if (path.node.callee.type === 'Identifier') {\n if (\n path.node.callee.name === 'createRoute' ||\n path.node.callee.name === 'createFileRoute'\n ) {\n if (\n path.parentPath.node.type === 'CallExpression'\n ) {\n const options = resolveIdentifier(\n path,\n path.parentPath.node.arguments[0],\n )\n\n let found = false\n\n const hasImportedOrDefinedIdentifier = (\n name: string,\n ) => {\n return programPath.scope.hasBinding(name)\n }\n\n if (t.isObjectExpression(options)) {\n options.properties.forEach((prop) => {\n if (t.isObjectProperty(prop)) {\n if (t.isIdentifier(prop.key)) {\n if (prop.key.name === 'component') {\n const value = prop.value\n\n if (t.isIdentifier(value)) {\n removeIdentifierLiteral(path, value)\n }\n\n // Prepend the import statement to the program along with the importer function\n // Check to see if lazyRouteComponent is already imported before attempting\n // to import it again\n\n if (\n !hasImportedOrDefinedIdentifier(\n 'lazyRouteComponent',\n )\n ) {\n programPath.unshiftContainer('body', [\n template.smart(\n `import { lazyRouteComponent } from '@tanstack/react-router'`,\n )() as t.Statement,\n ])\n }\n\n if (\n !hasImportedOrDefinedIdentifier(\n '$$splitComponentImporter',\n )\n ) {\n programPath.unshiftContainer('body', [\n template.smart(\n `const $$splitComponentImporter = () => import('${splitUrl}')`,\n )() as t.Statement,\n ])\n }\n\n prop.value = template.expression(\n `lazyRouteComponent($$splitComponentImporter, 'component')`,\n )() as any\n\n programPath.pushContainer('body', [\n template.smart(\n `function DummyComponent() { return null }`,\n )() as t.Statement,\n ])\n\n found = true\n } else if (prop.key.name === 'loader') {\n const value = prop.value\n\n if (t.isIdentifier(value)) {\n removeIdentifierLiteral(path, value)\n }\n\n // Prepend the import statement to the program along with the importer function\n\n if (\n !hasImportedOrDefinedIdentifier(\n 'lazyFn',\n )\n ) {\n programPath.unshiftContainer('body', [\n template.smart(\n `import { lazyFn } from '@tanstack/react-router'`,\n )() as t.Statement,\n ])\n }\n\n if (\n !hasImportedOrDefinedIdentifier(\n '$$splitLoaderImporter',\n )\n ) {\n programPath.unshiftContainer('body', [\n template.smart(\n `const $$splitLoaderImporter = () => import('${splitUrl}')`,\n )() as t.Statement,\n ])\n }\n\n prop.value = template.expression(\n `lazyFn($$splitLoaderImporter, 'loader')`,\n )() as any\n\n found = true\n }\n }\n }\n\n programPath.scope.crawl()\n })\n }\n\n if (found as boolean) {\n programPath.pushContainer('body', [\n template.smart(\n `function TSR_Dummy_Component() {}`,\n )() as t.Statement,\n ])\n }\n }\n }\n }\n },\n },\n state,\n )\n\n eliminateUnreferencedIdentifiers(programPath)\n },\n },\n },\n },\n {\n root: process.cwd(),\n minify: process.env.NODE_ENV === 'production',\n },\n ],\n ].filter(Boolean),\n }),\n })\n}\n\n// Reusable function to get literal value or resolve variable to literal\nfunction resolveIdentifier(path: any, node: any) {\n if (t.isIdentifier(node)) {\n const binding = path.scope.getBinding(node.name)\n if (\n binding\n // && binding.kind === 'const'\n ) {\n const declarator = binding.path.node\n if (t.isObjectExpression(declarator.init)) {\n return declarator.init\n } else if (t.isFunctionDeclaration(declarator.init)) {\n return declarator.init\n }\n }\n return undefined\n }\n\n return node\n}\n\nfunction removeIdentifierLiteral(path: any, node: any) {\n if (t.isIdentifier(node)) {\n const binding = path.scope.getBinding(node.name)\n if (binding) {\n binding.path.remove()\n }\n }\n}\n\nconst splitNodeTypes = ['component', 'loader'] as const\ntype SplitNodeType = (typeof splitNodeTypes)[number]\n\nexport async function splitFile(opts: {\n code: string\n compileAst: CompileAstFn\n filename: string\n}) {\n return await opts.compileAst({\n code: opts.code,\n filename: opts.filename,\n getBabelConfig: () => ({\n plugins: [\n [\n {\n visitor: {\n Program: {\n enter(programPath: babel.NodePath<t.Program>, state: State) {\n const splitNodesByType: Record<\n SplitNodeType,\n t.Node | undefined\n > = {\n component: undefined,\n loader: undefined,\n }\n\n // Find the node\n programPath.traverse(\n {\n CallExpression: (path) => {\n if (path.node.callee.type === 'Identifier') {\n if (\n path.node.callee.name === 'createRoute' ||\n path.node.callee.name === 'createFileRoute'\n ) {\n if (\n path.parentPath.node.type === 'CallExpression'\n ) {\n const options = resolveIdentifier(\n path,\n path.parentPath.node.arguments[0],\n )\n\n if (t.isObjectExpression(options)) {\n options.properties.forEach((prop) => {\n if (t.isObjectProperty(prop)) {\n splitNodeTypes.forEach((type) => {\n if (t.isIdentifier(prop.key)) {\n if (prop.key.name === type) {\n splitNodesByType[type] = prop.value\n }\n }\n })\n }\n })\n\n // Remove all of the options\n options.properties = []\n }\n }\n }\n }\n },\n },\n state,\n )\n\n splitNodeTypes.forEach((splitType) => {\n let splitNode = splitNodesByType[splitType]\n\n if (!splitNode) {\n return\n }\n\n while (t.isIdentifier(splitNode)) {\n const binding = programPath.scope.getBinding(\n splitNode.name,\n )\n splitNode = binding?.path.node\n }\n\n // Add the node to the program\n if (splitNode) {\n if (t.isFunctionDeclaration(splitNode)) {\n programPath.pushContainer(\n 'body',\n t.variableDeclaration('const', [\n t.variableDeclarator(\n t.identifier(splitType),\n t.functionExpression(\n splitNode.id || null, // Anonymize the function expression\n splitNode.params,\n splitNode.body,\n splitNode.generator,\n splitNode.async,\n ),\n ),\n ]),\n )\n } else if (\n t.isFunctionExpression(splitNode) ||\n t.isArrowFunctionExpression(splitNode)\n ) {\n programPath.pushContainer(\n 'body',\n t.variableDeclaration('const', [\n t.variableDeclarator(\n t.identifier(splitType),\n splitNode as any,\n ),\n ]),\n )\n } else if (t.isImportSpecifier(splitNode)) {\n programPath.pushContainer(\n 'body',\n t.variableDeclaration('const', [\n t.variableDeclarator(\n t.identifier(splitType),\n splitNode.local,\n ),\n ]),\n )\n } else {\n console.info(splitNode)\n throw new Error(\n `Unexpected splitNode type ☝️: ${splitNode.type}`,\n )\n }\n }\n\n // If the splitNode exists at the top of the program\n // then we need to remove that copy\n programPath.node.body = programPath.node.body.filter(\n (node) => {\n return node !== splitNode\n },\n )\n\n // Export the node\n programPath.pushContainer('body', [\n t.exportNamedDeclaration(null, [\n t.exportSpecifier(\n t.identifier(splitType),\n t.identifier(splitType),\n ),\n ]),\n ])\n })\n\n // convert exports to imports from the original file\n programPath.traverse({\n ExportNamedDeclaration(path) {\n // e.g. export const x = 1 or export { x }\n // becomes\n // import { x } from '${opts.id}'\n\n if (path.node.declaration) {\n if (t.isVariableDeclaration(path.node.declaration)) {\n path.replaceWith(\n t.importDeclaration(\n path.node.declaration.declarations.map((decl) =>\n t.importSpecifier(\n t.identifier((decl.id as any).name),\n t.identifier((decl.id as any).name),\n ),\n ),\n t.stringLiteral(\n opts.filename.split(\n `?${splitPrefix}`,\n )[0] as string,\n ),\n ),\n )\n }\n }\n },\n })\n\n eliminateUnreferencedIdentifiers(programPath)\n },\n },\n },\n },\n {\n root: process.cwd(),\n minify: process.env.NODE_ENV === 'production',\n },\n ],\n ].filter(Boolean),\n }),\n })\n}\n"],"names":[],"mappings":";;;;AAyBA,eAAsB,YAAY,MAI/B;AACM,SAAA,MAAM,KAAK,WAAW;AAAA,IAC3B,MAAM,KAAK;AAAA,IACX,UAAU,KAAK;AAAA,IACf,gBAAgB,OAAO;AAAA,MACrB,SAAS;AAAA,QACP;AAAA,UACE;AAAA,YACE,SAAS;AAAA,cACP,SAAS;AAAA,gBACP,MAAM,aAAwC,OAAc;AAC1D,wBAAM,WAAW,GAAG,WAAW,IAAI,KAAK,QAAQ,IAAI,WAAW;AAEnD,8BAAA;AAAA,oBACV;AAAA,sBACE,gBAAgB,CAAC,SAAS;AACxB,4BAAI,KAAK,KAAK,OAAO,SAAS,cAAc;AAExC,8BAAA,KAAK,KAAK,OAAO,SAAS,iBAC1B,KAAK,KAAK,OAAO,SAAS,mBAC1B;AACA,gCACE,KAAK,WAAW,KAAK,SAAS,kBAC9B;AACA,oCAAM,UAAU;AAAA,gCACd;AAAA,gCACA,KAAK,WAAW,KAAK,UAAU,CAAC;AAAA,8BAAA;AAGlC,kCAAI,QAAQ;AAEN,oCAAA,iCAAiC,CACrC,SACG;AACI,uCAAA,YAAY,MAAM,WAAW,IAAI;AAAA,8BAAA;AAGtC,kCAAA,EAAE,mBAAmB,OAAO,GAAG;AACzB,wCAAA,WAAW,QAAQ,CAAC,SAAS;AAC/B,sCAAA,EAAE,iBAAiB,IAAI,GAAG;AAC5B,wCAAI,EAAE,aAAa,KAAK,GAAG,GAAG;AACxB,0CAAA,KAAK,IAAI,SAAS,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AAK,WAAW,GAAG;AAC7C,+BAAA;AAAA,4BACH,EAAE;AAAA,8BACA,KAAK,KAAK,YAAY,aAAa;AAAA,gCAAI,CAAC,SACtC,EAAE;AAAA,kCACA,EAAE,WAAY,KAAK,GAAW,IAAI;AAAA,kCAClC,EAAE,WAAY,KAAK,GAAW,IAAI;AAAA,gCACpC;AAAA,8BACF;AAAA,8BACA,EAAE;AAAA,gCACA,KAAK,SAAS;AAAA,kCACZ,IAAI,WAAW;AAAA,kCACf,CAAC;AAAA,8BACL;AAAA,4BACF;AAAA,0BAAA;AAAA,wBAEJ;AAAA,sBACF;AAAA,oBACF;AAAA,kBAAA,CACD;AAED,mDAAiC,WAAW;AAAA,gBAC9C;AAAA,cACF;AAAA,YACF;AAAA,UACF;AAAA,UACA;AAAA,YACE,MAAM,QAAQ,IAAI;AAAA,YAClB,QAAQ,QAAQ,IAAI,aAAa;AAAA,UACnC;AAAA,QACF;AAAA,MAAA,EACA,OAAO,OAAO;AAAA,IAAA;AAAA,EAClB,CACD;AACH;"}
|
|
1
|
+
{"version":3,"file":"compilers.js","sources":["../../src/compilers.ts"],"sourcesContent":["import * as t from '@babel/types'\nimport * as template from '@babel/template'\nimport { splitPrefix } from './constants'\nimport { eliminateUnreferencedIdentifiers } from './eliminateUnreferencedIdentifiers'\nimport type * as babel from '@babel/core'\nimport type { CompileAstFn } from './ast'\n\ntype SplitModulesById = Record<\n string,\n { id: string; node: t.FunctionExpression }\n>\n\ninterface State {\n filename: string\n opts: {\n minify: boolean\n root: string\n }\n imported: Record<string, boolean>\n refs: Set<any>\n serverIndex: number\n splitIndex: number\n splitModulesById: SplitModulesById\n}\n\nexport async function compileFile(opts: {\n code: string\n compileAst: CompileAstFn\n filename: string\n}) {\n return await opts.compileAst({\n code: opts.code,\n filename: opts.filename,\n getBabelConfig: () => ({\n plugins: [\n [\n {\n visitor: {\n Program: {\n enter(programPath: babel.NodePath<t.Program>, state: State) {\n const splitUrl = `${splitPrefix}:${opts.filename}?${splitPrefix}`\n\n /**\n * If the component for the route is being imported from\n * another file, this is to track the path to that file\n * the path itself doesn't matter, we just need to keep\n * track of it so that we can remove it from the imports\n * list if it's not being used like:\n *\n * `import '../shared/imported'`\n */\n let existingCompImportPath: string | null = null\n let existingLoaderImportPath: string | null = null\n\n programPath.traverse(\n {\n CallExpression: (path) => {\n if (!t.isIdentifier(path.node.callee)) {\n return\n }\n\n if (\n !(\n path.node.callee.name === 'createRoute' ||\n path.node.callee.name === 'createFileRoute'\n )\n ) {\n return\n }\n\n if (t.isCallExpression(path.parentPath.node)) {\n const options = resolveIdentifier(\n path,\n path.parentPath.node.arguments[0],\n )\n\n let found = false\n\n const hasImportedOrDefinedIdentifier = (\n name: string,\n ) => {\n return programPath.scope.hasBinding(name)\n }\n\n if (t.isObjectExpression(options)) {\n options.properties.forEach((prop) => {\n if (t.isObjectProperty(prop)) {\n if (t.isIdentifier(prop.key)) {\n if (prop.key.name === 'component') {\n const value = prop.value\n\n if (t.isIdentifier(value)) {\n existingCompImportPath =\n getImportSpecifierAndPathFromLocalName(\n programPath,\n value.name,\n ).path\n\n removeIdentifierLiteral(path, value)\n }\n\n // Prepend the import statement to the program along with the importer function\n // Check to see if lazyRouteComponent is already imported before attempting\n // to import it again\n\n if (\n !hasImportedOrDefinedIdentifier(\n 'lazyRouteComponent',\n )\n ) {\n programPath.unshiftContainer('body', [\n template.statement(\n `import { lazyRouteComponent } from '@tanstack/react-router'`,\n )(),\n ])\n }\n\n if (\n !hasImportedOrDefinedIdentifier(\n '$$splitComponentImporter',\n )\n ) {\n programPath.unshiftContainer('body', [\n template.statement(\n `const $$splitComponentImporter = () => import('${splitUrl}')`,\n )(),\n ])\n }\n\n prop.value = template.expression(\n `lazyRouteComponent($$splitComponentImporter, 'component')`,\n )()\n\n programPath.pushContainer('body', [\n template.statement(\n `function DummyComponent() { return null }`,\n )(),\n ])\n\n found = true\n } else if (prop.key.name === 'loader') {\n const value = prop.value\n\n if (t.isIdentifier(value)) {\n existingLoaderImportPath =\n getImportSpecifierAndPathFromLocalName(\n programPath,\n value.name,\n ).path\n\n removeIdentifierLiteral(path, value)\n }\n\n // Prepend the import statement to the program along with the importer function\n\n if (\n !hasImportedOrDefinedIdentifier('lazyFn')\n ) {\n programPath.unshiftContainer('body', [\n template.smart(\n `import { lazyFn } from '@tanstack/react-router'`,\n )() as t.Statement,\n ])\n }\n\n if (\n !hasImportedOrDefinedIdentifier(\n '$$splitLoaderImporter',\n )\n ) {\n programPath.unshiftContainer('body', [\n template.statement(\n `const $$splitLoaderImporter = () => import('${splitUrl}')`,\n )(),\n ])\n }\n\n prop.value = template.expression(\n `lazyFn($$splitLoaderImporter, 'loader')`,\n )()\n\n found = true\n }\n }\n }\n\n programPath.scope.crawl()\n })\n }\n\n if (found as boolean) {\n programPath.pushContainer('body', [\n template.statement(\n `function TSR_Dummy_Component() {}`,\n )(),\n ])\n }\n }\n },\n },\n state,\n )\n\n eliminateUnreferencedIdentifiers(programPath)\n\n /**\n * If the component for the route is being imported,\n * and it's not being used, remove the import statement\n * from the program, by checking that the import has no\n * specifiers\n */\n if (\n (existingCompImportPath as string | null) ||\n (existingLoaderImportPath as string | null)\n ) {\n programPath.traverse({\n ImportDeclaration(path) {\n if (path.node.specifiers.length > 0) return\n if (\n path.node.source.value === existingCompImportPath ||\n path.node.source.value === existingLoaderImportPath\n ) {\n path.remove()\n }\n },\n })\n }\n },\n },\n },\n },\n {\n root: process.cwd(),\n minify: process.env.NODE_ENV === 'production',\n },\n ],\n ].filter(Boolean),\n }),\n })\n}\n\nfunction getImportSpecifierAndPathFromLocalName(\n programPath: babel.NodePath<t.Program>,\n name: string,\n): {\n specifier:\n | t.ImportSpecifier\n | t.ImportDefaultSpecifier\n | t.ImportNamespaceSpecifier\n | null\n path: string | null\n} {\n let specifier:\n | t.ImportSpecifier\n | t.ImportDefaultSpecifier\n | t.ImportNamespaceSpecifier\n | null = null\n let path: string | null = null\n\n programPath.traverse({\n ImportDeclaration(importPath) {\n const found = importPath.node.specifiers.find(\n (targetSpecifier) => targetSpecifier.local.name === name,\n )\n if (found) {\n specifier = found\n path = importPath.node.source.value\n }\n },\n })\n\n return { specifier, path }\n}\n\n// Reusable function to get literal value or resolve variable to literal\nfunction resolveIdentifier(path: any, node: any) {\n if (t.isIdentifier(node)) {\n const binding = path.scope.getBinding(node.name)\n if (\n binding\n // && binding.kind === 'const'\n ) {\n const declarator = binding.path.node\n if (t.isObjectExpression(declarator.init)) {\n return declarator.init\n } else if (t.isFunctionDeclaration(declarator.init)) {\n return declarator.init\n }\n }\n return undefined\n }\n\n return node\n}\n\nfunction removeIdentifierLiteral(path: any, node: any) {\n if (t.isIdentifier(node)) {\n const binding = path.scope.getBinding(node.name)\n if (binding) {\n binding.path.remove()\n }\n }\n}\n\nconst splitNodeTypes = ['component', 'loader'] as const\ntype SplitNodeType = (typeof splitNodeTypes)[number]\n\nexport async function splitFile(opts: {\n code: string\n compileAst: CompileAstFn\n filename: string\n}) {\n return await opts.compileAst({\n code: opts.code,\n filename: opts.filename,\n getBabelConfig: () => ({\n plugins: [\n [\n {\n visitor: {\n Program: {\n enter(programPath: babel.NodePath<t.Program>, state: State) {\n const splitNodesByType: Record<\n SplitNodeType,\n t.Node | undefined\n > = {\n component: undefined,\n loader: undefined,\n }\n\n // Find the node\n programPath.traverse(\n {\n CallExpression: (path) => {\n if (!t.isIdentifier(path.node.callee)) {\n return\n }\n\n if (\n !(\n path.node.callee.name === 'createRoute' ||\n path.node.callee.name === 'createFileRoute'\n )\n ) {\n return\n }\n\n if (t.isCallExpression(path.parentPath.node)) {\n const options = resolveIdentifier(\n path,\n path.parentPath.node.arguments[0],\n )\n\n if (t.isObjectExpression(options)) {\n options.properties.forEach((prop) => {\n if (t.isObjectProperty(prop)) {\n splitNodeTypes.forEach((type) => {\n if (t.isIdentifier(prop.key)) {\n if (prop.key.name === type) {\n splitNodesByType[type] = prop.value\n }\n }\n })\n }\n })\n\n // Remove all of the options\n options.properties = []\n }\n }\n },\n },\n state,\n )\n\n splitNodeTypes.forEach((splitType) => {\n let splitNode = splitNodesByType[splitType]\n\n if (!splitNode) {\n return\n }\n\n while (t.isIdentifier(splitNode)) {\n const binding = programPath.scope.getBinding(\n splitNode.name,\n )\n splitNode = binding?.path.node\n }\n\n // Add the node to the program\n if (splitNode) {\n if (t.isFunctionDeclaration(splitNode)) {\n programPath.pushContainer(\n 'body',\n t.variableDeclaration('const', [\n t.variableDeclarator(\n t.identifier(splitType),\n t.functionExpression(\n splitNode.id || null, // Anonymize the function expression\n splitNode.params,\n splitNode.body,\n splitNode.generator,\n splitNode.async,\n ),\n ),\n ]),\n )\n } else if (\n t.isFunctionExpression(splitNode) ||\n t.isArrowFunctionExpression(splitNode)\n ) {\n programPath.pushContainer(\n 'body',\n t.variableDeclaration('const', [\n t.variableDeclarator(\n t.identifier(splitType),\n splitNode as any,\n ),\n ]),\n )\n } else if (\n t.isImportSpecifier(splitNode) ||\n t.isImportDefaultSpecifier(splitNode)\n ) {\n programPath.pushContainer(\n 'body',\n t.variableDeclaration('const', [\n t.variableDeclarator(\n t.identifier(splitType),\n splitNode.local,\n ),\n ]),\n )\n } else {\n console.info(splitNode)\n throw new Error(\n `Unexpected splitNode type ☝️: ${splitNode.type}`,\n )\n }\n }\n\n // If the splitNode exists at the top of the program\n // then we need to remove that copy\n programPath.node.body = programPath.node.body.filter(\n (node) => {\n return node !== splitNode\n },\n )\n\n // Export the node\n programPath.pushContainer('body', [\n t.exportNamedDeclaration(null, [\n t.exportSpecifier(\n t.identifier(splitType),\n t.identifier(splitType),\n ),\n ]),\n ])\n })\n\n // convert exports to imports from the original file\n programPath.traverse({\n ExportNamedDeclaration(path) {\n // e.g. export const x = 1 or export { x }\n // becomes\n // import { x } from '${opts.id}'\n\n if (path.node.declaration) {\n if (t.isVariableDeclaration(path.node.declaration)) {\n path.replaceWith(\n t.importDeclaration(\n path.node.declaration.declarations.map((decl) =>\n t.importSpecifier(\n t.identifier((decl.id as any).name),\n t.identifier((decl.id as any).name),\n ),\n ),\n t.stringLiteral(\n opts.filename.split(\n `?${splitPrefix}`,\n )[0] as string,\n ),\n ),\n )\n }\n }\n },\n })\n\n eliminateUnreferencedIdentifiers(programPath)\n },\n },\n },\n },\n {\n root: process.cwd(),\n minify: process.env.NODE_ENV === 'production',\n },\n ],\n ].filter(Boolean),\n }),\n })\n}\n"],"names":[],"mappings":";;;;AAyBA,eAAsB,YAAY,MAI/B;AACM,SAAA,MAAM,KAAK,WAAW;AAAA,IAC3B,MAAM,KAAK;AAAA,IACX,UAAU,KAAK;AAAA,IACf,gBAAgB,OAAO;AAAA,MACrB,SAAS;AAAA,QACP;AAAA,UACE;AAAA,YACE,SAAS;AAAA,cACP,SAAS;AAAA,gBACP,MAAM,aAAwC,OAAc;AAC1D,wBAAM,WAAW,GAAG,WAAW,IAAI,KAAK,QAAQ,IAAI,WAAW;AAW/D,sBAAI,yBAAwC;AAC5C,sBAAI,2BAA0C;AAElC,8BAAA;AAAA,oBACV;AAAA,sBACE,gBAAgB,CAAC,SAAS;AACxB,4BAAI,CAAC,EAAE,aAAa,KAAK,KAAK,MAAM,GAAG;AACrC;AAAA,wBACF;AAGE,4BAAA,EACE,KAAK,KAAK,OAAO,SAAS,iBAC1B,KAAK,KAAK,OAAO,SAAS,oBAE5B;AACA;AAAA,wBACF;AAEA,4BAAI,EAAE,iBAAiB,KAAK,WAAW,IAAI,GAAG;AAC5C,gCAAM,UAAU;AAAA,4BACd;AAAA,4BACA,KAAK,WAAW,KAAK,UAAU,CAAC;AAAA,0BAAA;AAGlC,8BAAI,QAAQ;AAEN,gCAAA,iCAAiC,CACrC,SACG;AACI,mCAAA,YAAY,MAAM,WAAW,IAAI;AAAA,0BAAA;AAGtC,8BAAA,EAAE,mBAAmB,OAAO,GAAG;AACzB,oCAAA,WAAW,QAAQ,CAAC,SAAS;AAC/B,kCAAA,EAAE,iBAAiB,IAAI,GAAG;AAC5B,oCAAI,EAAE,aAAa,KAAK,GAAG,GAAG;AACxB,sCAAA,KAA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F;AAAA,UACF;AAAA,UACA;AAAA,YACE,MAAM,QAAQ,IAAI;AAAA,YAClB,QAAQ,QAAQ,IAAI,aAAa;AAAA,UACnC;AAAA,QACF;AAAA,MAAA,EACA,OAAO,OAAO;AAAA,IAAA;AAAA,EAClB,CACD;AACH;"}
|
package/package.json
CHANGED
package/src/compilers.ts
CHANGED
|
@@ -40,134 +40,160 @@ export async function compileFile(opts: {
|
|
|
40
40
|
enter(programPath: babel.NodePath<t.Program>, state: State) {
|
|
41
41
|
const splitUrl = `${splitPrefix}:${opts.filename}?${splitPrefix}`
|
|
42
42
|
|
|
43
|
+
/**
|
|
44
|
+
* If the component for the route is being imported from
|
|
45
|
+
* another file, this is to track the path to that file
|
|
46
|
+
* the path itself doesn't matter, we just need to keep
|
|
47
|
+
* track of it so that we can remove it from the imports
|
|
48
|
+
* list if it's not being used like:
|
|
49
|
+
*
|
|
50
|
+
* `import '../shared/imported'`
|
|
51
|
+
*/
|
|
52
|
+
let existingCompImportPath: string | null = null
|
|
53
|
+
let existingLoaderImportPath: string | null = null
|
|
54
|
+
|
|
43
55
|
programPath.traverse(
|
|
44
56
|
{
|
|
45
57
|
CallExpression: (path) => {
|
|
46
|
-
if (path.node.callee
|
|
47
|
-
|
|
58
|
+
if (!t.isIdentifier(path.node.callee)) {
|
|
59
|
+
return
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (
|
|
63
|
+
!(
|
|
48
64
|
path.node.callee.name === 'createRoute' ||
|
|
49
65
|
path.node.callee.name === 'createFileRoute'
|
|
50
|
-
)
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
+
)
|
|
67
|
+
) {
|
|
68
|
+
return
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (t.isCallExpression(path.parentPath.node)) {
|
|
72
|
+
const options = resolveIdentifier(
|
|
73
|
+
path,
|
|
74
|
+
path.parentPath.node.arguments[0],
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
let found = false
|
|
78
|
+
|
|
79
|
+
const hasImportedOrDefinedIdentifier = (
|
|
80
|
+
name: string,
|
|
81
|
+
) => {
|
|
82
|
+
return programPath.scope.hasBinding(name)
|
|
83
|
+
}
|
|
66
84
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
if (
|
|
83
|
-
!hasImportedOrDefinedIdentifier(
|
|
84
|
-
'lazyRouteComponent',
|
|
85
|
-
)
|
|
86
|
-
) {
|
|
87
|
-
programPath.unshiftContainer('body', [
|
|
88
|
-
template.smart(
|
|
89
|
-
`import { lazyRouteComponent } from '@tanstack/react-router'`,
|
|
90
|
-
)() as t.Statement,
|
|
91
|
-
])
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
if (
|
|
95
|
-
!hasImportedOrDefinedIdentifier(
|
|
96
|
-
'$$splitComponentImporter',
|
|
97
|
-
)
|
|
98
|
-
) {
|
|
99
|
-
programPath.unshiftContainer('body', [
|
|
100
|
-
template.smart(
|
|
101
|
-
`const $$splitComponentImporter = () => import('${splitUrl}')`,
|
|
102
|
-
)() as t.Statement,
|
|
103
|
-
])
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
prop.value = template.expression(
|
|
107
|
-
`lazyRouteComponent($$splitComponentImporter, 'component')`,
|
|
108
|
-
)() as any
|
|
109
|
-
|
|
110
|
-
programPath.pushContainer('body', [
|
|
111
|
-
template.smart(
|
|
112
|
-
`function DummyComponent() { return null }`,
|
|
113
|
-
)() as t.Statement,
|
|
114
|
-
])
|
|
115
|
-
|
|
116
|
-
found = true
|
|
117
|
-
} else if (prop.key.name === 'loader') {
|
|
118
|
-
const value = prop.value
|
|
119
|
-
|
|
120
|
-
if (t.isIdentifier(value)) {
|
|
121
|
-
removeIdentifierLiteral(path, value)
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// Prepend the import statement to the program along with the importer function
|
|
125
|
-
|
|
126
|
-
if (
|
|
127
|
-
!hasImportedOrDefinedIdentifier(
|
|
128
|
-
'lazyFn',
|
|
129
|
-
)
|
|
130
|
-
) {
|
|
131
|
-
programPath.unshiftContainer('body', [
|
|
132
|
-
template.smart(
|
|
133
|
-
`import { lazyFn } from '@tanstack/react-router'`,
|
|
134
|
-
)() as t.Statement,
|
|
135
|
-
])
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
if (
|
|
139
|
-
!hasImportedOrDefinedIdentifier(
|
|
140
|
-
'$$splitLoaderImporter',
|
|
141
|
-
)
|
|
142
|
-
) {
|
|
143
|
-
programPath.unshiftContainer('body', [
|
|
144
|
-
template.smart(
|
|
145
|
-
`const $$splitLoaderImporter = () => import('${splitUrl}')`,
|
|
146
|
-
)() as t.Statement,
|
|
147
|
-
])
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
prop.value = template.expression(
|
|
151
|
-
`lazyFn($$splitLoaderImporter, 'loader')`,
|
|
152
|
-
)() as any
|
|
153
|
-
|
|
154
|
-
found = true
|
|
155
|
-
}
|
|
85
|
+
if (t.isObjectExpression(options)) {
|
|
86
|
+
options.properties.forEach((prop) => {
|
|
87
|
+
if (t.isObjectProperty(prop)) {
|
|
88
|
+
if (t.isIdentifier(prop.key)) {
|
|
89
|
+
if (prop.key.name === 'component') {
|
|
90
|
+
const value = prop.value
|
|
91
|
+
|
|
92
|
+
if (t.isIdentifier(value)) {
|
|
93
|
+
existingCompImportPath =
|
|
94
|
+
getImportSpecifierAndPathFromLocalName(
|
|
95
|
+
programPath,
|
|
96
|
+
value.name,
|
|
97
|
+
).path
|
|
98
|
+
|
|
99
|
+
removeIdentifierLiteral(path, value)
|
|
156
100
|
}
|
|
157
|
-
}
|
|
158
101
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
102
|
+
// Prepend the import statement to the program along with the importer function
|
|
103
|
+
// Check to see if lazyRouteComponent is already imported before attempting
|
|
104
|
+
// to import it again
|
|
105
|
+
|
|
106
|
+
if (
|
|
107
|
+
!hasImportedOrDefinedIdentifier(
|
|
108
|
+
'lazyRouteComponent',
|
|
109
|
+
)
|
|
110
|
+
) {
|
|
111
|
+
programPath.unshiftContainer('body', [
|
|
112
|
+
template.statement(
|
|
113
|
+
`import { lazyRouteComponent } from '@tanstack/react-router'`,
|
|
114
|
+
)(),
|
|
115
|
+
])
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (
|
|
119
|
+
!hasImportedOrDefinedIdentifier(
|
|
120
|
+
'$$splitComponentImporter',
|
|
121
|
+
)
|
|
122
|
+
) {
|
|
123
|
+
programPath.unshiftContainer('body', [
|
|
124
|
+
template.statement(
|
|
125
|
+
`const $$splitComponentImporter = () => import('${splitUrl}')`,
|
|
126
|
+
)(),
|
|
127
|
+
])
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
prop.value = template.expression(
|
|
131
|
+
`lazyRouteComponent($$splitComponentImporter, 'component')`,
|
|
132
|
+
)()
|
|
133
|
+
|
|
134
|
+
programPath.pushContainer('body', [
|
|
135
|
+
template.statement(
|
|
136
|
+
`function DummyComponent() { return null }`,
|
|
137
|
+
)(),
|
|
138
|
+
])
|
|
139
|
+
|
|
140
|
+
found = true
|
|
141
|
+
} else if (prop.key.name === 'loader') {
|
|
142
|
+
const value = prop.value
|
|
143
|
+
|
|
144
|
+
if (t.isIdentifier(value)) {
|
|
145
|
+
existingLoaderImportPath =
|
|
146
|
+
getImportSpecifierAndPathFromLocalName(
|
|
147
|
+
programPath,
|
|
148
|
+
value.name,
|
|
149
|
+
).path
|
|
150
|
+
|
|
151
|
+
removeIdentifierLiteral(path, value)
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Prepend the import statement to the program along with the importer function
|
|
155
|
+
|
|
156
|
+
if (
|
|
157
|
+
!hasImportedOrDefinedIdentifier('lazyFn')
|
|
158
|
+
) {
|
|
159
|
+
programPath.unshiftContainer('body', [
|
|
160
|
+
template.smart(
|
|
161
|
+
`import { lazyFn } from '@tanstack/react-router'`,
|
|
162
|
+
)() as t.Statement,
|
|
163
|
+
])
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (
|
|
167
|
+
!hasImportedOrDefinedIdentifier(
|
|
168
|
+
'$$splitLoaderImporter',
|
|
169
|
+
)
|
|
170
|
+
) {
|
|
171
|
+
programPath.unshiftContainer('body', [
|
|
172
|
+
template.statement(
|
|
173
|
+
`const $$splitLoaderImporter = () => import('${splitUrl}')`,
|
|
174
|
+
)(),
|
|
175
|
+
])
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
prop.value = template.expression(
|
|
179
|
+
`lazyFn($$splitLoaderImporter, 'loader')`,
|
|
180
|
+
)()
|
|
162
181
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
`function TSR_Dummy_Component() {}`,
|
|
167
|
-
)() as t.Statement,
|
|
168
|
-
])
|
|
182
|
+
found = true
|
|
183
|
+
}
|
|
184
|
+
}
|
|
169
185
|
}
|
|
170
|
-
|
|
186
|
+
|
|
187
|
+
programPath.scope.crawl()
|
|
188
|
+
})
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (found as boolean) {
|
|
192
|
+
programPath.pushContainer('body', [
|
|
193
|
+
template.statement(
|
|
194
|
+
`function TSR_Dummy_Component() {}`,
|
|
195
|
+
)(),
|
|
196
|
+
])
|
|
171
197
|
}
|
|
172
198
|
}
|
|
173
199
|
},
|
|
@@ -176,6 +202,29 @@ export async function compileFile(opts: {
|
|
|
176
202
|
)
|
|
177
203
|
|
|
178
204
|
eliminateUnreferencedIdentifiers(programPath)
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* If the component for the route is being imported,
|
|
208
|
+
* and it's not being used, remove the import statement
|
|
209
|
+
* from the program, by checking that the import has no
|
|
210
|
+
* specifiers
|
|
211
|
+
*/
|
|
212
|
+
if (
|
|
213
|
+
(existingCompImportPath as string | null) ||
|
|
214
|
+
(existingLoaderImportPath as string | null)
|
|
215
|
+
) {
|
|
216
|
+
programPath.traverse({
|
|
217
|
+
ImportDeclaration(path) {
|
|
218
|
+
if (path.node.specifiers.length > 0) return
|
|
219
|
+
if (
|
|
220
|
+
path.node.source.value === existingCompImportPath ||
|
|
221
|
+
path.node.source.value === existingLoaderImportPath
|
|
222
|
+
) {
|
|
223
|
+
path.remove()
|
|
224
|
+
}
|
|
225
|
+
},
|
|
226
|
+
})
|
|
227
|
+
}
|
|
179
228
|
},
|
|
180
229
|
},
|
|
181
230
|
},
|
|
@@ -190,6 +239,39 @@ export async function compileFile(opts: {
|
|
|
190
239
|
})
|
|
191
240
|
}
|
|
192
241
|
|
|
242
|
+
function getImportSpecifierAndPathFromLocalName(
|
|
243
|
+
programPath: babel.NodePath<t.Program>,
|
|
244
|
+
name: string,
|
|
245
|
+
): {
|
|
246
|
+
specifier:
|
|
247
|
+
| t.ImportSpecifier
|
|
248
|
+
| t.ImportDefaultSpecifier
|
|
249
|
+
| t.ImportNamespaceSpecifier
|
|
250
|
+
| null
|
|
251
|
+
path: string | null
|
|
252
|
+
} {
|
|
253
|
+
let specifier:
|
|
254
|
+
| t.ImportSpecifier
|
|
255
|
+
| t.ImportDefaultSpecifier
|
|
256
|
+
| t.ImportNamespaceSpecifier
|
|
257
|
+
| null = null
|
|
258
|
+
let path: string | null = null
|
|
259
|
+
|
|
260
|
+
programPath.traverse({
|
|
261
|
+
ImportDeclaration(importPath) {
|
|
262
|
+
const found = importPath.node.specifiers.find(
|
|
263
|
+
(targetSpecifier) => targetSpecifier.local.name === name,
|
|
264
|
+
)
|
|
265
|
+
if (found) {
|
|
266
|
+
specifier = found
|
|
267
|
+
path = importPath.node.source.value
|
|
268
|
+
}
|
|
269
|
+
},
|
|
270
|
+
})
|
|
271
|
+
|
|
272
|
+
return { specifier, path }
|
|
273
|
+
}
|
|
274
|
+
|
|
193
275
|
// Reusable function to get literal value or resolve variable to literal
|
|
194
276
|
function resolveIdentifier(path: any, node: any) {
|
|
195
277
|
if (t.isIdentifier(node)) {
|
|
@@ -250,36 +332,40 @@ export async function splitFile(opts: {
|
|
|
250
332
|
programPath.traverse(
|
|
251
333
|
{
|
|
252
334
|
CallExpression: (path) => {
|
|
253
|
-
if (path.node.callee
|
|
254
|
-
|
|
335
|
+
if (!t.isIdentifier(path.node.callee)) {
|
|
336
|
+
return
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (
|
|
340
|
+
!(
|
|
255
341
|
path.node.callee.name === 'createRoute' ||
|
|
256
342
|
path.node.callee.name === 'createFileRoute'
|
|
257
|
-
)
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
}
|
|
343
|
+
)
|
|
344
|
+
) {
|
|
345
|
+
return
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
if (t.isCallExpression(path.parentPath.node)) {
|
|
349
|
+
const options = resolveIdentifier(
|
|
350
|
+
path,
|
|
351
|
+
path.parentPath.node.arguments[0],
|
|
352
|
+
)
|
|
353
|
+
|
|
354
|
+
if (t.isObjectExpression(options)) {
|
|
355
|
+
options.properties.forEach((prop) => {
|
|
356
|
+
if (t.isObjectProperty(prop)) {
|
|
357
|
+
splitNodeTypes.forEach((type) => {
|
|
358
|
+
if (t.isIdentifier(prop.key)) {
|
|
359
|
+
if (prop.key.name === type) {
|
|
360
|
+
splitNodesByType[type] = prop.value
|
|
361
|
+
}
|
|
276
362
|
}
|
|
277
363
|
})
|
|
278
|
-
|
|
279
|
-
// Remove all of the options
|
|
280
|
-
options.properties = []
|
|
281
364
|
}
|
|
282
|
-
}
|
|
365
|
+
})
|
|
366
|
+
|
|
367
|
+
// Remove all of the options
|
|
368
|
+
options.properties = []
|
|
283
369
|
}
|
|
284
370
|
}
|
|
285
371
|
},
|
|
@@ -332,7 +418,10 @@ export async function splitFile(opts: {
|
|
|
332
418
|
),
|
|
333
419
|
]),
|
|
334
420
|
)
|
|
335
|
-
} else if (
|
|
421
|
+
} else if (
|
|
422
|
+
t.isImportSpecifier(splitNode) ||
|
|
423
|
+
t.isImportDefaultSpecifier(splitNode)
|
|
424
|
+
) {
|
|
336
425
|
programPath.pushContainer(
|
|
337
426
|
'body',
|
|
338
427
|
t.variableDeclaration('const', [
|