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

Avoid errors when requiring the browser bundle in Node #9749

Merged
merged 2 commits into from
Jun 3, 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
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