From 8e8adc95db86dca3957eb9c5dd225c4219bea5d7 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 10 May 2024 19:54:41 +0200 Subject: [PATCH] Some env improvements --- test/unit/config/gutenberg-env.js | 44 +++++++++---------------------- typings/gutenberg-env/index.d.ts | 11 ++++++++ 2 files changed, 23 insertions(+), 32 deletions(-) diff --git a/test/unit/config/gutenberg-env.js b/test/unit/config/gutenberg-env.js index a6a1b81f740970..25fb473a496766 100644 --- a/test/unit/config/gutenberg-env.js +++ b/test/unit/config/gutenberg-env.js @@ -1,32 +1,12 @@ -global.process.env = { - ...global.process.env, - /* - Inject the `IS_GUTENBERG_PLUGIN` global, used for feature flagging. - - The conversion to boolean is required here. Why? Package.json defines - IS_GUTENBERG_PLUGIN as follows: - - "config": { - "IS_GUTENBERG_PLUGIN": true - } - - Webpack then replaces references to IS_GUTENBERG_PLUGIN with a literal value `true`. - The file you are reading right now, however, receives a string value "true": - - "true" === process.env.npm_package_config_IS_GUTENBERG_PLUGIN - - The code can only work consistently in both environments when the value of - IS_GUTENBERG_PLUGIN is consistent. For this reason, the line below turns the - string representation of IS_GUTENBERG_PLUGIN into a boolean value. - */ - // eslint-disable-next-line @wordpress/is-gutenberg-plugin - IS_GUTENBERG_PLUGIN: - String( process.env.npm_package_config_IS_GUTENBERG_PLUGIN ) === 'true', - /** - * 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. - */ - IS_WORDPRESS_CORE: true, -}; +/* + * 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. + */ +globalThis.IS_WORDPRESS_CORE = true; + +// Inject the `IS_GUTENBERG_PLUGIN` global, used for feature flagging. +// eslint-disable-next-line @wordpress/is-gutenberg-plugin +globalThis.IS_GUTENBERG_PLUGIN = + String( process.env.npm_package_config_IS_GUTENBERG_PLUGIN ) === 'true'; diff --git a/typings/gutenberg-env/index.d.ts b/typings/gutenberg-env/index.d.ts index 0239ee2ecded8d..c2bc71ae435ee2 100644 --- a/typings/gutenberg-env/index.d.ts +++ b/typings/gutenberg-env/index.d.ts @@ -9,6 +9,17 @@ declare namespace NodeJS { declare var process: NodeJS.Process; +/** + * Whether the code is running in WordPress with SCRIPT_DEBUG flag. + */ declare var SCRIPT_DEBUG: undefined | boolean; + +/** + * Whether code is running within the Gutenberg plugin. + * + * When the codebase is built for the plugin, this variable will be set to `true`. + * When building for WordPress Core, it will be set to `false` or `undefined`. + */ declare var IS_GUTENBERG_PLUGIN: undefined | boolean; + declare var IS_WORDPRESS_CORE: undefined | boolean;