Developer Tools and Resources
This page provides a comprehensive overview of the tools, utilities, and resources available for developers working with the Mapbox GL JS codebase. Whether you're building the library from source, debugging rendering issues, or extending functionality, this guide will help you navigate the development ecosystem.
For more detailed information on specific topics, see the following sub-pages:
- Build System - Detailed explanation of building Mapbox GL JS from source
- Testing and Debugging - Comprehensive guide to test frameworks and debugging tools
- Networking and AJAX - In-depth coverage of request handling and transformation
Development Environment Setup
To work effectively with the Mapbox GL JS codebase, you'll need to set up a development environment:
- Clone the repository:
git clone https://github.com/mapbox/mapbox-gl-js.git - Install dependencies:
npm ci - Run the development server:
npm start
The development server will watch for changes to source files and automatically rebuild the library.
Build System Overview
Mapbox GL JS uses a modern build system centered around Rollup with plugins for TypeScript transpilation, module resolution, and minification. The build process creates multiple artifacts optimized for different use cases.
Build System Architecture
Key Build Commands
The repository provides various npm scripts for different build scenarios:
| Command | Description |
|---|---|
npm run build-dev | Development build with source maps and debugging tools |
npm run watch-dev | Watch mode for development |
npm run build-prod | Production build (unminified) |
npm run build-prod-min | Minified production build |
npm run build-csp | Content Security Policy compliant build |
npm run build-css | Build CSS stylesheets |
npm run build-style-spec | Build the style specification module |
npm run build-dts | Generate TypeScript declaration files |
Development Workflow
For active development, the repository provides a convenient start script:
npm startThis command runs multiple processes in parallel:
- Generates an access token script
- Watches CSS files for changes
- Watches source files and rebuilds in development mode
- Starts a static HTTP server on port 9966
Testing Infrastructure
Mapbox GL JS implements a comprehensive testing approach with multiple test types and runners. This ensures both unit-level correctness and proper visual rendering across browsers.
Testing Architecture
Key Test Types
The codebase includes several types of tests:
Unit Tests: Tests for individual components using Vitest
- Run with
npm run test-unit
- Run with
Rendering Tests: Visual regression tests for rendering capabilities
- Each test has a style specification and expected output image
- Examples: test/integration/render-tests/debug/collision-pitched/style.json
- Run with
npm run test-render
Query Tests: Tests for spatial querying functionality
- Run with
npm run test-query
- Run with
Expression Tests: Tests for the style expression evaluation system
- Run with
npm run test-expressions
- Run with
Type Checking: Verification of TypeScript types
- Run with
npm run test-typings
- Run with
Continuous Integration
CI/CD is handled by CircleCI, which runs tests on multiple platforms (Linux, macOS, Windows) and browsers (Chrome, Firefox, Safari). The configuration is defined in .circleci/config.yml
Debugging Tools
Mapbox GL JS includes several built-in debugging tools to help diagnose rendering issues and performance problems.
Debug UI
When using the development build, you can enable a debugging UI by setting the devtools option:
const map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v11',
devtools: true
});This UI provides controls for inspecting map properties, monitoring performance metrics, and visualizing internal state.
Debug Rendering Modes
Several debug rendering modes help visualize internal rendering structures:
Collision Boxes: Visualize text and icon collision detection
- Enable with
map.showCollisionBoxes = true - Implemented in src/shaders/collision_box.vertex.glsl and src/shaders/collision_box.fragment.glsl
- Example test cases: test/integration/render-tests/debug/collision-pitched/style.json
- Enable with
Tile Boundaries: Visualize tile loading and boundaries
- Enable with
map.showTileBoundaries = true
- Enable with
Terrain Wireframe: Visualize terrain mesh (when terrain is enabled)
- Enable with
map.showTerrainWireframe = true
- Enable with
Performance Monitoring
For performance monitoring, use the renderstart and render events to measure frame rendering duration:
map.on('renderstart', () => { /* Record start time */ });
map.on('render', () => { /* Calculate frame duration */ });Network Request Architecture
Mapbox GL JS implements a flexible system for handling network requests, supporting request transformation, authentication, and caching.
Request Pipeline
The networking system processes requests through the following pipeline:
- Request Creation: Generate a request for a resource (style, tile, etc.)
- Request Transformation: Apply custom transformations (if configured)
- Authentication: Add access tokens and credentials
- Request Execution: Send the HTTP request
- Response Caching: Cache responses for improved performance
- Response Parsing: Parse the response into the appropriate format
Customizing Requests
You can customize network requests by providing a transformRequest function:
const map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v11',
transformRequest: (url, resourceType) => {
// Modify the request
return { url, headers, credentials, method };
}
});The resourceType parameter identifies the type of resource:
'Style': Style JSON document'Source': Source data'Tile': Map tiles'Glyphs': Font glyphs'SpriteImage': Sprite images'SpriteJSON': Sprite metadata'Image': Custom images
Development Resources
Code Organization
The Mapbox GL JS codebase is organized into several main directories:
src/: Source codesrc/style-spec/: Style specification implementationsrc/render/: Rendering enginesrc/geo/: Geographic utilitiessrc/ui/: UI components (markers, popups, controls)src/util/: Utility functionssrc/shaders/: GLSL shader code
test/: Test codetest/unit/: Unit teststest/integration/: Integration tests (rendering, query)test/build/: Build tests
debug/: Debugging utilities and examplesdist/: Build output (generated during build)
Development Tools
The repository includes several tools to help maintain code quality:
Linting:
- ESLint for JavaScript/TypeScript:
npm run lint - Stylelint for CSS:
npm run lint-css - Configuration in .stylelintrc
- ESLint for JavaScript/TypeScript:
Bundle Size Checking:
- Ensures bundle size doesn't exceed limits
- Run with
npm run check-bundle-size
TypeScript Suppressions Checking:
- Monitors TypeScript error suppressions
- Run with
npm run check-ts-suppressions
Code Generation:
- Generates code for style specification, struct arrays, etc.
- Run with
npm run codegen
Community Resources
Beyond the code repository itself, these resources can help developers working with Mapbox GL JS:
- API Documentation: https://docs.mapbox.com/mapbox-gl-js/api/
- Style Specification: https://docs.mapbox.com/mapbox-gl-js/style-spec/
- Example Gallery: https://docs.mapbox.com/mapbox-gl-js/examples/
- GitHub Repository: https://github.com/mapbox/mapbox-gl-js
- Contributing Guide: CONTRIBUTING.md
Dependency Management
Mapbox GL JS uses npm for dependency management. The core dependencies are listed in package.json18-46 and developer dependencies in package.json48-122
Key dependencies include:
@mapbox/vector-tile: Vector tile decodinggeojson-vt: GeoJSON vector tile processingearcut: Polygon triangulation for WebGL renderinggl-matrix: Matrix math operations for WebGLsupercluster: Point clustering algorithm
The TypeScript type definitions are also included as dependencies to ensure type safety.
Using the Style Specification Tools
The Mapbox GL Style Specification is published as a separate npm package (@mapbox/mapbox-gl-style-spec) that includes several command-line utilities:
| Tool | Description |
|---|---|
gl-style-validate | Validate a style against the specification |
gl-style-format | Format a style to make it more readable |
gl-style-migrate | Migrate a style from an older version |
gl-style-composite | Composite multiple styles together |
These tools are defined in src/style-spec/package.json39-44 and can be installed via npm or used directly from the repository.