diff --git a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts index a4e74390d3d3c2..e70a3e30c948cc 100644 --- a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts +++ b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts @@ -400,6 +400,30 @@ const a = () => { } " `) + + // #9585 + expect( + await ssrTransformSimpleCode( + ` +import { n, m } from 'foo' +const foo = {} + +{ + const { [n]: m } = foo +} +` + ) + ).toMatchInlineSnapshot(` + " + const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\"); + + const foo = {} + + { + const { [__vite_ssr_import_0__.n]: m } = foo + } + " + `) }) test('nested object destructure alias', async () => { @@ -463,6 +487,45 @@ objRest() `) }) +test('object props and methods', async () => { + expect( + await ssrTransformSimpleCode( + ` +import foo from 'foo' + +const bar = 'bar' + +const obj = { + foo() {}, + [foo]() {}, + [bar]() {}, + foo: () => {}, + [foo]: () => {}, + [bar]: () => {}, + bar(foo) {} +} +` + ) + ).toMatchInlineSnapshot(` + " + const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\"); + + + const bar = 'bar' + + const obj = { + foo() {}, + [__vite_ssr_import_0__.default]() {}, + [bar]() {}, + foo: () => {}, + [__vite_ssr_import_0__.default]: () => {}, + [bar]: () => {}, + bar(foo) {} + } + " + `) +}) + test('class props', async () => { expect( await ssrTransformSimpleCode( diff --git a/packages/vite/src/node/ssr/ssrTransform.ts b/packages/vite/src/node/ssr/ssrTransform.ts index bf1633395ae992..0798d674547fb3 100644 --- a/packages/vite/src/node/ssr/ssrTransform.ts +++ b/packages/vite/src/node/ssr/ssrTransform.ts @@ -230,7 +230,7 @@ async function ssrTransformScript( // { foo } -> { foo: __import_x__.foo } // skip for destructuring patterns if ( - !isNodeInPatternWeakMap.get(parent) || + !isNodeInPattern(parent) || isInDestructuringAssignment(parent, parentStack) ) { s.appendLeft(id.end, `: ${binding}`) @@ -305,7 +305,10 @@ interface Visitors { onDynamicImport: (node: Node) => void } -const isNodeInPatternWeakMap = new WeakMap<_Node, boolean>() +const isNodeInPatternWeakSet = new WeakSet<_Node>() +const setIsNodeInPattern = (node: Property) => isNodeInPatternWeakSet.add(node) +const isNodeInPattern = (node: _Node): node is Property => + isNodeInPatternWeakSet.has(node) /** * Same logic from \@vue/compiler-core & \@vue/compiler-sfc @@ -425,7 +428,7 @@ function walk( }) } else if (node.type === 'Property' && parent!.type === 'ObjectPattern') { // mark property in destructuring pattern - isNodeInPatternWeakMap.set(node, true) + setIsNodeInPattern(node) } else if (node.type === 'VariableDeclarator') { const parentFunction = findParentFunction(parentStack) if (parentFunction) { @@ -474,8 +477,12 @@ function isRefIdentifier(id: Identifier, parent: _Node, parentStack: _Node[]) { } // property key - // this also covers object destructuring pattern - if (isStaticPropertyKey(id, parent) || isNodeInPatternWeakMap.get(parent)) { + if (isStaticPropertyKey(id, parent)) { + return false + } + + // object destructuring pattern + if (isNodeInPattern(parent) && parent.value === id) { return false }