-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
How can I include mapbox-gl in Gatsby? #6667
Comments
I'm not sure about Mapbox, but with Leaflet the problem is that it expects to find |
As @ascorbic mentions, this is typically caused by libraries that expect to always run in a browser environment. During Maybe that's not exactly what's happening here (I don't think Also searching the |
Would that work in Gatsby? mapbox/mapbox-gl-js#4593 (comment) (can't test it at the moment) |
Here is the minimal example that works locally, but fails during build: https://github.com/lipis/mapbox-gatsby I didn't manage to make it work.. can anyone else take a look? (Feel free to use the access key, it's for testing purposes only and I'll delete it afterwards) |
Thanks to @herrmannplatz!! In exports.onCreateWebpackConfig = ({ actions, stage }) => {
if (stage === "build-html") {
actions.setWebpackConfig({
module: {
rules: [
{
test: /mapbox-gl/,
use: ['null-loader']
},
],
}
})
}
}; |
I wasn't able to get mine to work as per the above suggestions. I ended up mocking my implementation to get it to work. let mapboxgl
let ReactMapboxGl = {}
if (typeof window !== `undefined`) {
mapboxgl = require('mapbox-gl')
ReactMapboxGl = require('react-mapbox-gl')
} else {
ReactMapboxGl.Map = () => {
return class Mock extends React.Component {
constructor() {
super()
}
render() {
return <div />
}
}
}
}
const Map = ReactMapboxGl.Map({
accessToken: '[your-token-here]',
})
class DirectoryMap extends React.Component {
constructor(props) {
super(props)
}
render() {
return (
<div className="directory-map">
<Map
ref={e => {
this.map = e
}}
zoom={[11]}
speed={[0.6]}
>
</Map>
</div>
)
}
} |
For this to work in the latest version of Gatsby, you need to modify #6667 (comment) to this:
so |
@jonlow I tried this but it is not working for me, I am not really sure what is going on in the code above you explain or take a look at my repo here to see why I cannot load mapbox in production? Thanks for any help in advance |
@arhoy if you don't mind i'll take a look tomorrow and see if i can provide you with a solution to the issue. Do you mind waiting a bit? |
That would be great! Thank you so much
…On Mon, Nov 4, 2019 at 6:47 PM jonniebigodes ***@***.***> wrote:
@arhoy <https://github.com/arhoy> if you don't mind i'll take a look
tomorrow and see if i can provide you with a solution to the issue. Do you
mind waiting a bit?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#6667?email_source=notifications&email_token=ACPMK7NZWNNS4IQZS3LCMBDQSDGDXA5CNFSM4FLKFWUKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEDBKH3I#issuecomment-549626861>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACPMK7NPZHZMYJLZWJWXDR3QSDGDXANCNFSM4FLKFWUA>
.
|
For those trying to modify |
Instructions here: gatsbyjs/gatsby#6667 (comment)
Instructions here: gatsbyjs/gatsby#6667 (comment)
Have tried all the above solutions and am still having troubles with my build. Any help would be much appreciated! |
none of the above worked for me so I did the inline exclusion like this import mapboxgl from "!mapbox-gl" // https://webpack.js.org/concepts/loaders/#inline Following the MApbpx guideline https://docs.mapbox.com/mapbox-gl-js/api/#excluding-gl-js-explicitly-from-transpilation |
None of the above worked, because I was using a React library that included mapbpx-gl under the hood. By checking the mapbox docs I found another solution that worked. You can add the following the following property to package.json: This is the default browserlist config ( I did try to find a solution by trying to change the babel transpilation excludes, but I found this to be too difficult with gatsby. |
@Hless, if you were using First, I installed worker-loader:
Then in
All of the above is documented in various places, but the secret-sauce comes from the I created a wrapper component for this in Typescript like so (excuse the @ts-ignores, I couldn't work out how to make Typescript ignore the lack of types for those libs):
Now I just use the Quite a nightmare, but thought this might help someone else avoid wasting hours on it! :) |
Do you have a repository where I can see the project? |
I'm trying to include the https://github.com/mapbox/mapbox-gl-js in a Gatsby project, but I'm getting an error during build. Works nicely in dev mode.
(If someone wants to try this should be easier: https://github.com/alex3165/react-mapbox-gl)
The text was updated successfully, but these errors were encountered: