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

"ReferenceError: Can't find variable: globalThis" @vitejs/plugin-legacy in Safari 11 #11645

Closed
7 tasks done
ptim opened this issue Jan 10, 2023 · 2 comments
Closed
7 tasks done
Labels
duplicate This issue or pull request already exists

Comments

@ptim
Copy link

ptim commented Jan 10, 2023

Describe the bug

Hi! I'm trying to build a simple react-ts app which consumes @chakra-ui/react for legacy browsers, and in testing Safari 11 I'm getting a blank page and the console error:

ReferenceError: Can't find variable: globalThis

Because Safari 11 supports modules, it gets the module bundle, no polyfills. If I hack dist/index.html to remove the <script modules .../> completely (and the nomodules sttr on legacy bundles) and have it load the legacy bundles, then page renders fine in Safari 11.

Is there a way of causing Safari 11 to use legacy bundles? Tx!

Reproduction

https://stackblitz.com/edit/vitejs-vite-9grdsk?file=vite.config.js,src%2Fmain.jsx,src%2FApp.jsx&terminal=dev

Steps to reproduce

  • configure @vitejs/plugin-legacy for Safari >= 10
  • npm run build && npm run preview -- --host
  • checked localhost:4173 in Safari 11 in browserstack (don't know if you can load stackblitz this way - believe you need to run locally with browserstack local installed)
  • I see a blank page and the console error:

    "ReferenceError: Can't find variable: globalThis".

// vite.config.js
export default defineConfig({
  plugins: [
    react(),
    legacy({
      targets: ["defaults", "safari >= 10", "not IE 11"],
    }),
  ],
});

System Info

System:
    OS: macOS 13.0.1
    CPU: (10) x64 Apple M1 Pro
    Memory: 64.05 MB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.12.1 - ~/Library/Caches/fnm_multishells/56743_1673306339588/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 8.19.3 - /opt/homebrew/bin/npm
  Browsers:
    Brave Browser: 108.1.46.153
    Firefox: 108.0.2
    Safari: 16.1
  npmPackages:
    @vitejs/plugin-legacy: ^3.0.1 => 3.0.1 
    @vitejs/plugin-react: ^3.0.0 => 3.0.1 
    vite: ^4.0.0 => 4.0.4

Used Package Manager

npm

Logs

ReferenceError: Can't find variable: globalThis

Sorry - can't copy text out of the browserstack console! (note that the error is raise in the module bundle)

CleanShot 2023-01-10 at 11 39 53@2x

Validations

@sapphi-red
Copy link
Member

I think you mean Safari 11.1 and not Safari 11, as the url to browserstack is linked to Safari 11.1.

Is there a way of causing Safari 11 to use legacy bundles?

Safari 11 will use legacy bundles and there aren't a way to make Safari 11.1 to use legacy bundles.(#6922)

The current intended way is to use modernPolyfills option.
https://github.com/vitejs/vite/tree/main/packages/plugin-legacy#modernpolyfills

@sapphi-red sapphi-red closed this as not planned Won't fix, can't repro, duplicate, stale Jan 10, 2023
@sapphi-red sapphi-red added duplicate This issue or pull request already exists and removed pending triage labels Jan 10, 2023
@ptim
Copy link
Author

ptim commented Jan 11, 2023

The current intended way is to use modernPolyfills option.

Thanks very much - es.global-this solves my issue! Will watch 6922 for other edge cases 🙏

import legacy from '@vitejs/plugin-legacy'

export default {
  plugins: [
    legacy({
      modernPolyfills: [
        // Safari 11 has modules, but throws > ReferenceError: Can't find variable: globalThis
        "es.global-this",
      ],
    }),
  ],
}

@github-actions github-actions bot locked and limited conversation to collaborators Jan 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants