Skip to content

Export plugin as an ESM compatible module#222

Merged
simonbrunel merged 10 commits into
chartjs:chartjs-nextfrom
santam85:chartjs-next
Mar 9, 2021
Merged

Export plugin as an ESM compatible module#222
simonbrunel merged 10 commits into
chartjs:chartjs-nextfrom
santam85:chartjs-next

Conversation

@santam85

Copy link
Copy Markdown
Contributor

Avoids getting errors like:
Uncaught TypeError: helpers$1.merge is not a function
when loading the plugin as a module.

santam85 and others added 4 commits January 8, 2021 22:44
…labels into chartjs-next

# Conflicts:
#	docs/.vuepress/plugins/chart-editor/index.js
#	package-lock.json
#	package.json
#	test/utils.js
#	types/index.d.ts
@santam85 santam85 marked this pull request as draft February 11, 2021 11:49
@santam85 santam85 marked this pull request as ready for review February 11, 2021 14:45
@santam85

Copy link
Copy Markdown
Contributor Author

@simonbrunel if you like this PR, and decide to merge it, could you consider releasing the target branch under the next label, in order to enable users of ng2-charts to start testing a v3.0 compatible beta?

@simonbrunel simonbrunel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@santam85 Thanks you for your help in migrating this plugin.

I can't guarantee that I will release a beta right after but I will try to do my best to have it released soon. The documentation will also need to be updated to reflect Chart.js 3 changes about registering plugins.

Comment thread docs/.vuepress/plugins/chart-editor/components/ChartEditor.vue Outdated
Comment thread docs/.vuepress/plugins/chart-editor/components/ChartEditor.vue Outdated
Comment thread docs/.vuepress/plugins/chart-editor/components/ChartEditor.vue Outdated
Comment thread docs/.vuepress/plugins/chart-editor/utils.js
Comment thread docs/.vuepress/plugins/chart-editor/utils.js Outdated
Comment thread src/plugin.esm.js Outdated
Comment thread src/plugin.js
Comment thread src/plugin.js Outdated
Comment thread test/specs/events.spec.js
Comment thread package.json
@santam85

Copy link
Copy Markdown
Contributor Author

@simonbrunel latest beta broke several tests, any clue what the cause might be? Can't link these errors to any of the listed breaking changes...

@kurkle

kurkle commented Feb 22, 2021

Copy link
Copy Markdown
Member

@santam85 its mostly the proxied options that default to scriptable being true, and thus tries to resolve the event handlers etc.

Adding something like that to the plugin should help (from #221):

  descriptors: {
    _scriptable: function(name) {
      return !['formatter', 'enter', 'leave', 'click'].includes(name);
    },
  },

@simonbrunel

simonbrunel commented Feb 22, 2021

Copy link
Copy Markdown
Member

@santam85 This plugin doesn't rely on proxied options (and I'm not sure I would like to) so it's probably better to fully disable this feature for now (_scriptable: false if supported) and keep the explicit resolve calls.

@kurkle Shouldn't proxied options for plugins default to false? The current behavior seems quite error prone and hard to debug and will probably break a lot of existing plugins.

@kurkle

kurkle commented Feb 22, 2021

Copy link
Copy Markdown
Member

@simonbrunel yes, it should default to false for plugins.

@kurkle

kurkle commented Feb 22, 2021

Copy link
Copy Markdown
Member

So, to make this work with beta.11 you'll need to add this:

  descriptors: {
    _scriptable: false,
    listeners: {
      _scriptable: false
    }
  },

And this should not be needed after next release.

Comment thread src/plugin.js
Comment thread src/plugin.js Outdated
simonbrunel
simonbrunel previously approved these changes Feb 23, 2021
@simonbrunel simonbrunel dismissed their stale review February 23, 2021 16:24

Some docs samples are broken, this needs to be investigated before merging.

@simonbrunel

simonbrunel commented Feb 23, 2021

Copy link
Copy Markdown
Member

@santam85 A few samples are broken compared to the chartjs-next branch:

Comment thread docs/.vuepress/plugins/chart-editor/components/ChartEditor.vue
@santam85 santam85 changed the title Updated imports to make library compatible with esm Export plugin as an ESM compatible module Feb 24, 2021

@kurkle kurkle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the samples issue persists, I'll take a closer look at it.

Comment thread types/index.d.ts

declare module 'chart.js' {
interface ChartDatasetProperties<TType extends ChartType, TData extends unknown[]> {
interface ChartDatasetProperties<TType extends ChartType, TData> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably an issue with the types in beta.12

@simonbrunel simonbrunel Mar 1, 2021

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the generic base type is still needed, maybe the core could export a nice alias:

// chart.js
export type AnyArray = unknown[];
// chart-plugin-datalabels
import { AnyArray } from 'chart.js';

declare module 'chart.js' {
  interface ChartDatasetProperties<TType extends ChartType, TData extends AnyArray> {
    //...
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could even be AnyData instead of AnyArray.

@santam85 santam85 Mar 1, 2021

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am just matching the definition here to prevent errors. If only array data-types are needed, would TData extends Array<any> work? Or even ChartDatasetProperties<TType extends ChartType, Array<any>>?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it needs to be the exact same definition as in the core repo, so it would not work. Also, I disabled the use of any and enforce array type consistency in this project, so it would need to be unknown[].

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@santam85 I think I misunderstood your previous comment. Are you suggesting to use Array<any> in the core repo?

If so, I would not go that way because any isn't good for typing, unknown is better because it forces a check of the actual type before using it. Using an alias such as AnyData would allow to extend this data type later (if needed) without breaking existing integrations (e.g. type AnyData = AnyArray | AnyObject) or type AnyData = (number | string | DataObject)[], etc.).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I meant in the core repo. Sounds good, will adjust accordingly once decided.

@santam85 santam85 requested a review from simonbrunel March 9, 2021 11:44
@simonbrunel

Copy link
Copy Markdown
Member

Thanks @santam85.

Samples are still broken: legend and tooltip should not be visible. It looks like an issue in the core lib where registering a plugin (and probably other types) doesn't preserve existing defaults:

import {Chart, registrables} from 'chart.js';

Chart.defaults.set({
  plugin: {
    legend: {
      display: false;
    }
  }
})

Chart.register(...registrables);

// Chart.defaults.plugin.legend.display === true

I will see how to fix the docs after merging this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants