Skip to content

Commit

Permalink
Conditionally require hammer.js (visgl#658)
Browse files Browse the repository at this point in the history
* Conditionally require hammer.js to avoid failure on server, on `window` and `document` references.

* Use `isBrowser` for conditional requires
Manually hoist ManagerMock ;)
  • Loading branch information
ericsoco authored and Lezhi Li committed May 31, 2017
1 parent 3292ed1 commit e74fe28
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
27 changes: 22 additions & 5 deletions src/utils/events/event-manager.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
import {Manager} from 'hammerjs';
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
};
}

import {
const Manager = isBrowser ? require('hammerjs').Manager : ManagerMock;
const {
BASIC_EVENT_ALIASES,
EVENT_RECOGNIZER_MAP,
RECOGNIZERS,
GESTURE_EVENT_ALIASES
} from './constants';
import WheelInput from './wheel-input';
import MoveInput from './move-input';
} = isBrowser ? require('./constants') : {};

/**
* Single API for subscribing to events about both
Expand Down
3 changes: 2 additions & 1 deletion src/utils/events/wheel-input.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit e74fe28

Please sign in to comment.