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

Add support for using third party worker-loader plugins #10219

Merged
merged 7 commits into from
Dec 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ workflows:
filters:
tags:
only: /.*/
- test-webpack:
requires:
- build
filters:
tags:
only: /.*/
- deploy-benchmarks:
requires:
- lint
Expand Down Expand Up @@ -200,6 +206,21 @@ jobs:
- store_artifacts:
path: "test/integration/query-tests/index.html"

test-webpack:
<<: *defaults
steps:
- attach_workspace:
at: .
- run:
name: Build Webpack
command: |
cd ./test/build/transpilation &&
npm install &&
npm run build &&
rm -rf node_modules
- store_artifacts:
path: "test/build/transpilation"

test-browser:
<<: *defaults
steps:
Expand Down
16 changes: 16 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,22 @@ const exported = {

workerUrl: '',

/**
* Provides an interface for external module bundlers such as Webpack or Rollup to package
* mapbox-gl's WebWorker into a separate class and integrate it with the library.
*
* Takes precedence over `mapboxgl.workerUrl`.
*
* @var {Object} workerClass
* @returns {Object|null} a Class object, an instance of which exposes the `Worker` interface.
* @example
* import mapboxgl from 'mapbox-gl/dist/mapbox-gl-csp.js'
* import MapboxGLWorker from 'mapbox-gl/dist/mapbox-gl-csp-worker.js'
*
* mapboxgl.workerClass = MapboxGLWorker;
*/
workerClass: null,

/**
* Sets the time used by GL JS internally for all animations. Useful for generating videos from GL JS.
* @var {number} time
Expand Down
2 changes: 1 addition & 1 deletion src/util/browser/web_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import mapboxgl from '../../';
import type {WorkerInterface} from '../web_worker';

export default function (): WorkerInterface {
return (new window.Worker(mapboxgl.workerUrl): any);
return (mapboxgl.workerClass != null) ? new mapboxgl.workerClass() : (new window.Worker(mapboxgl.workerUrl): any); // eslint-disable-line new-cap
}
13 changes: 13 additions & 0 deletions test/build/transpilation/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<html>
<head>
<link rel="stylesheet" href="../../../dist/mapbox-gl.css">
<style>
#map {
width: 100%;
height: 100%
}
</style>
<div id = "map"></div>
<script src="./dist/bundle.js"></script>
</head>
</html>
14 changes: 14 additions & 0 deletions test/build/transpilation/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* eslint-disable */

import mapboxgl from '../../../dist/mapbox-gl-csp.js';
import MapboxGLWorker from '../../../dist/mapbox-gl-csp-worker.js';

mapboxgl.accessToken = window.prompt("Enter access token");
mapboxgl.workerClass = MapboxGLWorker;

const map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v11', // stylesheet location
center: [-74.5, 40], // starting position [lng, lat]
zoom: 9 // starting zoom
});
20 changes: 20 additions & 0 deletions test/build/transpilation/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "gl-js-bundling-test",
"version": "0.0.1",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.12.10",
"@babel/preset-env": "^7.12.10",
"babel-loader": "^8.2.2",
"url-loader": "^4.1.1",
"webpack": "^5.10.0",
"webpack-cli": "^4.2.0",
"worker-loader": "^3.0.6"
}
}
31 changes: 31 additions & 0 deletions test/build/transpilation/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* eslint-disable */
const path = require('path');

module.exports = {
entry: './index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\bmapbox-gl-csp-worker.js\b/i,
use: {
loader: "worker-loader" ,
options: {
inline: 'no-fallback'
}
},
},
{
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
}
]
}
};