Skip to content

Commit c7cfa35

Browse files
fix: only run middleware compiler in client env
1 parent 6a62499 commit c7cfa35

1 file changed

Lines changed: 27 additions & 12 deletions

File tree

  • packages/start-plugin-core/src/start-compiler-plugin

‎packages/start-plugin-core/src/start-compiler-plugin/compilers.ts‎

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function compileStartOutputFactory(
2323
framework: CompileStartFrameworkOptions,
2424
) {
2525
return function compileStartOutput(opts: CompileOptions): GeneratorResult {
26-
const identifiers: Identifiers = {
26+
const identifiers: Partial<Identifiers> = {
2727
createServerOnlyFn: {
2828
name: 'createServerOnlyFn',
2929
handleCallExpression: handleCreateServerOnlyFnCallExpression,
@@ -39,11 +39,16 @@ export function compileStartOutputFactory(
3939
handleCallExpression: handleCreateIsomorphicFnCallExpression,
4040
paths: [],
4141
},
42-
createMiddleware: {
42+
}
43+
44+
// createMiddleware only performs modifications in the client environment
45+
// so we can avoid executing this on the server
46+
if (opts.env === 'client') {
47+
identifiers.createMiddleware = {
4348
name: 'createMiddleware',
4449
handleCallExpression: handleCreateMiddleware,
4550
paths: [],
46-
},
51+
}
4752
}
4853

4954
const ast = parseAst(opts)
@@ -65,7 +70,9 @@ export function compileStartOutputFactory(
6570
path.node.specifiers.forEach((specifier) => {
6671
transformFuncs.forEach((identifierKey) => {
6772
const identifier = identifiers[identifierKey]
68-
73+
if (!identifier) {
74+
return
75+
}
6976
if (
7077
specifier.type === 'ImportSpecifier' &&
7178
specifier.imported.type === 'Identifier'
@@ -84,25 +91,29 @@ export function compileStartOutputFactory(
8491
},
8592
CallExpression: (path) => {
8693
transformFuncs.forEach((identifierKey) => {
94+
const identifier = identifiers[identifierKey]
95+
if (!identifier) {
96+
return
97+
}
8798
// Check to see if the call expression is a call to the
8899
// identifiers[identifierKey].name
89100
if (
90101
t.isIdentifier(path.node.callee) &&
91-
path.node.callee.name === identifiers[identifierKey].name
102+
path.node.callee.name === identifier.name
92103
) {
93104
// The identifier could be a call to the original function
94105
// in the source code. If this is case, we need to ignore it.
95106
// Check the scope to see if the identifier is a function declaration.
96107
// if it is, then we can ignore it.
97108

98109
if (
99-
path.scope.getBinding(identifiers[identifierKey].name)?.path
100-
.node.type === 'FunctionDeclaration'
110+
path.scope.getBinding(identifier.name)?.path.node.type ===
111+
'FunctionDeclaration'
101112
) {
102113
return
103114
}
104115

105-
return identifiers[identifierKey].paths.push(path)
116+
return identifier.paths.push(path)
106117
}
107118

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

120-
if (callname === identifiers[identifierKey].name) {
121-
identifiers[identifierKey].paths.push(path)
131+
if (callname === identifier.name) {
132+
identifier.paths.push(path)
122133
}
123134
}
124135
}
@@ -129,8 +140,12 @@ export function compileStartOutputFactory(
129140
})
130141

131142
transformFuncs.forEach((identifierKey) => {
132-
identifiers[identifierKey].paths.forEach((path) => {
133-
identifiers[identifierKey].handleCallExpression(
143+
const identifier = identifiers[identifierKey]
144+
if (!identifier) {
145+
return
146+
}
147+
identifier.paths.forEach((path) => {
148+
identifier.handleCallExpression(
134149
path as babel.NodePath<t.CallExpression>,
135150
opts,
136151
)

0 commit comments

Comments
 (0)