Widgets: Declare dependencies in a per-widget package.json#78463
Conversation
compose its classNames with clsx and register widgets/* as workspaces
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Size Change: -246 B (0%) Total Size: 7.98 MB 📦 View Changed
ℹ️ View Unchanged
|
private widget manifests skip published-package field rules, like routes/
|
Flaky tests detected in 2c929da. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/26161020809
|
simison
left a comment
There was a problem hiding this comment.
👍 perfect, let's also add package.json line at packages/wp-build/README.md for "Structure", perhaps with comment "Optional npm dependencies manifest"?
What?
package.jsontowidgets/hello-worldthat declares the widget's own dependencies:@wordpress/icons,@wordpress/ui, andclsx.widgets/*as npm workspaces in the rootpackage.json.classNamewithclsx, moving the inline layout styles into a co-locatedstyle.module.css.Why?
ESLint's
import/no-extraneous-dependenciesresolves a file's dependencies from the nearestpackage.json. With no manifest underwidgets/, any widget that imports a bundled dependency (such asclsx) would force that dependency to be declared at the repository root just to satisfy the rule.A per-widget
package.jsonlets each widget declare the dependencies it actually uses, so the root manifest stays free of widget-specific entries. Registeringwidgets/*as workspaces makes npm aware of each widget's manifest and links its dependencies on install.How?
widgets/hello-world/package.jsondeclares the@wordpress/*packages the widget consumes (externalized at runtime by the build) plusclsx(bundled into the widget output). The@wordpress/*entries usefile:links, consistent with the other in-repo workspaces.widgets/*is added to the rootworkspacesarray;npm installlinks the workspace and records it inpackage-lock.json.render.tsxcomposes its rootclassNamewithclsx( styles.root ); the layout declarations move from an inlinestyleobject tostyle.module.css.Note: the build's externals are a fixed list (
@wordpress/*plus a handful of vendor globals), so the manifest does not change build output. It is dependency-resolution metadata for tooling (ESLint, npm workspaces).clsxis not in the externals list, so it is bundled and therefore belongs in the widget'sdependencies.Testing
npm installruns clean and records@wordpress/hello-world-widgetas a workspace inpackage-lock.json.npx eslint widgets/hello-world/render.tsx widgets/hello-world/widget.tspasses (noimport/no-extraneous-dependencies).npx stylelint widgets/hello-world/style.module.csspasses.build/widgets/hello-world/emits the widget withclsxbundled intorender.js,@wordpress/*listed as dependencies inrender.min.asset.php, and the CSS module emitted as the widget stylesheet.heading-2xlon the brand surface.Follow-ups
package.jsonto other widgets as they gain bundled dependencies.