Search code examples
angulardependencieschart.jsmomentjsangular7

Angular Chart.js - Remove Moment.js as Dependency / Reduce Bundle Size


I am using Chart.js with Angualar 7.

But Chart.js adds about 450KB to my bundle size. Through other resources I found out, that Moment.js is the main factor for the big size.

"chart.js": {
      "version": "2.7.3",
      "resolved": "https://registry.npmjs.org/chart.js/-/chart.js-2.7.3.tgz",
      "integrity": "sha512-3+7k/DbR92m6BsMUYP6M0dMsMVZpMnwkUyNSAbqolHKsbIzH2Q4LWVEHHYq7v0fmEV8whXE0DrjANulw9j2K5g==",
      "requires": {
        "chartjs-color": "^2.1.0",
        "moment": "^2.10.2"
      }
    },

I want to know how to remove moment.js from the dependencies, so that bundle size will be reduced.

Mybe there is a way to only import what's I am really using from Chart.js?


Solution

  • You can import standalone version - not bundled one with the following line:

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

    Or setting alias in web.config:

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

    Or Install Angular ChartJS which does not have moment.js bundled:

    https://github.com/emn178/angular2-chartjs