Premium Analytics: build the internal ui and data packages as script modules#49574
Conversation
|
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖 Follow this PR Review Process:
If you have questions about anything, reach out in #jetpack-developers for guidance! |
Declare wpScriptModuleExports on the internal ui and data packages so each bundles once into a registered script module, and consumer bundles externalize the import (the same mechanism that externalizes @wordpress/* script modules) instead of bundling per-consumer copies. Per-package rationale: - ui: styled components (CSS modules) can be delivered to build contexts with no style support — the module is built from the Phase-1 transpile output, where the scss is already compiled via the style runtime. - data: its module-scope QueryClient and global-error manager become genuine page-wide singletons (shared report cache across widgets instead of one cache per consumer bundle), and widget metadata modules that import helpers like getDefaultQueryParams stop bundling the data package (TanStack Query included) per widget. Adds the link: deps for the internal packages these modules' bundles resolve (datetime, formatters) plus ui itself for future importers — the resolution mechanism documented in #49189. Notably, tsconfig paths route bundled internal imports to src regardless of package.json entry fields (esbuild honors tsconfig paths), so wpScriptModuleExports externalization is the only way to deliver an internal package's compiled build output to consumers.
dcde93f to
e6bf2e5
Compare
Code Coverage SummaryThis PR did not change code coverage! That could be good or bad, depending on the situation. Everything covered before, and still is? Great! Nothing was covered before? Not so great. 🤷 |
Part of the work on analytics changes
Proposed changes
Why
The internal
packages/*are currently bundled into every consumer: each route, widget render, and widget metadata bundle that imports one gets its own private copy. That model has three concrete failure modes, two of which we've already hit:packages/dataexports a module-scopeQueryClientand global-error manager. Bundled per consumer, every widget type's render bundle gets its own copy — so as data-driven widget types accumulate, each carries a private cache (identical report requests across types can't dedupe) and "the" error manager fragments per bundle.src— including.module.scss. Any build pipeline without style plugins (concretely: the widget metadata build) hard-fails the moment its import graph touches a styled internal package (No loader is configured for ".scss" files). This is what blocks widget attribute editors today.There's no bundler-config way out of (2): esbuild honors
tsconfig.jsonpathsover package.json entry fields, so a bundled internal import always lands insrcno matter whatmodule/mainpoint at. The only mechanism that delivers a package's compiled output to consumers is the one@wordpress/*packages already use — register it as a script module and externalize the import.What
Declare
wpScriptModuleExportsonpackages/uiandpackages/data, so each is built once into a registered script module and consumer bundles externalize the import:QueryClientand error manager become page-wide singletons by construction (one registered module instance, every consumer imports it), and widget metadata modules importing helpers likegetDefaultQueryParamsshrink from ~31 KB to ~1.5 KB each (the bundled helper/datetime slice externalizes).Also adds the parent-manifest
link:entries these modules' bundles resolve through (datetime,formatters) plusuifor future importers — the resolution mechanism documented in #49189.Scope note:
widgets-toolkitwould also benefit (it bundles charts + components into every widget render) but is slated for dissolution, so it's deferred pending that decision.datetime/formatters/routing/iconsare small, logic-only, and singleton-free — per-consumer bundling is fine there.Related product discussion/links
Does this pull request change what data or activity we track or use?
No.
Testing instructions
pnpm --filter @automattic/jetpack-premium-analytics build— Phase 2 reports✔ Bundled uiand✔ Bundled data;build/modules/{ui,data}/and the module registry contain the new script modules.pnpm --filter @automattic/jetpack-premium-analytics typecheck— clean.try/wooa7s-1460-widget-inline-report-params-field), which carries this same config plus consumers:data/uideclared as external module dependencies, andmodules/data/index.min.jsloads once per page;proxy/reports/ordersrequests (one primary + one comparison) serving both widget types — per-bundle clients would fire 4, one pair per type, with nothing deduping across bundles.