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

Improve resolving optional dependencies. #770

Merged
merged 2 commits into from
Feb 5, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/choroplethFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var choroplethFeature = function (arg) {
/**
* @private
*/
var d3 = require('d3'),
var d3 = require('./d3/d3Renderer').d3,
m_this = this,
s_init = this._init,
m_choropleth = $.extend({},
Expand Down
15 changes: 9 additions & 6 deletions src/d3/d3Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var renderer = require('../renderer');
var d3Renderer = function (arg) {
'use strict';

var d3 = require('d3');
var d3 = d3Renderer.d3;
var object = require('./object');
var util = require('../util');
var geo_event = require('../event');
Expand Down Expand Up @@ -676,11 +676,12 @@ registerRenderer('d3', d3Renderer);
* @returns {boolean} true if available.
*/
d3Renderer.supported = function () {
if (!__webpack_modules__[require.resolveWeak('d3')]) { // eslint-disable-line
return false;
}
var d3 = require('d3');
return d3 !== undefined;
delete d3Renderer.d3;
// webpack expects optional dependencies to be wrapped in a try-catch
try {
d3Renderer.d3 = require('d3');
} catch (_error) {}
return d3Renderer.d3 !== undefined;
};

/**
Expand All @@ -692,6 +693,8 @@ registerRenderer('d3', d3Renderer);
d3Renderer.fallback = function () {
return null;
};

d3Renderer.supported(); // cache reference to d3 if it is available
})();

module.exports = d3Renderer;
2 changes: 1 addition & 1 deletion src/d3/lineFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var d3_lineFeature = function (arg) {
return new d3_lineFeature(arg);
}

var d3 = require('d3');
var d3 = require('./d3Renderer').d3;
var object = require('./object');
var timestamp = require('../timestamp');
var util = require('../util');
Expand Down
2 changes: 1 addition & 1 deletion src/d3/pathFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var d3_pathFeature = function (arg) {
}

var $ = require('jquery');
var d3 = require('d3');
var d3 = require('./d3Renderer').d3;
var object = require('./object');
var timestamp = require('../timestamp');

Expand Down
2 changes: 1 addition & 1 deletion src/d3/quadFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var d3_quadFeature = function (arg) {
}

var $ = require('jquery');
var d3 = require('d3');
var d3 = require('./d3Renderer').d3;
var object = require('./object');

quadFeature.call(this, arg);
Expand Down
2 changes: 1 addition & 1 deletion src/d3/vectorFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var d3_vectorFeature = function (arg) {

var object = require('./object');
var timestamp = require('../timestamp');
var d3 = require('d3');
var d3 = require('./d3Renderer').d3;

arg = arg || {};
vectorFeature.call(this, arg);
Expand Down
10 changes: 6 additions & 4 deletions src/mapInteractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -703,12 +703,14 @@ var mapInteractor = function (args) {
}
$node.toggleClass('highlight-focus',
!!(m_boundKeys && m_boundKeys.length && m_options.keyboard.focusHighlight));

// bind touch events
if ((m_this.hasTouchSupport() || m_options.alwaysTouch) &&
(usedInputs.pan || usedInputs.rotate) &&
__webpack_modules__[require.resolveWeak('hammerjs')]) { // eslint-disable-line
var Hammer = require('hammerjs');
(usedInputs.pan || usedInputs.rotate)) {
// webpack expects optional dependencies to be wrapped in a try-catch
var Hammer;
try {
Hammer = require('hammerjs');
} catch (_error) {}
if (Hammer !== undefined) {
var recog = [],
touchEvents = ['hammer.input'];
Expand Down
2 changes: 1 addition & 1 deletion src/ui/colorLegendWidget.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var d3 = require('d3');
var d3 = require('../d3/d3Renderer').d3;
var domWidget = require('./domWidget');
var inherit = require('../inherit');
var registerWidget = require('../registry').registerWidget;
Expand Down
2 changes: 1 addition & 1 deletion src/ui/legendWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var legendWidget = function (arg) {
}
svgWidget.call(this, arg);

var d3 = require('d3');
var d3 = require('../d3/d3Renderer').d3;
var geo_event = require('../event');

/** @private */
Expand Down
2 changes: 1 addition & 1 deletion src/ui/sliderWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var sliderWidget = function (arg) {
}
svgWidget.call(this, arg);

var d3 = require('d3');
var d3 = require('../d3/d3Renderer').d3;
var geo_event = require('../event');

var m_this = this,
Expand Down
2 changes: 2 additions & 0 deletions tests/cases/mapInteractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -1857,6 +1857,7 @@ describe('Optional Dependencies', function () {
it('test missing Hammer library', function () {
var old = __webpack_modules__[require.resolveWeak('hammerjs')]; // eslint-disable-line
__webpack_modules__[require.resolveWeak('hammerjs')] = null; // eslint-disable-line
delete __webpack_require__.c[require.resolveWeak('hammerjs')]; // eslint-disable-line
var map = geo.map({node: '#mapNode1'}),
interactor = map.interactor();

Expand All @@ -1871,5 +1872,6 @@ describe('Optional Dependencies', function () {
'panend', {touch: true, center: {x: 40, y: 20}});
expect(map.center().x).toBeCloseTo(0);
__webpack_modules__[require.resolveWeak('hammerjs')] = old; // eslint-disable-line
delete __webpack_require__.c[require.resolveWeak('hammerjs')]; // eslint-disable-line
});
});
2 changes: 2 additions & 0 deletions tests/cases/renderers.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ describe('renderers', function () {

var oldd3 = __webpack_modules__[require.resolveWeak('d3')]; // eslint-disable-line
__webpack_modules__[require.resolveWeak('d3')] = null; // eslint-disable-line
delete __webpack_require__.c[require.resolveWeak('d3')]; // eslint-disable-line
expect(geo.checkRenderer('d3')).toBe(null);
__webpack_modules__[require.resolveWeak('d3')] = oldd3; // eslint-disable-line
delete __webpack_require__.c[require.resolveWeak('d3')]; // eslint-disable-line
expect(geo.checkRenderer('d3')).toBe('d3');

mockVGLRenderer(false);
Expand Down