Skip to content

Commit

Permalink
Restore the "double experiment registration" check and make it condit…
Browse files Browse the repository at this point in the history
…ional on the ALLOW_EXPERIMENT_REREGISTRATION feature flag
  • Loading branch information
adamziel committed Feb 2, 2023
1 parent ada7693 commit b2585fc
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"npm": ">=6.9.0 <7"
},
"config": {
"IS_GUTENBERG_PLUGIN": true
"IS_GUTENBERG_PLUGIN": true,
"ALLOW_EXPERIMENT_REREGISTRATION": true
},
"dependencies": {
"@wordpress/a11y": "file:packages/a11y",
Expand Down
30 changes: 20 additions & 10 deletions packages/experiments/src/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ const registeredExperiments = [];
const requiredConsent =
'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.';

let allowReRegistration;
// Use try/catch to force "false" if the environment variable is not explicitly
// set to true (e.g. when building WordPress core).
try {
allowReRegistration = process.env.ALLOW_EXPERIMENT_REREGISTRATION;
} catch ( error ) {
allowReRegistration = false;
}

/**
* Called by a @wordpress package wishing to opt-in to accessing or exposing
* private experimental APIs.
Expand All @@ -68,19 +77,20 @@ export const __dangerousOptInToUnstableAPIsOnlyForCoreModules = (
'your product will inevitably break on one of the next WordPress releases.'
);
}
if ( ! process.env.IS_GUTENBERG_PLUGIN ) {
if (
! allowReRegistration &&
registeredExperiments.includes( moduleName )
) {
// This check doesn't play well with Story Books / Hot Module Reloading
// and isn't included in the Gutenberg plugin. It only matters in the
// WordPress core release.
if ( registeredExperiments.includes( moduleName ) ) {
throw new Error(
`You tried to opt-in to unstable APIs as module "${ moduleName }" which is already registered. ` +
'This feature is only for JavaScript modules shipped with WordPress core. ' +
'Please do not use it in plugins and themes as the unstable APIs will be removed ' +
'without a warning. If you ignore this error and depend on unstable features, ' +
'your product will inevitably break on one of the next WordPress releases.'
);
}
throw new Error(
`You tried to opt-in to unstable APIs as module "${ moduleName }" which is already registered. ` +
'This feature is only for JavaScript modules shipped with WordPress core. ' +
'Please do not use it in plugins and themes as the unstable APIs will be removed ' +
'without a warning. If you ignore this error and depend on unstable features, ' +
'your product will inevitably break on one of the next WordPress releases.'
);
}
if ( consent !== requiredConsent ) {
throw new Error(
Expand Down
7 changes: 7 additions & 0 deletions test/unit/config/is-gutenberg-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,11 @@ global.process.env = {
// eslint-disable-next-line @wordpress/is-gutenberg-plugin
IS_GUTENBERG_PLUGIN:
String( process.env.npm_package_config_IS_GUTENBERG_PLUGIN ) === 'true',
/**
* Whether to allow the same experiment to be registered multiple times.
* This is useful for development purposes, but should be set to false
* during the unit tests to ensure the Gutenberg plugin can be cleanly
* merged into WordPress core where this is false.
*/
ALLOW_EXPERIMENT_REREGISTRATION: false,
};
3 changes: 3 additions & 0 deletions tools/webpack/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ const plugins = [
// Inject the `IS_GUTENBERG_PLUGIN` global, used for feature flagging.
'process.env.IS_GUTENBERG_PLUGIN':
process.env.npm_package_config_IS_GUTENBERG_PLUGIN,
// Inject the `ALLOW_EXPERIMENT_REREGISTRATION` global, used by @wordpress/experiments.
'process.env.ALLOW_EXPERIMENT_REREGISTRATION':
process.env.npm_package_config_ALLOW_EXPERIMENT_REREGISTRATION,
} ),
mode === 'production' && new ReadableJsAssetsWebpackPlugin(),
];
Expand Down

0 comments on commit b2585fc

Please sign in to comment.