Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions packages/start-plugin-core/src/start-compiler-plugin/compilers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function compileStartOutputFactory(
framework: CompileStartFrameworkOptions,
) {
return function compileStartOutput(opts: CompileOptions): GeneratorResult {
const identifiers: Identifiers = {
const identifiers: Partial<Identifiers> = {
createServerOnlyFn: {
name: 'createServerOnlyFn',
handleCallExpression: handleCreateServerOnlyFnCallExpression,
Expand All @@ -39,11 +39,16 @@ export function compileStartOutputFactory(
handleCallExpression: handleCreateIsomorphicFnCallExpression,
paths: [],
},
createMiddleware: {
}

// createMiddleware only performs modifications in the client environment
// so we can avoid executing this on the server
if (opts.env === 'client') {
identifiers.createMiddleware = {
name: 'createMiddleware',
handleCallExpression: handleCreateMiddleware,
paths: [],
},
}
}

const ast = parseAst(opts)
Expand All @@ -65,7 +70,9 @@ export function compileStartOutputFactory(
path.node.specifiers.forEach((specifier) => {
transformFuncs.forEach((identifierKey) => {
const identifier = identifiers[identifierKey]

if (!identifier) {
return
}
if (
specifier.type === 'ImportSpecifier' &&
specifier.imported.type === 'Identifier'
Expand All @@ -84,25 +91,29 @@ export function compileStartOutputFactory(
},
CallExpression: (path) => {
transformFuncs.forEach((identifierKey) => {
const identifier = identifiers[identifierKey]
if (!identifier) {
return
}
// Check to see if the call expression is a call to the
// identifiers[identifierKey].name
if (
t.isIdentifier(path.node.callee) &&
path.node.callee.name === identifiers[identifierKey].name
path.node.callee.name === identifier.name
) {
// The identifier could be a call to the original function
// in the source code. If this is case, we need to ignore it.
// Check the scope to see if the identifier is a function declaration.
// if it is, then we can ignore it.

if (
path.scope.getBinding(identifiers[identifierKey].name)?.path
.node.type === 'FunctionDeclaration'
path.scope.getBinding(identifier.name)?.path.node.type ===
'FunctionDeclaration'
) {
return
}

return identifiers[identifierKey].paths.push(path)
return identifier.paths.push(path)
}

// handle namespace imports like "import * as TanStackStart from '@tanstack/react-start';"
Expand All @@ -117,8 +128,8 @@ export function compileStartOutputFactory(
path.node.callee.property.name,
].join('.')

if (callname === identifiers[identifierKey].name) {
identifiers[identifierKey].paths.push(path)
if (callname === identifier.name) {
identifier.paths.push(path)
}
}
}
Expand All @@ -129,8 +140,12 @@ export function compileStartOutputFactory(
})

transformFuncs.forEach((identifierKey) => {
identifiers[identifierKey].paths.forEach((path) => {
identifiers[identifierKey].handleCallExpression(
const identifier = identifiers[identifierKey]
if (!identifier) {
return
}
identifier.paths.forEach((path) => {
identifier.handleCallExpression(
path as babel.NodePath<t.CallExpression>,
opts,
)
Expand Down
Loading