Skip to content

Commit

Permalink
Go bonkers on the global vars
Browse files Browse the repository at this point in the history
  • Loading branch information
sirreal committed May 8, 2024
1 parent cc43b8c commit fdf93ae
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
17 changes: 17 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ const restrictedImports = [
},
];

const restrictedGlobals = [
{
name: 'SCRIPT_DEBUG',
message: 'Use globalThis.SCRIPT_DEBUG',
},
{
name: 'IS_GUTENBERG_PLUGIN',
message: 'Use globalThis.IS_GUTENBERG_PLUGIN',
},
{
name: 'IS_WORDPRESS_CORE',
message: 'Use globalThis.IS_WORDPRESS_CORE',
},
];

module.exports = {
root: true,
extends: [
Expand All @@ -92,6 +107,7 @@ module.exports = {
],
globals: {
wp: 'off',
globalThis: 'readonly',
},
settings: {
jsdoc: {
Expand Down Expand Up @@ -122,6 +138,7 @@ module.exports = {
paths: restrictedImports,
},
],
'no-restricted-globals': [ 'error', ...restrictedGlobals ],
'@typescript-eslint/no-restricted-imports': [
'error',
{
Expand Down
2 changes: 1 addition & 1 deletion packages/private-apis/src/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ let allowReRegistration;
// 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.IS_WORDPRESS_CORE ? false : true;
allowReRegistration = globalThis.IS_WORDPRESS_CORE ? false : true;
} catch ( error ) {
allowReRegistration = true;
}
Expand Down
6 changes: 3 additions & 3 deletions tools/webpack/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ const plugins = [
process.env.WP_BUNDLE_ANALYZER && new BundleAnalyzerPlugin(),
new DefinePlugin( {
// Inject the `IS_GUTENBERG_PLUGIN` global, used for feature flagging.
'process.env.IS_GUTENBERG_PLUGIN':
'globalThis.IS_GUTENBERG_PLUGIN':
process.env.npm_package_config_IS_GUTENBERG_PLUGIN,
// Inject the `IS_WORDPRESS_CORE` global, used for feature flagging.
'process.env.IS_WORDPRESS_CORE':
'globalThis.IS_WORDPRESS_CORE':
process.env.npm_package_config_IS_WORDPRESS_CORE,
// Inject the `SCRIPT_DEBUG` global, used for dev versions of JavaScript.
SCRIPT_DEBUG: mode === 'development',
'globalThis.SCRIPT_DEBUG': mode === 'development',
} ),
mode === 'production' && new ReadableJsAssetsWebpackPlugin(),
];
Expand Down
19 changes: 8 additions & 11 deletions typings/gutenberg-env/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
interface Environment {
NODE_ENV: unknown;
IS_GUTENBERG_PLUGIN?: boolean;
IS_WORDPRESS_CORE?: boolean;
}

declare namespace NodeJS {
export interface ProcessEnv extends Environment {}

export interface Process {
env: ProcessEnv;
interface ProcessEnv {
readonly NODE_ENV?: 'production' | 'development' | 'test';
}
interface Process {
env: NodeJS.ProcessEnv;
}
}

declare var process: NodeJS.Process;

declare var SCRIPT_DEBUG: boolean;
declare var SCRIPT_DEBUG: undefined | boolean;
declare var IS_GUTENBERG_PLUGIN: undefined | boolean;
declare var IS_WORDPRESS_CORE: undefined | boolean;

0 comments on commit fdf93ae

Please sign in to comment.