Solvedangular cli mapboxgl is incompatible with the CLI in a production build
✔️Accepted Answer
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.
Bug Report or Feature Request (mark with an
x
)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'
viang serve
seems to work just fine. But if you run it via:An error is thrown from one of the workers created by Mapbox.
Versions.
Repro steps.
"mapbox-gl": "^0.34.0"
"@types/mapbox-gl": "^0.30.0"
)import * as mapboxgl from 'mapboxgl'
anywhere in the app (logmapboxgl
just to see the output)const map: mapboxgl.Map = new Map(Object.assign({container: element}));
(element
is thenativeElement
from@ViewChild('map', {read: ElementRef}) mapRef: ElementRef
)The log given by the failure.
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
: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.