You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ESM Modules: ssrTransform blindly overwrites variable named as an import instead of checking for a locally scoped variable with the same name when destructuring an array
#5472
Closed
7 tasks done
jbmolle opened this issue
Oct 29, 2021
· 0 comments
This bug is similar to nuxt/nuxt#13736.
Found it by using i18n with nuxt3.
Here is the description from kazupon nuxt/nuxt#12585
If we destructure an array like let [ a, b, c ] = [ 1, 2, 3] and we do an import { a } from 'my-package' then vite doesn't see the local scope and replace the variable name.
Reproduction
Doing the test in packages/vite/src/node/ssr/tests/ssrTransform.spec.ts line 178
Actual test works:
test('do not rewrite when variable is in scope', async () => {
const result = await ssrTransform(
`import { fn } from 'vue';function A(){ const fn = () => {}; return { fn }; }`,
null,
null
)
expect(result.code).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
function A(){ const fn = () => {}; return { fn }; }"
`)
expect(result.deps).toEqual(['vue'])
})
Changing test with object destructuring also works:
test('do not rewrite when variable is in scope', async () => {
const result = await ssrTransform(
`import { fn } from 'vue';function A(){ let {fn, test} = {fn: 'foo', test: 'bar'}; return { fn }; }`,
null,
null
)
expect(result.code).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
function A(){ let {fn, test} = {fn: 'foo', test: 'bar'}; return { fn }; }"
`)
expect(result.deps).toEqual(['vue'])
})
But using test with array destructuring does not work:
test('do not rewrite when variable is in scope', async () => {
const result = await ssrTransform(
`import { fn } from 'vue';function A(){ let [fn, test] = ['foo', 'bar']; return { fn }; }`,
null,
null
)
expect(result.code).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
function A(){ let [fn, test] = ['foo', 'bar']; return { fn }; }"
`)
expect(result.deps).toEqual(['vue'])
})
The last test fails and we get: function A(){ let [fn, test] = ['foo', 'bar']; return { fn: vite_ssr_import_0.fn }; }
Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/vue-next instead.
Describe the bug
This bug is similar to nuxt/nuxt#13736.
Found it by using i18n with nuxt3.
Here is the description from kazupon nuxt/nuxt#12585
If we destructure an array like let [ a, b, c ] = [ 1, 2, 3] and we do an import { a } from 'my-package' then vite doesn't see the local scope and replace the variable name.
Reproduction
Doing the test in packages/vite/src/node/ssr/tests/ssrTransform.spec.ts line 178
Actual test works:
Changing test with object destructuring also works:
But using test with array destructuring does not work:
The last test fails and we get: function A(){ let [fn, test] = ['foo', 'bar']; return { fn: vite_ssr_import_0.fn }; }
System Info
Used Package Manager
pnpm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: