Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ssr): skip destructure assignment at overwrite import bindings #2417

Merged
merged 2 commits into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
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
27 changes: 27 additions & 0 deletions packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,30 @@ test('sourcemap source', async () => {
(await ssrTransform(`export const a = 1`, null, 'input.js')).map.sources
).toStrictEqual(['input.js'])
})

test('overwrite bindings', async () => {
expect(
(
await ssrTransform(
`import { inject } from 'vue';` +
`const a = { inject }\n` +
`const b = { test: inject }\n` +
`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`,
null,
null
)
).code
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = __vite_ssr_import__(\\"vue\\")
const a = { inject: __vite_ssr_import_0__.inject }
const b = { test: __vite_ssr_import_0__.inject }
function c() { const { test: inject } = { test: true }; console.log(inject) }
const d = __vite_ssr_import_0__.inject
function f() { console.log(__vite_ssr_import_0__.inject) }
function e() { const { inject } = { inject: true } }
"
`)
})
58 changes: 42 additions & 16 deletions packages/vite/src/node/ssr/ssrTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,23 @@ function walk(
const scope: Record<string, number> = Object.create(null)
const scopeMap = new WeakMap<_Node, Set<string>>()

const setScope = (node: FunctionNode, name: string) => {
let scopeIds = scopeMap.get(node)
if (scopeIds && scopeIds.has(name)) {
return
}
if (name in scope) {
scope[name]++
} else {
scope[name] = 1
}
if (!scopeIds) {
scopeIds = new Set()
scopeMap.set(node, scopeIds)
}
scopeIds.add(name)
}

;(eswalk as any)(root, {
enter(node: Node, parent: Node | null) {
if (node.type === 'ImportDeclaration') {
Expand Down Expand Up @@ -257,28 +274,29 @@ function walk(
parent.right === child
)
) {
const { name } = child
let scopeIds = scopeMap.get(node)
if (scopeIds && scopeIds.has(name)) {
return
}
if (name in scope) {
scope[name]++
} else {
scope[name] = 1
}
if (!scopeIds) {
scopeIds = new Set()
scopeMap.set(node, scopeIds)
}
scopeIds.add(name)
setScope(node, child.name)
}
}
})
)
} else if (node.type === 'Property' && parent!.type === 'ObjectPattern') {
// mark property in destructure pattern
;(node as any).inPattern = true
} else if (node.type === 'VariableDeclarator') {
const parentFunction = findParentFunction(parentStack)
if (parentFunction) {
if (node.id.type === 'ObjectPattern') {
node.id.properties.forEach((property) => {
if (property.type === 'RestElement') {
setScope(parentFunction, (property.argument as Identifier).name)
} else {
setScope(parentFunction, (property.value as Identifier).name)
}
})
} else {
setScope(parentFunction, (node.id as Identifier).name)
}
}
}
},

Expand Down Expand Up @@ -325,7 +343,7 @@ function isRefIdentifier(id: Identifier, parent: _Node, parentStack: _Node[]) {

// property key
// this also covers object destructure pattern
if (isStaticPropertyKey(id, parent)) {
if (isStaticPropertyKey(id, parent) || (parent as any).inPattern) {
return false
}

Expand Down Expand Up @@ -368,6 +386,14 @@ function isFunction(node: _Node): node is FunctionNode {
return /Function(?:Expression|Declaration)$|Method$/.test(node.type)
}

function findParentFunction(parentStack: _Node[]): FunctionNode | undefined {
for (const node of parentStack) {
if (isFunction(node)) {
return node
}
}
}

function isInDestructureAssignment(
parent: _Node,
parentStack: _Node[]
Expand Down