diff --git a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts index 163097c3c9ff18..5d0cb5201d45c6 100644 --- a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts +++ b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts @@ -224,7 +224,8 @@ test('overwrite bindings', async () => { `function c() { const { test: inject } = { test: true }; console.log(inject) }\n` + `const d = inject \n` + `function f() { console.log(inject) }\n` + - `function e() { const { inject } = { inject: true } }\n`, + `function e() { const { inject } = { inject: true } }\n` + + `function g() { const f = () => { const inject = true }; console.log(inject) }\n`, null, null ) @@ -237,6 +238,7 @@ test('overwrite bindings', async () => { const d = __vite_ssr_import_0__.inject function f() { console.log(__vite_ssr_import_0__.inject) } function e() { const { inject } = { inject: true } } + function g() { const f = () => { const inject = true }; console.log(__vite_ssr_import_0__.inject) } " `) }) diff --git a/packages/vite/src/node/ssr/ssrTransform.ts b/packages/vite/src/node/ssr/ssrTransform.ts index 0e838f1932f8c6..af194787c84b15 100644 --- a/packages/vite/src/node/ssr/ssrTransform.ts +++ b/packages/vite/src/node/ssr/ssrTransform.ts @@ -392,7 +392,8 @@ function isFunction(node: _Node): node is FunctionNode { } function findParentFunction(parentStack: _Node[]): FunctionNode | undefined { - for (const node of parentStack) { + for (let i = parentStack.length - 1; i >= 0; i--) { + const node = parentStack[i] if (isFunction(node)) { return node }