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

[Private APIs] Only prevent module re-registration if IS_WORDPRESS_CORE #48352

Merged
merged 4 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
"npm": ">=6.9.0 <7"
},
"config": {
"IS_GUTENBERG_PLUGIN": true,
"ALLOW_EXPERIMENT_REREGISTRATION": true
"IS_GUTENBERG_PLUGIN": true
},
"dependencies": {
"@wordpress/a11y": "file:packages/a11y",
Expand Down
11 changes: 7 additions & 4 deletions packages/private-apis/src/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,15 @@ const requiredConsent =

/** @type {boolean} */
let allowReRegistration;
// Use try/catch to force "false" if the environment variable is not explicitly
// set to true (e.g. when building WordPress core).
// The safety measure is meant for WordPress core where IS_WORDPRESS_CORE
// is set to true.
// For the general use-case, the re-registration should be allowed by default
// Let's default to true, then. Try/catch will fall back to "true" even if the
// environment variable is not explicitly defined.
try {
allowReRegistration = process.env.ALLOW_EXPERIMENT_REREGISTRATION ?? false;
allowReRegistration = process.env.IS_WORDPRESS_CORE ? false : true;
} catch ( error ) {
allowReRegistration = false;
allowReRegistration = true;
}

/**
Expand Down
6 changes: 0 additions & 6 deletions storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,4 @@ module.exports = {
emotionAlias: false,
storyStoreV7: true,
},
env: ( config ) => ( {
...config,
// Inject the `ALLOW_EXPERIMENT_REREGISTRATION` global, used by
// @wordpress/private-apis.
ALLOW_EXPERIMENT_REREGISTRATION: true,
} ),
};
8 changes: 0 additions & 8 deletions test/native/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ import { Image, NativeModules as RNNativeModules } from 'react-native';
// testing environment: https://github.com/facebook/react-native/blob/6c19dc3266b84f47a076b647a1c93b3c3b69d2c5/Libraries/Core/setUpNavigator.js#L17
global.navigator = global.navigator ?? {};

/**
* 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.
*/
global.process.env.ALLOW_EXPERIMENT_REREGISTRATION = true;

// Set up the app runtime globals for the test environment, which includes
// modifying the above `global.navigator`
require( '../../packages/react-native-editor/src/globals' );
Expand Down
10 changes: 5 additions & 5 deletions test/unit/config/gutenberg-env.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ global.process.env = {
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.
* Feature flag guarding features specific to WordPress core.
* It's important to set it to "true" in the test environment
* to ensure the Gutenberg plugin can be cleanly merged into
* WordPress core.
*/
ALLOW_EXPERIMENT_REREGISTRATION: false,
IS_WORDPRESS_CORE: true,
};
6 changes: 3 additions & 3 deletions tools/webpack/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +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/private-apis.
'process.env.ALLOW_EXPERIMENT_REREGISTRATION':
process.env.npm_package_config_ALLOW_EXPERIMENT_REREGISTRATION,
// Inject the `IS_WORDPRESS_CORE` global, used for feature flagging.
'process.env.IS_WORDPRESS_CORE':
process.env.npm_package_config_IS_WORDPRESS_CORE,
} ),
mode === 'production' && new ReadableJsAssetsWebpackPlugin(),
];
Expand Down
2 changes: 1 addition & 1 deletion typings/gutenberg-env/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
interface Environment {
NODE_ENV: unknown;
IS_GUTENBERG_PLUGIN?: boolean;
ALLOW_EXPERIMENT_REREGISTRATION?: boolean;
IS_WORDPRESS_CORE?: boolean;
}
interface Process {
env: Environment;
Expand Down