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

Error while rendering map with mapbox-gl: "_typeof is not defined" #1128

Closed
dolbyzerr opened this issue Apr 3, 2018 · 23 comments
Closed

Error while rendering map with mapbox-gl: "_typeof is not defined" #1128

dolbyzerr opened this issue Apr 3, 2018 · 23 comments

Comments

@dolbyzerr
Copy link

This a 🐛 bug report

🎛 Configuration (.babelrc, package.json, cli command)

{
  "dependencies": {
    "mapbox-gl": "^0.44.1",
    "parcel-bundler": "^1.7.0"
  }
}
npx parcel index.html

🤔 Expected Behavior

A map should be rendered.

😯 Current Behavior

Grey area is rendered:
screenshot 2018-04-03 12 11 57

The error in console:
Uncaught ReferenceError: _typeof is not defined

screenshot 2018-04-03 12 20 10

💁 Possible Solution

As far as I understood from the error and this issue from mapbox mapbox/mapbox-gl-js#3422 it happens because parcel transpiling mapbox file that already being transpiled. Is there any way to alter that behaviour?

💻 Code Sample

<html>
<head>
  <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.44.1/mapbox-gl.css' rel='stylesheet' />
</head>
<body>
  <div id='map' style='width: 400px; height: 300px;'></div>
  <script src="./index.js"></script>
</body>
</html>
import mapboxgl from 'mapbox-gl'

mapboxgl.accessToken = 'pk.eyJ1IjoiZG9sYnl6ZXJyIiwiYSI6InhIS25oN0EifQ.QQrwwFUZu6trJNjGsrpTFQ'
var map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/mapbox/streets-v9'
});

🌍 Your Environment

Software Version(s)
Parcel 1.7.0
Node v8.6.0
npm 5.6.0
Operating System macOS 10.13.4
@dolbyzerr dolbyzerr changed the title Error while rendering map with mapbox-gl: _typeof is not defined Error while rendering map with mapbox-gl: "_typeof is not defined" Apr 3, 2018
@dcposch
Copy link

dcposch commented Apr 29, 2018

i'm running into the same problem.

my guess is that it's related to Web Workers. the _typeof is not defined errors all occur in Web Worker contexts, which are loaded from blob URIs. the blobs reference _typeof but do not define it.

@safareli
Copy link

safareli commented May 3, 2018

you might wanna just "disable" babel see #509 (comment)

@davidnagli davidnagli added this to To Do in Bugs via automation Jul 2, 2018
@davidnagli
Copy link
Contributor

Related: #670
The root cause of this issue is that we don't support web workers. Anybody willing to contribute this feature? How would we go about doing this?

Running into this exact issue for a work project and really need a fix 😇

@tobinbradley
Copy link

Here's my "fun with Babel" contribution.

Fiddling with my .babelrc I discovered limiting it to modern browsers worked.

{
  "presets": [
    ["env", {
      "targets": {
        "browsers": "last 2 Firefox versions, last 2 Chrome versions, last 2 Edge versions, last 2 Safari versions"
      }
    }]
  ]
}

After a bunch of fiddling, it seems Babel's transformation for IE and Android Browser are causing the problem. If I use the browser string "defaults, not ie <999, not Android < 999" to exclude those browsers, Bob's your uncle. Put either of those two browsers back in the pool, _typeof is not defined error.

But, if I run the Mapbox GL JS library through the Babel command line (Babel CLI 6.26.0, adds 2kb to the file) and then link to it directly in the example at the top, it works fine (note no import is going on here).

<script src="mapbox-babeled.js"></script>
  <script>
    mapboxgl.accessToken = 'pk.eyJ1IjoiZG9sYnl6ZXJyIiwiYSI6InhIS25oN0EifQ.QQrwwFUZu6trJNjGsrpTFQ'
    var map = new mapboxgl.Map({
      container: 'map',
      style: 'mapbox://styles/mapbox/streets-v9'
    });
  </script>

So, it looks like it's something with Babel combined with the import bundler, but only when Babel is correcting for those browsers. I guess.

@jotasolano
Copy link

I tried @tobinbradley's solution (changing the .babelrc browsers, not the second option) and it did not work for me. Maybe something has changed between there and now, but I get the same error as in the original bug. Any other ideas about how to deal with this?

@MoritzStefaner
Copy link

I can confirm I have the same issue here using

"devDependencies": {
    "@babel/core": "^7.4.0",
    "@babel/preset-env": "^7.4.2",
    "@babel/preset-react": "^7.0.0",
    "babel-preset-react": "^6.24.1",
    "parcel-bundler": "^1.12.3"
  },
  "dependencies": {
    "mapbox-gl": "^0.51.0",
    "react-mapbox-gl": "^4.2.0"
  }

Any suggestions, ideas, …?

@jotasolano
Copy link

I ran out of ideas. I ended up using another bundler/boilerplate solution much to my chagrin. It looks to me like this won't be fixed until Parcel 2 arrives

@cyrilchapon
Copy link

Confirmed same issue with react-map-gl and even raw mapbox-gl...

@AwesomeChap
Copy link

AwesomeChap commented May 30, 2019

Here's my "fun with Babel" contribution.

Fiddling with my .babelrc I discovered limiting it to modern browsers worked.

{
  "presets": [
    ["env", {
      "targets": {
        "browsers": "last 2 Firefox versions, last 2 Chrome versions, last 2 Edge versions, last 2 Safari versions"
      }
    }]
  ]
}

After a bunch of fiddling, it seems Babel's transformation for IE and Android Browser are causing the problem. If I use the browser string "defaults, not ie <999, not Android < 999" to exclude those browsers, Bob's your uncle. Put either of those two browsers back in the pool, _typeof is not defined error.

