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

@vitejs/plugin-legacy breaks const hoisting in loops #13038

Closed
7 tasks done
IlyaSemenov opened this issue Apr 28, 2023 · 2 comments
Closed
7 tasks done

@vitejs/plugin-legacy breaks const hoisting in loops #13038

IlyaSemenov opened this issue Apr 28, 2023 · 2 comments
Labels
bug: upstream Bug in a dependency of Vite plugin: legacy

Comments

@IlyaSemenov
Copy link
Contributor

Describe the bug

I am compiling a legacy build for Chrome 49 with:

import { defineConfig } from 'vite';
import legacy from '@vitejs/plugin-legacy';

export default defineConfig({
  plugins: [
    legacy({
      targets: ['chrome 49'],
    }),
  ],
});

In the app code, I use variable hoisting inside a loop:

const messages = {
  good: 'You should see me',
  bad: 'You should not see me - hoisting broken!',
};
const obj = {};
for (const key in messages) {
  obj[key] = () => messages[key]; // <---- each closure uses a hoisted key
}
element.innerHTML = `${obj.good()}`;

What I expect is that the code works identically in both modern and legacy build.

Instead, the legacy build doesn't hoist key in the above loop. The resulting code is clearly wrong:

obj = {};
for (key in messages) {
  obj[key] = () => messages[key]; // <---- key is not hoisted
}
element.innerHTML = `${obj.good()}`;

Reproduction

https://stackblitz.com/edit/vitejs-vite-ndrwsg?file=vite.config.js,counter.js&terminal=dev

Steps to reproduce

Build and run production build:

pnpm build
pnpm preview
  1. Load the modern build. The page will say: "You should see me"

  2. Load the legacy build. To do so on a modern browser, edit dist/index.html and remove two lines:

  • <script type="module" crossorigin src="/assets/index-9d88f3c8.js"></script>
  • if (window.__vite_is_modern_browser) return;

The page will say: "You should not see me - hoisting broken!"

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 7.17.0 - /usr/local/bin/npm
  npmPackages:
    @vitejs/plugin-legacy: ^4.0.3 => 4.0.3 
    vite: ^4.3.2 => 4.3.3

Used Package Manager

pnpm

Logs

No response

Validations

@stackblitz
Copy link

stackblitz bot commented Apr 28, 2023

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@sapphi-red
Copy link
Member

sapphi-red commented Apr 28, 2023

This can be reproduced only with babel + preset-env.
https://stackblitz.com/edit/node-6scvhf?file=index.js

Closing as this seems to be tracked at babel/babel#15594.

@sapphi-red sapphi-red closed this as not planned Won't fix, can't repro, duplicate, stale Apr 28, 2023
@github-actions github-actions bot locked and limited conversation to collaborators May 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug: upstream Bug in a dependency of Vite plugin: legacy
Projects
None yet
Development

No branches or pull requests

2 participants