Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

feat: add ESM support

Open dangreen opened this issue 3 years ago • 3 comments

Alternative of https://github.com/chartjs/Chart.js/pull/10479

More changes than expected, but still.

To import CommonJS modules in/from ESM project .cjs extension is required.

dangreen avatar Jul 28 '22 16:07 dangreen

I think this PR also resolves this issue https://github.com/chartjs/Chart.js/issues/9436

If we still want to support commonjs, cjs bundle from this PR has compatible with ESM exports:

Chart.helpers = {...helpers};
Chart._adapters = _adapters;
Chart.Animation = Animation;
Chart.Animations = Animations;
Chart.animator = animator;
Chart.controllers = registry.controllers.items;
Chart.DatasetController = DatasetController;
Chart.Element = Element;
Chart.elements = elements;
Chart.Interaction = Interaction;
Chart.layouts = layouts;
Chart.platforms = platforms;
Chart.Scale = Scale;
Chart.Ticks = Ticks;
Object.assign(Chart, controllers, scales, elements, plugins, platforms);
Chart.Chart = Chart;
module.exports = Chart;

@benmccann What do you think?

dangreen avatar Jul 29 '22 09:07 dangreen

I'd love to drop CJS and do ESM-only if possible

benmccann avatar Jul 29 '22 12:07 benmccann

This leads to another related change needed in docs: https://www.chartjs.org/docs/master/getting-started/integration.html Should probably have ES Modules at top, update the CommonJS part and what about the Require JS?

kurkle avatar Aug 03 '22 16:08 kurkle

@kurkle Done.

dangreen avatar Aug 03 '22 21:08 dangreen

I will resolve conflicts later today

dangreen avatar Aug 04 '22 13:08 dangreen

Should we keep the unminified UMD build? It might help people if they encounter an error in the minified build or otherwise want to debug. Perhaps we should have a chart.umd.js and chart.umd.min.js?

benmccann avatar Aug 04 '22 15:08 benmccann

@benmccann maybe just emit sourcemaps for UMD bundle ?

dangreen avatar Aug 04 '22 15:08 dangreen