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

泄露秘密 提交于 2021-01-28 02:06:40

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/54111163/angular-chart-js-remove-moment-js-as-dependency-reduce-bundle-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!