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

Separate Private APIs from Experimental APIs #47785

Closed
adamziel opened this issue Feb 6, 2023 · 9 comments
Closed

Separate Private APIs from Experimental APIs #47785

adamziel opened this issue Feb 6, 2023 · 9 comments
Labels
Developer Experience Ideas about improving block and theme developer experience Needs Technical Feedback Needs testing from a developer perspective. [Type] Experimental Experimental feature or API.

Comments

@adamziel
Copy link
Contributor

adamziel commented Feb 6, 2023

The problem

The new lock()/unlock() API doesn't allow exporting experimental APIs from the Gutenebrg plugin. It only enables sharing private APIs between Gutenberg packages.

Shipping public experimental APIs in the Gutenberg plugin enables developer to test them and provide feedback. This informs and shapes the Gutenberg plugin development. Historically, Gutenberg exported experimental APIs with the __experimental prefix. An unfortunate consequence was that they often got merged to WordPress core where they became stable APIs.

Proposed solution

I propose to:

  • Rename the @wordpress/experiments package to @wordpress/private-apis to clearly communicate its purpose
  • Only use the lock()/unlock() utilities to make APIs private and inaccessible in WordPress Core
  • Manually export experimental APIs from the Gutenberg plugin without merging them in WordPress Core

In practice, it would look like this:

// This function can't be used by extenders in any context:
function privateApi() {}

// This function can be used by extenders with the Gutenberg plugin but not in vanilla WordPress Core:
function experimentalApi() {}

// Gutenberg treats both functions as private APIs internally:
const experiments = {};
lock(experiments, { privateApi, experimentalApi });

// The experimental API is explicitly exported. Using IS_GUTENBERG_PLUGIN 
// allows Webpack to exclude the experimental export from WordPress core. 
if ( IS_GUTENBERG_PLUGIN ) {
   export function __experimentalApi() {
       return unlock( experiments ).experimentalApi( ...arguments );
   }
}

cc @youknowriad @talldan @gziolo @luisherranz @dmsnell @jsnajdr @mcsf @peterwilsoncc

@adamziel adamziel added Needs Technical Feedback Needs testing from a developer perspective. Developer Experience Ideas about improving block and theme developer experience [Type] Experimental Experimental feature or API. labels Feb 6, 2023
@youknowriad
Copy link
Contributor

I think we should rename the package indeed, and sooner rather than later (before 6.2)

@peterwilsoncc
Copy link
Contributor

I agree, private-apis is a better name.

For the purposes of WordPress 6.2, would it be possible to limit the addition of IS_GUTENBERG_PLUGIN code to gutenberg trunk for the time being and simply rename the package is the appropriate wp/* branch? That will reduce the chance of unintended side-affects.

@mcsf
Copy link
Contributor

mcsf commented Feb 7, 2023

I think the proposal is consistent with the direction we've been taking, including the renaming.

If I have to pick nits from the example, I see APIs are exported by creating a new function for each one:

// The experimental API is explicitly exported. Using IS_GUTENBERG_PLUGIN 
// allows Webpack to exclude the experimental export from WordPress core. 
if ( IS_GUTENBERG_PLUGIN ) {
   export function __experimentalApi() {
       return unlock( experiments ).experimentalApi( ...arguments );
   }
}

I wonder if this is unnecessary overhead compared to simply exposing exported objects:

if ( IS_GUTENBERG_PLUGIN ) {
   export const __experimentalApi = unlock( experiments ).experimentalApi;
}

@adamziel
Copy link
Contributor Author

adamziel commented Feb 7, 2023

@mcsf I like exporting the objects directly! I’ll prepare a PR and a documentation update.

@peterwilsoncc I’m not aware of any experimental API that’s awaiting urgent release before 6.2 so I think we’re good.

As for renaming the package in a wp/* branch, sure. Just to be sure - I also want to do it in trunk. Or is there a reason not to?

@adamziel
Copy link
Contributor Author

adamziel commented Feb 7, 2023

I started two related PRs:

I'm still working on getting the CI to pass

@peterwilsoncc
Copy link
Contributor

As for renaming the package in a wp/* branch, sure. Just to be sure - I also want to do it in trunk. Or is there a reason not to?

Not that I can see, no.

My main thought for limiting the changes in the wp/6.2 branch is it's less risky during beta. I'll find time to look at the PRs shortly.

@adamziel
Copy link
Contributor Author

Here's the documentation update: #47785

@adamziel
Copy link
Contributor Author

The only remaining part of this issue is the coding guidelines change:

#47973

I'll appreciate your reviews so we can close this.

adamziel added a commit that referenced this issue Apr 13, 2023
…47973)

Documents the separation between private APIs and experimental APIs. Part of #47785
@adamziel
Copy link
Contributor Author

Closing this issue since #47937 is now merged. Yay!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Developer Experience Ideas about improving block and theme developer experience Needs Technical Feedback Needs testing from a developer perspective. [Type] Experimental Experimental feature or API.
Projects
None yet
Development

No branches or pull requests

4 participants