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

when @babel/polyfill is used with webpack 5 it throws a ReferenceError: global is not defined #9232

Closed
capaj opened this issue Dec 23, 2018 · 6 comments · Fixed by #9780 · May be fixed by O-Mutt/fields-of-arle#9
Closed
Labels
i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: polyfill

Comments

@capaj
Copy link

capaj commented Dec 23, 2018

Bug Report

Current Behavior
on startup, I get the ReferenceError

index.js:25 Uncaught ReferenceError: global is not defined
    at Object.../node_modules/@babel/polyfill/lib/index.js (index.js:25)
    at __webpack_require__ (bootstrap:22)
    at fn (hot module replacement:64)
    at Module../src/index/globals-import.ts (error-handler.js:37)
    at __webpack_require__ (bootstrap:22)
    at fn (hot module replacement:64)
    at Module../src/index/index.js (globals-import.ts:15)
    at __webpack_require__ (bootstrap:22)
    at bootstrap:47
    at bootstrap:47

Expected behavior/code
no ReferenceError is thrown

Environment

  • Babel version(s): "@babel/polyfill": "^7.2.5"
  • Node/npm version: Node 8/npm 6.4.1
  • OS: ubuntu
  • Monorepo: no
  • How you are using Babel: [loader]

I understand that I probably should just provide a global and that could mitigate the startup issue, but I also wanted to ask why?
Isn't babel polyfill primarily targeted at old browsers? Why is it that it relies on global to bootstrap itself when it's obviously not going to be there? Unless using webpack 1-4 obviously....

@babel-bot
Copy link
Collaborator

Hey @capaj! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community
that typically always has someone willing to help. You can sign-up here
for an invite.

@nicolo-ribaudo
Copy link
Member

I think that this has something to do with the fact that webpack 5 doesn't polyfill any node feature (i.e. global) by default anymore.

@nicolo-ribaudo
Copy link
Member

Does webpack/webpack#8537 (comment) work for you?

@capaj
Copy link
Author

capaj commented Dec 23, 2018

@nicolo-ribaudo just tested adding:

module.exports = {
  node: {
    global: true
  },

and yeah that works nicely. I'd still be interested to know why babel polyfill relies on the global in the first place? Wouldn't it be nicer to first check if there's a window object and if it is, use it instead of global?

@nicolo-ribaudo
Copy link
Member

Yeah, or we could just use the global polyfill from core-js

@andersk
Copy link
Contributor

andersk commented Dec 24, 2018

The documentation recommends using @babel/preset-env with the useBuiltIns option set to 'entry' or 'usage'. In that case the top-level @babel/polyfill module isn’t used and this bug doesn’t appear.

We should still fix @babel/polyfill, but at least the useBuiltIns workaround is something you might want to be doing anyway.

andersk added a commit to andersk/MusicParsed that referenced this issue Dec 24, 2018
Makes the JavaScript bundles a tiny bit smaller.

(Requires the useBuiltIns option of @babel/preset-env due to
babel/babel#9232.)

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jul 3, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Jul 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: polyfill
Projects
None yet
4 participants