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

Cannot include Moment.js and Chart.js with RequireJS separately #4987

Closed
daniel-evers opened this issue Nov 23, 2017 · 3 comments
Closed

Cannot include Moment.js and Chart.js with RequireJS separately #4987

daniel-evers opened this issue Nov 23, 2017 · 3 comments

Comments

@daniel-evers
Copy link

Expected Behavior

To be able to include Moment.js and Chart.js with RequireJS without using the bundle version of Chart.js to avoid that Moment.js has to be included twice and possibly in different versions.

Current Behavior

The example at https://jsfiddle.net/8m67zr8y/1/ shows that Chart.js cannot find Moment.js, although Moment.js is basically available.

The example at https://jsfiddle.net/8m67zr8y/ shows the current possibility. However, Moment.js is included twice in this case.

Possible Solution

Provide a configuration option to pass a reference to the Moment.js instance explicitly or try to include Moment.js in an AMD environment with require('moment') for example.

Steps to Reproduce (for bugs)

See examples at https://jsfiddle.net/8m67zr8y/ and https://jsfiddle.net/8m67zr8y/1/

@jcopperfield
Copy link
Contributor

jcopperfield commented Nov 23, 2017

The problem is that the moment.js library doesn't expose the global window.moment function.
Adding the following will make it work. https://jsfiddle.net/8m67zr8y/3/

define('moment-adapter', ['moment'], function(moment) {
    // Set the global.
    window.moment = moment;
    return moment;
});

require(['jquery', 'moment-adapter', 'chartjs'], function($, moment, Chart) {

source: requirejs/requirejs#1554

@daniel-evers
Copy link
Author

So simple, so good. The problem is solved. But is there no way without the global Moment.js function? In the AMD context this seems a bit "dirty" from my point of view.

@jcopperfield
Copy link
Contributor

At the moment I think this is your only option using requirejs. Until Chart.js acquires the possibility of a pluggable time library. For now it is hard coded: scales/scale.time.js#L5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants