Skip to content

Commit

Permalink
Some env improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
sirreal committed May 10, 2024
1 parent d2ebe15 commit 8e8adc9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 32 deletions.
44 changes: 12 additions & 32 deletions test/unit/config/gutenberg-env.js
Original file line number Diff line number Diff line change
@@ -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';
11 changes: 11 additions & 0 deletions typings/gutenberg-env/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 8e8adc9

Please sign in to comment.