ng2-charts icon indicating copy to clipboard operation
ng2-charts copied to clipboard

Add way to remove moment from build

Open almgwary opened this issue 5 years ago • 12 comments

Moment js increase the bundle size, add option to remove it while build

Moment.js is installed along Chart.js as dependency. If you don't want to use Moment.js (either because you use a different date adapter or simply because don't need time functionalities), you will have to configure your bundler to exclude this dependency (e.g. using externals for Webpack or external for Rollup).

https://github.com/chartjs/Chart.js/blob/f2b099b835bf5d6dfe3a7d3097997ec11983c3ed/docs/getting-started/integration.md#L28

almgwary avatar May 06 '19 09:05 almgwary

@paviad check this

almgwary avatar May 06 '19 09:05 almgwary

I am not proficient in webpack, moment is indeed a part of chart.js therefore I have no easy way to get rid of it in ng2-charts...

paviad avatar May 06 '19 10:05 paviad

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

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

Or setting alias

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

almgwary avatar May 06 '19 11:05 almgwary

But I do not include chart.js as a dependency but as a peer dependency - so it is up to the user to include all of chart.js or just the bare package without the bundle.

paviad avatar May 06 '19 12:05 paviad

But I do not include chart.js as a dependency but as a peer dependency - so it is up to the user to include all of chart.js or just the bare package without the bundle.

you should change the chart.js import at base-chart.directive to be like this

import * as chartJs from 'chart.js/dist/Chart.bundle.js'

this should solve the problem

almgwary avatar May 08 '19 07:05 almgwary

Any update on this? This can save 574.32 Kb. With chart.js 2.8 it should now be possible. https://github.com/chartjs/Chart.js/pull/5960

I would also like to know if we can also remove lodash.js dependency?

naveedahmed1 avatar Aug 29 '19 18:08 naveedahmed1

I'm also interested about any update. I'm in the same situation with naveedahmed1.

M-Blobby avatar Aug 30 '19 08:08 M-Blobby

Hello,

Any updates on this?

AceVentura avatar Aug 18 '20 15:08 AceVentura

Hello,

Any updates on this?

Copy the base directive in your project and apply the following update

https://github.com/valor-software/ng2-charts/issues/1108#issuecomment-490383242

almgwary avatar Aug 19 '20 16:08 almgwary

Hello, Any updates on this?

Copy the base directive in your project and apply the following update

#1108 (comment)

That would require me to copy the code from github. I'm using the npm package

AceVentura avatar Aug 21 '20 09:08 AceVentura

Wait, what is wrong with this import in base-chart.directive.ts:

import {
  Chart,
  ChartConfiguration,
  ChartDataSets,
  ChartOptions,
  ChartPoint, ChartType,
  PluginServiceGlobalRegistration,
  PluginServiceRegistrationOptions,
  pluginService
} from 'chart.js';

paviad avatar Aug 26 '20 16:08 paviad

You can use a custom webpack configuration to mark moment as an external package. This can be accomplished by installing/configuring the @angular-builders/custom-webpack package and then including this custom webpack config:

module.exports = {
  externals: {
    moment: 'moment',
  },
}

AlecDusheck avatar Jan 06 '21 04:01 AlecDusheck

fixed in v3+

santam85 avatar Mar 01 '24 16:03 santam85