Chart.js: Import Chart.js without moment

Created on 5 Feb 2018  路  11Comments  路  Source: chartjs/Chart.js

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
<!--- Not obligatory, but suggest a fix/reason for the bug, -->
<!--- or ideas how to implement the addition or change -->
Multiple entry points.

```js
import Chart from 'chart.js' // grabs standalone
import Chart from 'chart.js/bundled' // grabs bundled

enhancement

Most helpful comment

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

All 11 comments

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

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.

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/

@husayt

Well that has nothing to do with the issue :D

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

+1

@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 https://github.com/chartjs/Chart.js/issues/4303

Closing as duplicate of https://github.com/chartjs/Chart.js/issues/4303 which was fixed today

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.

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.

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

benmccann picture benmccann  路  3Comments

adriantombu picture adriantombu  路  3Comments

joebirkin picture joebirkin  路  3Comments

lizbanach picture lizbanach  路  3Comments

srijitcoder picture srijitcoder  路  3Comments