Skip to content

Commit

Permalink
Setup moment to be pluggable
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed May 26, 2018
1 parent 25b7f41 commit 5b6118a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 2 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ function buildTask() {
}

var bundled = browserify('./src/chart.js', { standalone: 'Chart' })
.ignore('luxon')
.plugin(collapse)
.bundle()
.on('error', errorHandler)
Expand All @@ -121,6 +122,7 @@ function buildTask() {
.pipe(gulp.dest(outDir));

var nonBundled = browserify('./src/chart.js', { standalone: 'Chart' })
.ignore('luxon')
.ignore('moment')
.plugin(collapse)
.bundle()
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
"karma-firefox-launcher": "^1.0.1",
"karma-jasmine": "^1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"moment": "^2.10.2",
"luxon": "^1.2.1",
"merge-stream": "^1.0.1",
"pixelmatch": "^4.0.2",
"vinyl-source-stream": "^1.1.0",
Expand All @@ -51,7 +53,9 @@
"main": "Chart.js"
},
"dependencies": {
"chartjs-color": "^2.1.0",
"chartjs-color": "^2.1.0"
},
"peerDependencies": {
"moment": "^2.10.2"
}
}
18 changes: 16 additions & 2 deletions src/scales/scale.time.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
/* global window: false */
'use strict';

var moment = require('moment');
var moment;
try {
moment = require('moment');
} catch(e) {
}
moment = typeof moment === 'function' ? moment : window.moment;

var luxon;
try {
luxon = require('luxon');
} catch(e) {
}
luxon = typeof luxon === 'object' ? luxon : window.luxon;

var defaults = require('../core/core.defaults');
var helpers = require('../helpers/index');
var Scale = require('../core/core.scale');
Expand Down Expand Up @@ -492,9 +503,12 @@ module.exports = function() {

var TimeScale = Scale.extend({
initialize: function() {
if (!moment) {
if (!moment && !luxon) {
throw new Error('Chart.js - Moment.js could not be found! You must include it before Chart.js to use the time scale. Download at https://momentjs.com');
}
if (!moment) {
throw new Error('Chart.js - Moment.js could not be found! You must include it before Chart.js to use the time scale. Download at https://momentjs.com. Luxon support coming soon');
}

this.mergeTicksOptions();

Expand Down

0 comments on commit 5b6118a

Please sign in to comment.