diff --git a/rollup/bundle_prelude.js b/rollup/bundle_prelude.js index 50fe63085e3..4787f64f4d0 100644 --- a/rollup/bundle_prelude.js +++ b/rollup/bundle_prelude.js @@ -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' })); + } } } diff --git a/src/util/browser.js b/src/util/browser.js index 710841b0d13..808a6fcc5c4 100755 --- a/src/util/browser.js +++ b/src/util/browser.js @@ -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 { diff --git a/src/util/browser/window.js b/src/util/browser/window.js index 7a525659dbb..d2ceea5613a 100644 --- a/src/util/browser/window.js +++ b/src/util/browser/window.js @@ -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); diff --git a/src/util/dom.js b/src/util/dom.js index fd5d3e91498..34e2af64f8b 100644 --- a/src/util/dom.js +++ b/src/util/dom.js @@ -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]; diff --git a/test/build/min.test.js b/test/build/min.test.js index 5cdcda50406..649a893274c 100644 --- a/test/build/min.test.js +++ b/test/build/min.test.js @@ -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, {