Add wpPlugin.packageSources via pnpm patch on @wordpress/build#48089
Add wpPlugin.packageSources via pnpm patch on @wordpress/build#48089
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 🤖 🔴 Action required: Please include detailed testing steps, explaining how to test your change, like so: 🔴 Action required: We would recommend that you add a section to the PR description to specify whether this PR includes any changes to data or privacy, like so: Follow this PR Review Process:
If you have questions about anything, reach out in #jetpack-developers for guidance! Premium Analytics plugin: No scheduled milestone found for this plugin. If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack. |
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. 🤷 |
There was a problem hiding this comment.
Move to the existing .pnpm-patches/ directory.
Also, I'm not terribly fond of the "apply a large patch I hope to get merged upstream soon" approach here; such hopes seem frequently dashed.
There was a problem hiding this comment.
Yeah, that's fair. We need to try to move these changes upstream asap.
There was a problem hiding this comment.
Unneeded. Any comments about a patch can be included at the top of the patch file itself, as shown by the existing patches.
|
@anomiex PR is not ready for review. Sorry about it. We need to land the package and plugin PRs first |
|
Will this idea cause problems if multiple different wp-build-using plugins each include incompatible versions of It looks like, unless all plugins are careful to only load their |
6f55a70 to
ae38a48
Compare
a53fd17 to
7ccaa1a
Compare
Great point. As far as I understand, this is inherent to WordPress's global script module registry, not specific to The same trade-off already exists with Looking at the generated Within the Jetpack monorepo, this is mitigated by the shared lockfile: all plugins built from the same commit get the same version of Without This is a crucial point to keep in mind as more plugins adopt this pattern. Does it make sense? |
|
Something to keep in mind is that it doesn't seem like the people working on wp-build are considering these sorts of edge cases; they seem pretty focused on Gutenberg and Core. You should probably copy that information to your WordPress/gutenberg#77226 PR. |
|
Note to also keep testing Forms builds and works well with this, as it's the only other thing using WP Build: |
2eafa61 to
ecf8b90
Compare
7ccaa1a to
e9b7a96
Compare
|
Addressed all three:
|
Good idea. I'll add the script module collision analysis to the Gutenberg PR #77226 so upstream maintainers are aware of the deduplication trade-off. |
Confirmed. |
dd7ccc7 to
8610eb4
Compare
patches @wordpress/build@0.11.0 to add packageSources config for cross-directory package discovery. mirrors upstream Gutenberg PR WordPress/gutenberg#77226 1:1. flags @automattic/number-formatters as a script-module exporter via wpScriptModuleExports so consumers can externalize it.
configures @automattic/number-formatters as a named packageSource and demonstrates both import modes in the dashboard route: static import for formatNumber, dynamic import for formatNumberCompact via React.lazy and Suspense.
25c7409 to
1b3fb87
Compare
regenerate patch against 0.12.0 sources using upstream PR #77226 diff. header embeds the version-agnostic rebase procedure.
# Conflicts: # pnpm-lock.yaml # projects/packages/premium-analytics/package.json
What?
Patches
@wordpress/build@0.11.0to addwpPlugin.packageSources. This config lets wp-build discover, externalize, and bundle shared packages that live outside./packages/, preserving their npm identity as script module IDs.wpModule/wpScriptModuleExportspackages outside./packages/*WordPress/gutenberg#77225Closes WOOA7S-1341 WOOA7S-1342
Why?
@wordpress/buildonly scans./packages/for compilable packages. Shared packages in Jetpack'sjs-packages/(like@automattic/number-formatters) get inlined into each consumer's bundle, duplicating code and breaking script module deduplication.This is the bridge approach: a pnpm patch that mirrors the upstream PR 1:1 until Core merges it. Zero Jetpack-specific logic.
How?
pnpm patch (
.pnpm-patches/@wordpress__build@0.11.0.patch)Three changes to
@wordpress/build:Per-package externals: Named sources listed in
packageSourcesare externalized individually via exact-matchonResolvehandlers, instead of inferring the entire@scope/*as external.Skip transpilation for external sources: Packages from
packageSourcesare pre-built by their own build system. wp-build only bundles them as script modules, skippingtranspilePackage().wpScriptModuleExportson shared packages: Shared packages declarewpScriptModuleExportsin theirpackage.jsonso the externals plugin can detect them as script modules and track dependencies in.asset.php.Validation with premium-analytics
The dashboard route imports
@automattic/number-formattersto demonstrate both import modes:formatNumber): compiled as a script module dependency with'import' => 'static'in the asset metadataformatNumberCompact): loaded viaReact.lazy+Suspense, tracked as'import' => 'dynamic'in the asset metadataBoth modes produce a single shared script module registered under
@automattic/number-formatters, with no build artifacts insidejs-packages/number-formatters/.Patch lifecycle
When Gutenberg merges #77226 and Jetpack upgrades
@wordpress/build: delete the patch, remove the entry frompnpm-workspace.yaml, runpnpm install.Trade-offs
packageSourcesextends the same shared-module trust model thatexternalNamespacesalready applies to@wordpress/*packages. All consumers of the samepackageSourcesentry share a single script module registration in WordPress's global registry — last plugin to load wins if versions differ.Within the monorepo this is mitigated by the shared lockfile; cross-release version skew is the real risk (plugin A on release N, plugin B on release N-1).
PR #48095 adds PHPUnit tests that document and verify this behavior.
Testing
Bundled @automattic/number-formattersin Phase 2build/modules/registry.phplists@automattic/number-formatterswith its npm identity preservedbuild/routes/dashboard/content.min.asset.phptracksnumber-formattersas static + dynamic dependency:build/orbuild-module/artifacts insidejs-packages/number-formatters/pnpm --filter @automattic/jetpack-forms run buildstill succeeds (regression check)The Analytics page:
<hostname>/wp-admin/admin.php?page=jetpack-premium-analytics