Skip to content

Commit

Permalink
Avoid errors when requiring the browser bundle in Node (#9749)
Browse files Browse the repository at this point in the history
* avoid errors when requiring the browser bundle in Node

* add a test for bundle evaluation in Node
  • Loading branch information
mourner authored Jun 3, 2020
1 parent 4e15254 commit 536513d
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 4 deletions.
4 changes: 3 additions & 1 deletion rollup/bundle_prelude.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ if (!shared) {
var sharedChunk = {};
shared(sharedChunk);
mapboxgl = chunk(sharedChunk);
mapboxgl.workerUrl = window.URL.createObjectURL(new Blob([workerBundleString], { type: 'text/javascript' }));
if (typeof window !== 'undefined') {
mapboxgl.workerUrl = window.URL.createObjectURL(new Blob([workerBundleString], { type: 'text/javascript' }));
}
}
}
2 changes: 1 addition & 1 deletion src/util/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const exported = {
return linkEl.href;
},

hardwareConcurrency: window.navigator.hardwareConcurrency || 4,
hardwareConcurrency: window.navigator && window.navigator.hardwareConcurrency || 4,

get devicePixelRatio() { return window.devicePixelRatio; },
get prefersReducedMotion(): boolean {
Expand Down
3 changes: 2 additions & 1 deletion src/util/browser/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
/* eslint-env browser */
import type {Window} from '../../types/window';

export default (self: Window);
// shim window for the case of requiring the browser bundle in Node
export default typeof self !== 'undefined' ? (self: Window) : (({}: any): Window);
2 changes: 1 addition & 1 deletion src/util/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ DOM.createNS = function (namespaceURI: string, tagName: string) {
return el;
};

const docStyle = window.document.documentElement.style;
const docStyle = window.document && window.document.documentElement.style;

function testProp(props) {
if (!docStyle) return props[0];
Expand Down
5 changes: 5 additions & 0 deletions test/build/min.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ test('can be browserified', (t) => {
});
});

test('evaluates without errors', (t) => {
t.doesNotThrow(() => require(path.join(__dirname, '../../dist/mapbox-gl.js')));
t.end();
});

test('distributed in plain ES5 code', (t) => {
const linter = new Linter();
const messages = linter.verify(minBundle, {
Expand Down

0 comments on commit 536513d

Please sign in to comment.