Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ChartJs] Replace chart.js/auto import with Chart.register calls #1263

Merged
merged 1 commit into from
May 10, 2024

Conversation

smnandre
Copy link
Member

@smnandre smnandre commented Nov 9, 2023

Q A
Bug fix? yes
New feature? no
Issues Fix #1262
License MIT

Ongoing attempt to fix ChartJS problems

}
},
"peerDependencies": {
"@hotwired/stimulus": "^3.0.0",
"chart.js": "^3.4.1 <3.9"
"chart.js": "^3.9"
Copy link
Member

Choose a reason for hiding this comment

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

For others looking at this, we can absolutely allow v4 right now. But we should do it in a minor release, not a patch release.

@weaverryan
Copy link
Member

Background info: Chart.js changed some things starting in 3.9, which is why we previously restricted that version (they were arguably BC breaks).

In chart.js 2.13.0, we changed the JS package shipped to type: module. That had an unexpected side effect of no longer loading the chart.js/auto import (even though it loads fine in our test). Tbh, the reasons are a bit of a mystery to me.

Anyway, here is the breakdown of the fix:

A) For Encore users, you will need to upgrade chart.js to ^3.9. You may currently have, for example, 3.8.0, so upgrading to 3.9.* is fine - just a minor update.
B) For AssetMapper users, you will need to importmap:require 'chart.js' (and you no longer need chart.js/auto). Flex may actually do this automatically - I think it will, but we'll need to try it :)

@smnandre
Copy link
Member Author

smnandre commented Nov 9, 2023

About the typescript error :
Capture d’écran 2023-11-09 à 15 11 08

I think 4.0 was release the other day and since Chart.js v3 it's been rewritten with Typescript, so you don't need these typedefinitions anymore.

DefinitelyTyped/DefinitelyTyped#63641 (comment)

Just removed the file... let's see

@weaverryan
Copy link
Member

I'm checking into this failure. What's being imported in the tests is different than what's imported in Encore... which is why our original tests didn't catch the bug.

@smnandre
Copy link
Member Author

smnandre commented Nov 9, 2023

So we could release this fix, and check all this module/ES/node/2015/6/beta/tango/charlie later ? Or is there a risk ?

weaverryan added a commit that referenced this pull request Nov 9, 2023
This PR was merged into the 2.x branch.

Discussion
----------

[Chart.js] Reverting chart.js type: module

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| Issues        | Fix #1262
| License       | MIT

This is complex, and relates to some hard/weird/broken changes that chart.js made in 3.9. Facts:

* Changing `type: module` and making no other changes breaks `chart.js` v3 (but v4 works fine)
* Changing the import from `chart.js/auto'` to `chart.js` - like #1263 fixes things. However, it breaks the tests in 3.9... because chart.js doesn't have an `exports` key in that version, so our tests / node environment continue to load the `main` key instead of the `module` key.

So, this will "put thing back" and get chart.js working again.

After this, we should:

1) Re-add `type: module` to chart.js
2) Upgrade `chart.js` to v4

By doing that, other than actually migrating any custom chart.js code from 3 -> 4 (https://www.chartjs.org/docs/latest/migration/v4-migration.html), users won't need to make any other changes.

The question about 3 -> 4 is if we can do that in a minor UX version of it needs to be a major. Technically, nothing in `symfony/ux-chart.js` changes... except that it will force you to change a JS dep up a major version. It's a gray area.

Cheers!

Commits
-------

3fca0ca [Chart.js] Reverting chart.js type: module
@weaverryan
Copy link
Member

Fixed in #1264. However, we can make this PR the proper PR for the NEXT version, which would include:

  • Re-adding type: module
  • Upgrading chart.js to v4

@smnandre
Copy link
Member Author

smnandre commented Nov 9, 2023

I did not realize Chart.js 4.0 was released 12 months ago.

That could itself be a good argument to push gently users towards an upgrade :)

@smnandre smnandre force-pushed the fix/chartjs-encore branch from 63c757d to fd99323 Compare April 14, 2024 21:35
@smnandre smnandre changed the title Fix Chart.js encore compilation [ChartJs] Replace chart.js/auto import with Chart.register calls Apr 14, 2024
@smnandre
Copy link
Member Author

smnandre commented Apr 14, 2024

I rebased and tried to clean this PR i opened a long time ago, but i always end with test failures somewhere...

Could you guys @evertharmeling @simondaigre maybe help me there ?

(cc issue #1695 )


Chart.register(...registerables);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Chart.register(...registerables);
registerables.forEach(registerable => Chart.register(registerable));

https://stackoverflow.com/questions/75449457/typeerror-chart-registerables-is-not-iterable-cannot-read-property-undefined

Copy link
Contributor

Choose a reason for hiding this comment

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

I came across this one too, however we still need to verfify registerables != undefined as with this code we would still get the following error on 3.x:

TypeError: Cannot read properties of undefined (reading 'forEach')

Copy link
Member Author

Choose a reason for hiding this comment

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

And i still think it should not be "undefined" :))

Copy link
Contributor

Choose a reason for hiding this comment

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

https://github.com/evertharmeling/ux-1263

This seems to backup that it is our testsuite that's messing with us. I've added a console log on the registerables (symfony/ux). And when using [email protected] the registerables variable is NOT undefined.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok so its a « module » thing we need to force on the tests… that narrows the problem !

thanks a lot for taking this time it really helps a lot :)

@simondaigre
Copy link
Contributor

@smnandre Do you need some help to finish the PR ?

@smnandre
Copy link
Member Author

smnandre commented May 8, 2024

@smnandre Do you need some help to finish the PR ?

That'd be really nice, yes :)

@simondaigre
Copy link
Contributor

simondaigre commented May 8, 2024

I also checked and :

So there is two solutions:

  1. We stick chart.js to 3.x OR 4.x (BC break ?)
  2. Solution provided by @evertharmeling works and as the issue is only related to our Vitest test suite, I think we can use it

@smnandre
Copy link
Member Author

smnandre commented May 8, 2024

@simondaigre thank you so much for taking this time, really 🙇

Let's say we take the solution 2, is there any way to still check 3.x on the CI with no "false failures" ?

@simondaigre
Copy link
Contributor

I have no failures on CI with 2/ (for both 3.x and 4.x).

@smnandre smnandre force-pushed the fix/chartjs-encore branch from 0f94046 to 09135eb Compare May 9, 2024 00:29
@smnandre smnandre force-pushed the fix/chartjs-encore branch from 7131c0a to addc1f8 Compare May 9, 2024 00:32
@smnandre
Copy link
Member Author

smnandre commented May 9, 2024

I have no failures on CI with 2/ (for both 3.x and 4.x).

So you'd merge the PR ?

(i updated to take the @evertharmeling suggestion)

@simondaigre
Copy link
Contributor

simondaigre commented May 9, 2024

I checked on a real project with both chart.js 3.x and 4.x, it's OK for me: no breaks and it fixes my issue.
(changes to lockfiles seems unrelated to this PR I think).

@smnandre
Copy link
Member Author

Well let's merge it and test one last time on the dev branch before the next release ! :)

(@kbond if you can ?)

A massive thank to you guys @simondaigre @evertharmeling !! 🙇

@kbond
Copy link
Member

kbond commented May 10, 2024

Thanks all!

@kbond kbond merged commit 796dd58 into symfony:2.x May 10, 2024
35 of 36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Chart.js] Build fails with latest 2.13.0 release
5 participants