From 5b6118aaf703e9e1f5e57d27c9ff813cba982208 Mon Sep 17 00:00:00 2001 From: Ben McCann Date: Fri, 25 May 2018 21:05:11 -0700 Subject: [PATCH] Setup moment to be pluggable --- gulpfile.js | 2 ++ package.json | 6 +++++- src/scales/scale.time.js | 18 ++++++++++++++++-- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index 24c01665ae4..63f207cd6ec 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -107,6 +107,7 @@ function buildTask() { } var bundled = browserify('./src/chart.js', { standalone: 'Chart' }) + .ignore('luxon') .plugin(collapse) .bundle() .on('error', errorHandler) @@ -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() diff --git a/package.json b/package.json index a2e6c494af1..05554d0f0b2 100644 --- a/package.json +++ b/package.json @@ -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", @@ -51,7 +53,9 @@ "main": "Chart.js" }, "dependencies": { - "chartjs-color": "^2.1.0", + "chartjs-color": "^2.1.0" + }, + "peerDependencies": { "moment": "^2.10.2" } } diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index dfd708b2345..1cfb84281cd 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -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'); @@ -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();