But, if I run the Mapbox GL JS library through the Babel command line (Babel CLI 6.26.0, adds 2kb to the file) and then link to it directly in the example at the top, it works fine (note no import is going on here).

<script src="mapbox-babeled.js"></script>
  <script>
    mapboxgl.accessToken = 'pk.eyJ1IjoiZG9sYnl6ZXJyIiwiYSI6InhIS25oN0EifQ.QQrwwFUZu6trJNjGsrpTFQ'
    var map = new mapboxgl.Map({
      container: 'map',
      style: 'mapbox://styles/mapbox/streets-v9'
    });
  </script>

So, it looks like it's something with Babel combined with the import bundler, but only when Babel is correcting for those browsers. I guess.

May be you should try excluding /mapbox-gl from transpiling. It worked for me. My final .babelrc looks like this

{
  "presets": [
    [
      "env",
      {
        "targets": {
          "browsers": "last 2 Firefox versions, last 2 Chrome versions, last 2 Edge versions, last 2 Safari versions"
        },
        "exclude" : ["/mapbox-gl"]
      }
    ]
  ],
  "plugins": [
    "@babel/plugin-proposal-class-properties"
  ]
}

@lrjbrual
Copy link

I have the same issue, still can't find any good solution.

@vemundeldegard
Copy link

I cannot find a fix for this. I have tried all of the above in .babelrc and nothing works.

@cyrilchapon
Copy link

Please for god sake. "Just don't transpile the module lol" is not an answer at all. Some are actually using it in production with facing customers.

Everything about Babel here is not about fixing the issue; but avoiding the issue, making somehow Babel avoid mapbox transpilation in some context.

We need an answer. Doesn't mapbox work in certain browser contexts because of webworkers ? Fine, please we figure it out and spell it, list it. Shouldn't we ?

@jotasolano
Copy link

@cyrilchapon while I understand your frustration, this is not the right tone. This is open source software, you're not paying a dime for Parcel and the root issue has been identified and open. I'm sure the core devs would welcome your contribution (#670) but otherwise I think you should be more respectful of all the work they have done.

@cyrilchapon
Copy link

@jotasolano was I revendicative on my previous answer ? Or disrespectful against contributors ? Or even acting like a "customer" ? I don't agree actually. I'm pretty aware this is open source software, and I did not even mention the fact this issue not being tackled.

I was complaining about bad issue triaging and commenting. "Just don't transpile" is not a clever answer, and that's all I'm saying.

Everyone coming to this issue will now know this issue is being tackled, thanks to the link you posted. Please notice no-one could guess that before your answer. In that sense, this issue moved on a little bit; and that's my way of contributing.

@niceone
Copy link

niceone commented Dec 20, 2019

I cannot find a fix for this. I have tried all of the above in .babelrc and nothing works.

Excluding mapbox-gl in .babelrc did the trick for me as suggested above, but only after forcing Parcel to rebuild everything. Try --no-cache on parcel.

@jotasolano
Copy link

jotasolano commented Dec 20, 2019

@jotasolano was I revendicative on my previous answer ? Or disrespectful against contributors ? Or even acting like a "customer" ? I don't agree actually. I'm pretty aware this is open source software, and I did not even mention the fact this issue not being tackled.

I was complaining about bad issue triaging and commenting. "Just don't transpile" is not a clever answer, and that's all I'm saying.

Everyone coming to this issue will now know this issue is being tackled, thanks to the link you posted. Please notice no-one could guess that before your answer. In that sense, this issue moved on a little bit; and that's my way of contributing.

@cyrilchapon If you scroll up you'll see that the issue I referenced was posted pretty early (comment 4) #1128 (comment) I just re-referenced for you.

@raxod502
Copy link

Is there a workaround to this issue that doesn't involve dropping support for most browsers? The solution suggested #1128 (comment) works but not if I set the browser list to defaults.

Is Parcel ignoring the excludes list? If so, why?

@jordymeow
Copy link

I am encountering the same issue but using Babel 7 (@babel/...). The solutions mentioned above, therefore, don't work for me. It seems the options for env have changed. I spent a lot of time trying to find a solution and understanding the extremely obscure Babel documentation, but to be honest, I failed. And can't wait to be able to code again. Surely there is a better way than switching back to Webpack? :(

Would love to have your ideas. I can't be the only one using Babel 7, Parcel, and MapBox.

@agustinmulet
Copy link

I got to fix this error using Babel 7, and react-map-gl
What I did was install Babel 7 with yarn add -D @babel/core @babel/cli and then add a .babelrc file with this:

{
    "presets": [
        ["@babel/env", {
        "targets": {
            "browsers": [
            ">2%",
            "last 1 Edge version",
            "last 2 Safari version",
            "last 1 Firefox version"
            ]
        },
        "loose": true
        }]
    ]
}

According to the Babel docs, the "loose": true does the trick, excluding typeof-symbol which seems to be causing this issue.

@willbarkoff
Copy link

Has anyone been able to solve this with TypeScript (no babel)?

@babak-bekhrad
Copy link

babak-bekhrad commented Nov 7, 2020

I have the same issue. but for fixing this bug, I used the Mapbox CDN instead of module bundler and finally the map area is rendered without Grey area .
<script src='https://api.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.js'></script>

@markusand
Copy link

{
  "presets": [
    ["env", {
      "targets": {
        "browsers": "last 2 Firefox versions, last 2 Chrome versions, last 2 Edge versions, last 2 Safari versions"
      }
    }]
  ]
}

Following @tobinbradley solution, I found that just creating a .browserslist and completely excluding IE works for me.

defaults
not IE > 0

I'm using new mapbox-gl 2.0.0

@devongovett
Copy link
Member

Appears to work correctly in Parcel 2 nightly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Bugs
  
To Do
Development

No branches or pull requests