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

Import Chart.js without moment #5235

Closed
apertureless opened this issue Feb 5, 2018 · 11 comments
Closed

Import Chart.js without moment #5235

apertureless opened this issue Feb 5, 2018 · 11 comments

Comments

@apertureless
Copy link
Contributor

Expected Behavior

If I import chart.js over import Chart from 'chart.js' it should grab the standalone version without moment.js and with import Chart from 'chart.js/dist/Chartjs.bundled.js' it should grab the bundled one.

Current Behavior

import Chart from 'chart.js'

grabs the bundled version.

Possible Solution

Multiple entry points.

import Chart from 'chart.js' // grabs standalone
import Chart from 'chart.js/bundled' // grabs bundled
@thepian
Copy link

thepian commented Feb 6, 2018

Is there a way to use Chart.js without Moment? It is quite a large dependency

@apertureless
Copy link
Contributor Author

apertureless commented Feb 6, 2018

Well you can import the standalone

import Chart from 'chart.js/dist/Chart.js'

Or you can set an alias in your webpack.config

resolve: {
    alias: {
      'chart.js': 'chart.js/dist/Chart.js'
    }
}

Then it would pick the standalone if you import Chart from 'chart.js'

However I think it should be the default behaviour to pick the standalone version without moment.

@husayt
Copy link

husayt commented Feb 6, 2018

Also, I don't think it needs all of momen.js. One can just import core moment with out locales and etc. See
https://momentjs.com/docs/#/use-it/require-js/

or if not try http://jmduke.com/posts/migrating-from-moment-to-date-fns/

@apertureless
Copy link
Contributor Author

@husayt

Well that has nothing to do with the issue :D

@codeofsumit
Copy link

it'd be great to have chartJS move from moment to date-fns though. It would be quite the file size reduction.

@shlima
Copy link

shlima commented Mar 2, 2018

+1

@apertureless
Copy link
Contributor Author

@codeofsumit yea it would. But again, thats not what the issue is about.
Its about the default entry.

There is another issue about removing moment #4303

@benmccann
Copy link
Contributor

Closing as duplicate of #4303 which was fixed today

@simonbrunel simonbrunel added this to the Version 2.8 milestone Jan 30, 2019
@simonbrunel
Copy link
Member

We changed the main entry for dist/Chart.js as part of #5978 .

Until 2.8, we used Browserify and the fact you was able to use dist/Chart.js without Moment.js in a Webpack environment was a bug due to a wrong config on our side. Since, we moved to Rollup which makes the UMD build cleaner but also fixed that bug. That means dist/Chart.js still depends on moment but now is optional (if the dependency is missing in your project, it will not fail).

However, the chart.js package still depends on moment (to prevent breaking changes) so in order to exclude moment from your Webpack build, you need to flag it as external. We agree it's not ideal, but moving moment as a peer dependency would break too many projects.

@apertureless 2.8 is not released yet and we would love to get feedback about these recent changes to get a chance to adjust our code in case we broke some use cases. Would you be able to test the current master against your vue-chartjs wrapper? Especially, the use case described in this ticket.

@giniedp
Copy link

giniedp commented Feb 22, 2019

Thats a bummer. Can we have moment as peer dependency somewhere in future?
For me moment is bundled twice, because i also use moment in my project. Have to add an alias for now.

// webpack.js
// ...
  resolve: {
    alias: {
      // avoids bundling moment twice in our bundle, since chart.js hat its own moment dependency
      // https://github.com/chartjs/Chart.js/issues/5235#issuecomment-458854521
      "moment": path.join(project.cwd, "node_modules", "moment", "moment.js")
    }
  },

besides that, this is a great library.

@benmccann
Copy link
Contributor

Yes, moment will stop being a dependency in v3, but that may be quite some ways out.

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

No branches or pull requests

9 participants