From ba70cd94d67b6d81a6dd9bea5e8b164495231114 Mon Sep 17 00:00:00 2001 From: Eric Socolofsky Date: Tue, 23 May 2017 22:29:56 -0700 Subject: [PATCH 1/2] Conditionally require hammer.js to avoid failure on server, on `window` and `document` references. --- src/utils/events/event-manager.js | 22 +++++++++++++++++----- src/utils/events/wheel-input.js | 3 ++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/utils/events/event-manager.js b/src/utils/events/event-manager.js index c4c0a2f84a7..11410ba4590 100644 --- a/src/utils/events/event-manager.js +++ b/src/utils/events/event-manager.js @@ -1,13 +1,15 @@ -import {Manager} from 'hammerjs'; +import WheelInput from './wheel-input'; +import MoveInput from './move-input'; -import { +const Manager = (typeof window === 'undefined' || typeof document === 'undefined') ? + ManagerMock : require('hammerjs').Manager; +const { BASIC_EVENT_ALIASES, EVENT_RECOGNIZER_MAP, RECOGNIZERS, GESTURE_EVENT_ALIASES -} from './constants'; -import WheelInput from './wheel-input'; -import MoveInput from './move-input'; +} = (typeof window === 'undefined' || typeof document === 'undefined') ? + {} : require('./constants'); /** * Single API for subscribing to events about both @@ -149,3 +151,13 @@ export default class EventManager { return event => this.manager.emit(eventAlias, event); } } + +function ManagerMock(m) { + const noop = () => {}; + return { + on: noop, + off: noop, + destroy: noop, + emit: noop + }; +} diff --git a/src/utils/events/wheel-input.js b/src/utils/events/wheel-input.js index d2905aa4d7d..2b2449c2510 100644 --- a/src/utils/events/wheel-input.js +++ b/src/utils/events/wheel-input.js @@ -1,4 +1,5 @@ -/* global window:false */ +import window from '../../lib/utils/globals'; + const ua = typeof window.navigator !== 'undefined' ? window.navigator.userAgent.toLowerCase() : ''; const firefox = ua.indexOf('firefox') !== -1; From d2ee12aeda47b4b0ab144041e367709db05593b4 Mon Sep 17 00:00:00 2001 From: Eric Socolofsky Date: Wed, 24 May 2017 10:37:57 -0700 Subject: [PATCH 2/2] Use `isBrowser` for conditional requires Manually hoist ManagerMock ;) --- src/utils/events/event-manager.js | 33 ++++++++++++++++++------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/utils/events/event-manager.js b/src/utils/events/event-manager.js index 11410ba4590..e00c118eb9c 100644 --- a/src/utils/events/event-manager.js +++ b/src/utils/events/event-manager.js @@ -1,15 +1,30 @@ import WheelInput from './wheel-input'; import MoveInput from './move-input'; +import {isBrowser} from '../../controllers/globals'; + +// Hammer.js directly references `document` and `window`, +// which means that importing it in environments without +// those objects throws errors. Therefore, instead of +// directly `import`ing 'hammerjs' and './constants' +// (which imports Hammer.js) we conditionally require it +// depending on support for those globals. +function ManagerMock(m) { + const noop = () => {}; + return { + on: noop, + off: noop, + destroy: noop, + emit: noop + }; +} -const Manager = (typeof window === 'undefined' || typeof document === 'undefined') ? - ManagerMock : require('hammerjs').Manager; +const Manager = isBrowser ? require('hammerjs').Manager : ManagerMock; const { BASIC_EVENT_ALIASES, EVENT_RECOGNIZER_MAP, RECOGNIZERS, GESTURE_EVENT_ALIASES -} = (typeof window === 'undefined' || typeof document === 'undefined') ? - {} : require('./constants'); +} = isBrowser ? require('./constants') : {}; /** * Single API for subscribing to events about both @@ -151,13 +166,3 @@ export default class EventManager { return event => this.manager.emit(eventAlias, event); } } - -function ManagerMock(m) { - const noop = () => {}; - return { - on: noop, - off: noop, - destroy: noop, - emit: noop - }; -}