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

Introduce component usage stats #96882

Open
wants to merge 7 commits into
base: trunk
Choose a base branch
from

Conversation

tyxla
Copy link
Member

@tyxla tyxla commented Nov 28, 2024

What?

This PR introduces react-scanner to keep an eye on component usage stats.

Why?

It can be useful to track the adoption of our components over time, and we're automating that as part of our work on design systems (bjpUB-p2).

See WordPress/gutenberg#65463 for the same in Gutenberg itelf.

How?

Running react-scanner with the default config to count components and props yields a result like this:

{
 "LinkPicker": {
    "instances": [
      {
        "importInfo": {
          "imported": "LinkPicker",
          "local": "LinkPicker",
          "moduleName": "@wordpress/components",
          "importType": "ImportSpecifier"
        },
        "props": {
          "value": "(Identifier)",
          "onLinkPicked": "(Identifier)",
          "onCancel": "(Identifier)"
        },
        "propsSpread": false,
        "location": {
          "file": "gutenberg/packages/format-library/src/link/modal-screens/link-picker-screen.native.js",
          "start": {
            "line": 52,
            "column": 4
          }
        }
      }
    ]
  },
  "CardHeader": {
    "instances": [
      {
        "importInfo": {
          "imported": "CardHeader",
          "local": "CardHeader",
          "moduleName": "@wordpress/components",
          "importType": "ImportSpecifier"
        },
        "props": {
          "isBorderless": false,
          "justify": "left",
          "size": "small",
          "gap": "6"
        },
        "propsSpread": false,
        "location": {
          "file": "gutenberg/packages/preferences/src/components/preferences-modal-tabs/index.js",
          "start": {
            "line": 142,
            "column": 10
          }
        }
      }
    ]
  }
}

Testing Instructions

  • Checkout this branch
  • Run yarn install
  • Run yarn run component-usage-stats
  • Check out the output

Testing Instructions for Keyboard

Same

Screenshots or screencast

None

@matticbot
Copy link
Contributor

This PR does not affect the size of JS and CSS bundles shipped to the user's browser.

Generated by performance advisor bot at iscalypsofastyet.com.

@matticbot
Copy link
Contributor

matticbot commented Nov 28, 2024

This PR modifies the release build for the following Calypso Apps:

For info about this notification, see here: PCYsg-OT6-p2

  • notifications

To test WordPress.com changes, run install-plugin.sh $pluginSlug add/react-scanner-component-stats on your sandbox.

@tyxla tyxla force-pushed the add/react-scanner-component-stats branch from 3738f8c to ebed2a8 Compare December 5, 2024 13:45
@@ -28,6 +28,7 @@ test-results*.xml
/client/chart.json
/client/style.json
*xunit_*.xml
/results
Copy link
Member Author

Choose a reason for hiding this comment

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

Ignoring the result dir.

We're going with the same naming choice as in the Gutenberg repo.

@@ -313,7 +315,7 @@
"stacktrace-gps": "^3.0.3",
"stylelint": "^16.8.2",
"tslib": "^2.3.0",
"typescript": "^5.3.3",
"typescript": "5.3.3",
Copy link
Member Author

Choose a reason for hiding this comment

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

React Scanner comes with a newer TS version, so I'm pinning the current one.

@@ -413,7 +415,8 @@
"@wordpress/viewport": "6.8.0",
"@wordpress/warning": "3.8.0",
"@wordpress/widgets": "4.8.0",
"@wordpress/wordcount": "4.8.0"
"@wordpress/wordcount": "4.8.0",
"typescript": "5.3.3"
Copy link
Member Author

Choose a reason for hiding this comment

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

In order for React Scanner to pick our TS version up, we need to specifically pin the resolution version here.

