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

Named export is not defined when used destructuring an object #9585

Closed
7 tasks done
inakiabt opened this issue Aug 8, 2022 · 0 comments · Fixed by #9609
Closed
7 tasks done

Named export is not defined when used destructuring an object #9585

inakiabt opened this issue Aug 8, 2022 · 0 comments · Fixed by #9609
Labels
feat: ssr p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority)

Comments

@inakiabt
Copy link

inakiabt commented Aug 8, 2022

Describe the bug

Note: Found this issue testing my app with vitest. I've asked in vitest repo in case it was an issue on their side, but seems it is not.

Looks like imported named exports are not being correctly transformed when used in object desctructuring while building for SSR (this is what vitest uses to transform files while testing).

import { createServer } from 'vite';
const server = await createServer({});

const code = `
import { CODE } from './path'

console.log(CODE);

export const func = ({ [CODE]: result }) => result;
`;
const result = await server.ssrTransform(code);

console.log(result.code);

Outputs:

const __vite_ssr_import_0__ = await __vite_ssr_import__("./path");

console.log(__vite_ssr_import_0__.CODE);

const func = ({ [CODE]: result }) => result;
Object.defineProperty(__vite_ssr_exports__, "func", { enumerable: true, configurable: true, get(){ return func }});

As can be seen, CODE is not correctly referenced in line 5.
I would expect to get something like:

const __vite_ssr_import_0__ = await __vite_ssr_import__("./path");

console.log(__vite_ssr_import_0__.CODE);

const func = ({ [__vite_ssr_import_0__.CODE]: result }) => result;
Object.defineProperty(__vite_ssr_exports__, "func", { enumerable: true, configurable: true, get(){ return func }});

Originally posted by @inakiabt in vitest-dev/vitest#1789

Reproduction

https://stackblitz.com/edit/node-h31zjx?file=index.js

System Info

System:
    OS: macOS 12.5
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 795.55 MB / 16.00 GB
    Shell: 5.1.16 - /usr/local/bin/bash
  Binaries:
    Node: 16.10.0 - ~/.nvm/versions/node/v16.10.0/bin/node
    npm: 7.24.0 - ~/.nvm/versions/node/v16.10.0/bin/npm
    Watchman: 2022.07.04.00 - /usr/local/bin/watchman
  Browsers:
    Chrome: 103.0.5060.134
    Firefox: 102.0.1
    Safari: 15.6
  npmPackages:
    vite: ^2.9.14 => 2.9.14 
    vitest: ^0.20.3 => 0.20.3

Used Package Manager

npm

Logs

No response

Validations

@sapphi-red sapphi-red added the p3-minor-bug An edge case that only affects very specific usage (priority) label Aug 9, 2022
@sapphi-red sapphi-red added p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority) and removed p3-minor-bug An edge case that only affects very specific usage (priority) labels Aug 10, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Aug 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feat: ssr p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants