question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

mapboxgl is incompatible with the CLI in a production build

See original GitHub issue

Bug Report or Feature Request (mark with an x)

- [x] bug report -> please search issues before submitting
- [ ] feature request

It seems that using Mapbox GL and Angular CLI when targeting production does not work.

Running an app that imports Mapbox with import * as mapboxgl from 'mapboxgl' via ng serve seems to work just fine. But if you run it via:

ng serve --open --sourcemap --target=production --output-hashing=all

An error is thrown from one of the workers created by Mapbox.

Versions.

@angular/cli: 1.0.0-rc.4
node: 7.4.0
os: darwin x64
@angular/common: 2.4.10
@angular/compiler: 2.4.10
@angular/core: 2.4.10
@angular/flex-layout: 2.0.0-rc.1
@angular/forms: 2.4.10
@angular/http: 2.4.10
@angular/material: 2.0.0-beta.2
@angular/platform-browser: 2.4.10
@angular/platform-browser-dynamic: 2.4.10
@angular/router: 3.4.10
@angular/cli: 1.0.0-rc.4
@angular/compiler-cli: 2.4.10
@angular/tsc-wrapped: 0.5.2

Repro steps.

  1. Scaffold an Angular CLI app
  2. Add Mapbox GL dep "mapbox-gl": "^0.34.0"
  3. Add the typings ("@types/mapbox-gl": "^0.30.0")
  4. import * as mapboxgl from 'mapboxgl' anywhere in the app (log mapboxgl just to see the output)
  5. Create a map in one of the components const map: mapboxgl.Map = new Map(Object.assign({container: element})); (element is the nativeElement from @ViewChild('map', {read: ElementRef}) mapRef: ElementRef)

The log given by the failure.

blob:https://ds.infarm.com/e940fbf0-8c73-42bf-a3cd-d5326c57e0f6:1 Uncaught ReferenceError: e is not defined
33../support/isBuffer @ blob:https://ds.infarm.com/e940fbf0-8c73-42bf-a3cd-d5326c57e0f6:1
o @ blob:https://ds.infarm.com/e940fbf0-8c73-42bf-a3cd-d5326c57e0f6:1
(anonymous) @ blob:https://ds.infarm.com/e940fbf0-8c73-42bf-a3cd-d5326c57e0f6:1
102.util @ blob:https://ds.infarm.com/e940fbf0-8c73-42bf-a3cd-d5326c57e0f6:1
o @ blob:https://ds.infarm.com/e940fbf0-8c73-42bf-a3cd-d5326c57e0f6:1
(anonymous) @ blob:https://ds.infarm.com/e940fbf0-8c73-42bf-a3cd-d5326c57e0f6:1
128.../error/validation_error @ blob:https://ds.infarm.com/e940fbf0-8c73-42bf-a3cd-d5326c57e0f6:1
o @ blob:https://ds.infarm.com/e940fbf0-8c73-42bf-a3cd-d5326c57e0f6:1
(anonymous) @ blob:https://ds.infarm.com/e940fbf0-8c73-42bf-a3cd-d5326c57e0f6:1
142../reference/latest @ blob:https://ds.infarm.com/e940fbf0-8c73-42bf-a3cd-d5326c57e0f6:1
o @ blob:https://ds.infarm.com/e940fbf0-8c73-42bf-a3cd-d5326c57e0f6:1
(anonymous) @ blob:https://ds.infarm.com/e940fbf0-8c73-42bf-a3cd-d5326c57e0f6:1
156.../style-spec/validate_style.min @ blob:https://ds.infarm.com/e940fbf0-8c73-42bf-a3cd-d5326c57e0f6:1
o @ blob:https://ds.infarm.com/e940fbf0-8c73-42bf-a3cd-d5326c57e0f6:1
(anonymous) @ blob:https://ds.infarm.com/e940fbf0-8c73-42bf-a3cd-d5326c57e0f6:1
148.../style-spec/reference/latest @ blob:https://ds.infarm.com/e940fbf0-8c73-42bf-a3cd-d5326c57e0f6:1
o @ blob:https://ds.infarm.com/e940fbf0-8c73-42bf-a3cd-d5326c57e0f6:1
(anonymous) @ blob:https://ds.infarm.com/e940fbf0-8c73-42bf-a3cd-d5326c57e0f6:1
154.../style-spec/feature_filter @ blob:https://ds.infarm.com/e940fbf0-8c73-42bf-a3cd-d5326c57e0f6:1
o @ blob:https://ds.infarm.com/e940fbf0-8c73-42bf-a3cd-d5326c57e0f6:1
(anonymous) @ blob:https://ds.infarm.com/e940fbf0-8c73-42bf-a3cd-d5326c57e0f6:1
98.../style/style_layer_index @ blob:https://ds.infarm.com/e940fbf0-8c73-42bf-a3cd-d5326c57e0f6:1
o @ blob:https://ds.infarm.com/e940fbf0-8c73-42bf-a3cd-d5326c57e0f6:1
(anonymous) @ blob:https://ds.infarm.com/e940fbf0-8c73-42bf-a3cd-d5326c57e0f6:1
anonymous @ blob:https://ds.infarm.com/e940fbf0-8c73-42bf-a3cd-d5326c57e0f6:3
o @ blob:https://ds.infarm.com/e940fbf0-8c73-42bf-a3cd-d5326c57e0f6:1
t @ blob:https://ds.infarm.com/e940fbf0-8c73-42bf-a3cd-d5326c57e0f6:1
(anonymous) @ blob:https://ds.infarm.com/e940fbf0-8c73-42bf-a3cd-d5326c57e0f6:1

Desired functionality.

An app using Mapbox should work when building for production.

Mention any other details that might be useful.

If I instead load Mapbox via .angular-cli.json:

"styles": [
    "../node_modules/mapbox-gl/dist/mapbox-gl.css"
],
"scripts": [
    "../node_modules/mapbox-gl/dist/mapbox-gl.js"
]

It works as expected. I suspect it might have something to do with minification or some other transformation applied to one of the mapbox scripts.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:8
  • Comments:54 (12 by maintainers)

github_iconTop GitHub Comments

9reactions
aptogocommented, Aug 11, 2017

I think I have a solution to this issue…

My setup:

@angular/cli: 1.2.1
node: 6.9.5
os: darwin x64
@angular/animations: 4.3.0
@angular/cdk: 2.0.0-beta.8
@angular/common: 4.3.0
@angular/compiler: 4.3.0
@angular/core: 4.3.0
@angular/flex-layout: 2.0.0-rc.1
@angular/forms: 4.3.0
@angular/http: 4.3.0
@angular/material: 2.0.0-beta.8
@angular/platform-browser: 4.3.0
@angular/platform-browser-dynamic: 4.3.0
@angular/router: 4.3.0
@angular/cli: 1.2.1
@angular/compiler-cli: 4.3.0
@angular/language-service: 4.3.0

Install mapbox-gl (note that this is just to get the prebuilt js and css files in /dist. The source is not used) (my version is mapbox-gl@0.39.1): npm install --save mapbox-gl

Install mapbox-gl types (my version is @types/mapbox-gl@0.39.4): npm install --save @types/mapbox-gl

Very important - this is the key step Edit tsconfig.app.json and change the compilerOptions/types line: types: ["mapbox-gl", "geojson"]

Edit tsconfig.spec.json and change the compilerOptions/types line: types: [“mapbox-gl”, “geojson”, “jasmine”, “node”]

This enables automatic inclusion of the required types from the @types packages

Add the pre-built mapbox js and css to your .angular-cli.json config:\

...
"styles": [
        "styles.scss",
        "../node_modules/mapbox-gl/dist//mapbox-gl.css"
      ],
      "scripts": [
        "../node_modules/mapbox-gl/dist/mapbox-gl.js"
      ]
...

Now make sure that you have no import * as mapbox from 'mapbox-gl' statements anywhere in your codebase. You should be able to use mapbox with full typing like this e.g mapboxgl.Map, mapboxgl.LngLat. Geojson types are also pulled in e.g. GeoJSON.FeatureCollection<GeoJSON.GeometryObject>

Note that if you forget and leave any import * as mapbox from 'mapbox-gl' statements in your codebase then you will end up with 2 copies of mapboxgl - 1 from the script inclusion, and 1 built from the source. However, the 1 from the script inclusion seems to be loaded after so it will still work ok but the size of your app will be bigger.

2reactions
filipesilvacommented, Nov 29, 2017
Read more comments on GitHub >

github_iconTop Results From Across the Web

Issue in Ionic production build for Mapbox GL JS feature
But for production build i.e ionic cordova build android --prod ... Issue: mapboxgl is incompatible with the CLI in a production build.
Read more >
Mapbox GL JS in Single-Page-Application (Vue 3) not working ...
I've created an application using Vue 3 and a map based on Mapbox GL JS (version 2.11). The app was scaffolded with Vue...
Read more >
Using Mapbox GL JS with React - LogRocket Blog
Want to build map interfaces with React? In this post, learn to use react-map-gl to build two variations of map components.
Read more >
mapbox-gl | Yarn - Package Manager
Mapbox GL JS is a JavaScript library for interactive, customizable vector maps on the web. It takes map styles that conform to the...
Read more >
Installation | Mapbox GL JS
The JavaScript bundle is incompatible with some Babel transforms because of the way it shares code between the main thread and Web Worker....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found