@tyxla tyxla marked this pull request as ready for review December 5, 2024 14:25
@tyxla tyxla requested review from mirka and ciampo December 5, 2024 14:25
@matticbot matticbot added [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. labels Dec 5, 2024
@tyxla tyxla requested a review from jsnajdr December 5, 2024 14:28
@ciampo
Copy link
Contributor

ciampo commented Dec 11, 2024

While the script generates a 32k lines results.json file, I'm getting some errors while running the command:

❯ yarn run component-usage-stats
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/client/a8c-for-agencies/sections/partner-directory/agency-details/hooks/use-country-list.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/client/a8c-for-agencies/sections/signup/agency-details-form/hooks/use-countries-and-states.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/client/components/search-sites/utils.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/client/data/plugins/helpers.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/client/jetpack-cloud/sections/partner-portal/company-details-form/hooks/use-countries-and-states.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/client/landing/stepper/constants.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/client/landing/stepper/utils/shuffle-array.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/client/landing/subscriptions/helpers/index.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/client/lib/promote-post/index.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/client/my-sites/email/form/mailboxes/components/utilities/get-cart-items.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/client/my-sites/plans/jetpack-plans/iterations.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/client/my-sites/stats/hooks/default-query-params.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/client/state/reader/recommended-sites/selectors/get-reader-recommended-sites.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/packages/calypso-products/src/constants/akismet.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/packages/calypso-products/src/constants/jetpack.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/packages/calypso-products/src/constants/terms.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/packages/calypso-products/src/constants/titan.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/packages/calypso-products/src/constants/types.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/packages/calypso-products/src/constants/wpcom.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/packages/calypso-products/src/types.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/packages/create-calypso-config/src/index.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/packages/data-stores/src/add-ons/constants.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/packages/domains-table/src/use-domain-row.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/packages/explat-client/src/internal/test-common.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/packages/js-utils/src/key-by.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/packages/search/src/use-fuzzy-search.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/packages/state-utils/src/create-selector/index.ts
Scanned 15730 files in 16.430049041 seconds

Using [email protected] and [email protected]

@tyxla
Copy link
Member Author

tyxla commented Dec 12, 2024

While the script generates a 32k lines results.json file, I'm getting some errors while running the command:

❯ yarn run component-usage-stats
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/client/a8c-for-agencies/sections/partner-directory/agency-details/hooks/use-country-list.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/client/a8c-for-agencies/sections/signup/agency-details-form/hooks/use-countries-and-states.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/client/components/search-sites/utils.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/client/data/plugins/helpers.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/client/jetpack-cloud/sections/partner-portal/company-details-form/hooks/use-countries-and-states.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/client/landing/stepper/constants.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/client/landing/stepper/utils/shuffle-array.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/client/landing/subscriptions/helpers/index.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/client/lib/promote-post/index.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/client/my-sites/email/form/mailboxes/components/utilities/get-cart-items.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/client/my-sites/plans/jetpack-plans/iterations.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/client/my-sites/stats/hooks/default-query-params.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/client/state/reader/recommended-sites/selectors/get-reader-recommended-sites.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/packages/calypso-products/src/constants/akismet.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/packages/calypso-products/src/constants/jetpack.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/packages/calypso-products/src/constants/terms.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/packages/calypso-products/src/constants/titan.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/packages/calypso-products/src/constants/types.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/packages/calypso-products/src/constants/wpcom.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/packages/calypso-products/src/types.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/packages/create-calypso-config/src/index.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/packages/data-stores/src/add-ons/constants.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/packages/domains-table/src/use-domain-row.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/packages/explat-client/src/internal/test-common.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/packages/js-utils/src/key-by.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/packages/search/src/use-fuzzy-search.ts
Failed to parse: /Users/marcociampini/Code/a8c/wp-calypso/packages/state-utils/src/create-selector/index.ts
Scanned 15730 files in 16.430049041 seconds

Using [email protected] and [email protected]

Yeah, that's expected. Just like in the other repos we ran react-scanner in, these can occur for some files that the tool can't parse. I'm not particularly concerned about having a few of them, especially because neither contains component usage, and because we can follow-up and fix them if needed.

IMHO we should be good to go if that's your only concern.

@ciampo
Copy link
Contributor

ciampo commented Dec 13, 2024

My only worry is that if the number of files continues to grow, it may eventually skew stats.

Maybe we could also track the files that are not parsed (or at least the number of files), so that we can monitor the stat and act on it in case the number keeps growing?

Otherwise I'm good with merging this PR and iterating on it.

Copy link
Member

@mirka mirka left a comment

Choose a reason for hiding this comment

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

Working as expected 👍

And with the suggested config changes, there are no parse failures and the JSON output is identical.

react-scanner.config.js Outdated Show resolved Hide resolved
react-scanner.config.js Show resolved Hide resolved
// Consider only imports of `@wordpress/components`
importedFrom: '@wordpress/components',
// Full usage report
processors: [ [ 'raw-report', { outputTo: './results/calypso.json' } ] ],
Copy link
Member

Choose a reason for hiding this comment

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

I understand this is mostly just for automated execution on a server somewhere, but it might be helpful to have components-usage or components-usage-stats in the folder name, instead of just results.

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree - I got this feedback multiple times and I'll be addressing it, first in Gutenberg and in the related script, then here and in the Jetpack PR.

tyxla and others added 2 commits January 3, 2025 13:40
Co-authored-by: Lena Morita <[email protected]>
Co-authored-by: Lena Morita <[email protected]>
@ciampo
Copy link
Contributor

ciampo commented Jan 9, 2025

No further feedback left on my end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Components [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. [Type] Task
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants