From 3c6a5386a132899445734f5bd8b5ffffac7b4119 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 5 Sep 2024 19:17:38 +0200 Subject: [PATCH 01/21] Restructure/refactor to prepare for a11y for a module --- packages/a11y/src/index.js | 75 +---------------- packages/a11y/src/index.native.js | 2 +- .../a11y/src/{ => shared}/add-container.js | 0 .../add-intro-text.ts} | 10 +-- packages/a11y/src/{ => shared}/clear.js | 0 .../a11y/src/{ => shared}/filter-message.js | 0 packages/a11y/src/shared/index.js | 81 +++++++++++++++++++ 7 files changed, 89 insertions(+), 79 deletions(-) rename packages/a11y/src/{ => shared}/add-container.js (100%) rename packages/a11y/src/{add-intro-text.js => shared/add-intro-text.ts} (83%) rename packages/a11y/src/{ => shared}/clear.js (100%) rename packages/a11y/src/{ => shared}/filter-message.js (100%) create mode 100644 packages/a11y/src/shared/index.js diff --git a/packages/a11y/src/index.js b/packages/a11y/src/index.js index 957c76500c434..59e93da780bd8 100644 --- a/packages/a11y/src/index.js +++ b/packages/a11y/src/index.js @@ -2,87 +2,20 @@ * WordPress dependencies */ import domReady from '@wordpress/dom-ready'; +import { __ } from '@wordpress/i18n'; /** * Internal dependencies */ -import addIntroText from './add-intro-text'; -import addContainer from './add-container'; -import clear from './clear'; -import filterMessage from './filter-message'; +import { makeSetupFunction } from './shared/index'; +export { speak } from './shared/index'; /** * Create the live regions. */ -export function setup() { - const introText = document.getElementById( 'a11y-speak-intro-text' ); - const containerAssertive = document.getElementById( - 'a11y-speak-assertive' - ); - const containerPolite = document.getElementById( 'a11y-speak-polite' ); - - if ( introText === null ) { - addIntroText(); - } - - if ( containerAssertive === null ) { - addContainer( 'assertive' ); - } - - if ( containerPolite === null ) { - addContainer( 'polite' ); - } -} +export const setup = makeSetupFunction( __( 'Notifications' ) ); /** * Run setup on domReady. */ domReady( setup ); - -/** - * Allows you to easily announce dynamic interface updates to screen readers using ARIA live regions. - * This module is inspired by the `speak` function in `wp-a11y.js`. - * - * @param {string} message The message to be announced by assistive technologies. - * @param {string} [ariaLive] The politeness level for aria-live; default: 'polite'. - * - * @example - * ```js - * import { speak } from '@wordpress/a11y'; - * - * // For polite messages that shouldn't interrupt what screen readers are currently announcing. - * speak( 'The message you want to send to the ARIA live region' ); - * - * // For assertive messages that should interrupt what screen readers are currently announcing. - * speak( 'The message you want to send to the ARIA live region', 'assertive' ); - * ``` - */ -export function speak( message, ariaLive ) { - /* - * Clear previous messages to allow repeated strings being read out and hide - * the explanatory text from assistive technologies. - */ - clear(); - - message = filterMessage( message ); - - const introText = document.getElementById( 'a11y-speak-intro-text' ); - const containerAssertive = document.getElementById( - 'a11y-speak-assertive' - ); - const containerPolite = document.getElementById( 'a11y-speak-polite' ); - - if ( containerAssertive && ariaLive === 'assertive' ) { - containerAssertive.textContent = message; - } else if ( containerPolite ) { - containerPolite.textContent = message; - } - - /* - * Make the explanatory text available to assistive technologies by removing - * the 'hidden' HTML attribute. - */ - if ( introText ) { - introText.removeAttribute( 'hidden' ); - } -} diff --git a/packages/a11y/src/index.native.js b/packages/a11y/src/index.native.js index f6f53b6343adb..e17597a8b2747 100644 --- a/packages/a11y/src/index.native.js +++ b/packages/a11y/src/index.native.js @@ -1,7 +1,7 @@ /** * Internal dependencies */ -import filterMessage from './filter-message'; +import filterMessage from './shared/filter-message'; /** * Update the ARIA live notification area text node. diff --git a/packages/a11y/src/add-container.js b/packages/a11y/src/shared/add-container.js similarity index 100% rename from packages/a11y/src/add-container.js rename to packages/a11y/src/shared/add-container.js diff --git a/packages/a11y/src/add-intro-text.js b/packages/a11y/src/shared/add-intro-text.ts similarity index 83% rename from packages/a11y/src/add-intro-text.js rename to packages/a11y/src/shared/add-intro-text.ts index 2bcf453ec44c8..6bd97c887664d 100644 --- a/packages/a11y/src/add-intro-text.js +++ b/packages/a11y/src/shared/add-intro-text.ts @@ -1,22 +1,18 @@ -/** - * WordPress dependencies - */ -import { __ } from '@wordpress/i18n'; - /** * Build the explanatory text to be placed before the aria live regions. * * This text is initially hidden from assistive technologies by using a `hidden` * HTML attribute which is then removed once a message fills the aria-live regions. * + * @param {string} introTextContent The translated intro text content. * @return {HTMLParagraphElement} The explanatory text HTML element. */ -export default function addIntroText() { +export default function addIntroText( introTextContent: string ) { const introText = document.createElement( 'p' ); introText.id = 'a11y-speak-intro-text'; introText.className = 'a11y-speak-intro-text'; - introText.textContent = __( 'Notifications' ); + introText.textContent = introTextContent; introText.setAttribute( 'style', diff --git a/packages/a11y/src/clear.js b/packages/a11y/src/shared/clear.js similarity index 100% rename from packages/a11y/src/clear.js rename to packages/a11y/src/shared/clear.js diff --git a/packages/a11y/src/filter-message.js b/packages/a11y/src/shared/filter-message.js similarity index 100% rename from packages/a11y/src/filter-message.js rename to packages/a11y/src/shared/filter-message.js diff --git a/packages/a11y/src/shared/index.js b/packages/a11y/src/shared/index.js new file mode 100644 index 0000000000000..5136d63c01221 --- /dev/null +++ b/packages/a11y/src/shared/index.js @@ -0,0 +1,81 @@ +/** + * Internal dependencies + */ +import addContainer from '../shared/add-container'; +import addIntroText from '../shared/add-intro-text'; +import clear from '../shared/clear'; +import filterMessage from '../shared/filter-message'; + +/** + * Create the live regions. + * @param {string} introTextContent The intro text content. + */ +export function makeSetupFunction( introTextContent ) { + return function setup() { + const introText = document.getElementById( 'a11y-speak-intro-text' ); + const containerAssertive = document.getElementById( + 'a11y-speak-assertive' + ); + const containerPolite = document.getElementById( 'a11y-speak-polite' ); + + if ( introText === null ) { + addIntroText( introTextContent ); + } + + if ( containerAssertive === null ) { + addContainer( 'assertive' ); + } + + if ( containerPolite === null ) { + addContainer( 'polite' ); + } + }; +} + +/** + * Allows you to easily announce dynamic interface updates to screen readers using ARIA live regions. + * This module is inspired by the `speak` function in `wp-a11y.js`. + * + * @param {string} message The message to be announced by assistive technologies. + * @param {string} [ariaLive] The politeness level for aria-live; default: 'polite'. + * + * @example + * ```js + * import { speak } from '@wordpress/a11y'; + * + * // For polite messages that shouldn't interrupt what screen readers are currently announcing. + * speak( 'The message you want to send to the ARIA live region' ); + * + * // For assertive messages that should interrupt what screen readers are currently announcing. + * speak( 'The message you want to send to the ARIA live region', 'assertive' ); + * ``` + */ +export function speak( message, ariaLive ) { + /* + * Clear previous messages to allow repeated strings being read out and hide + * the explanatory text from assistive technologies. + */ + clear(); + + message = filterMessage( message ); + + const introText = document.getElementById( 'a11y-speak-intro-text' ); + const containerAssertive = document.getElementById( + 'a11y-speak-assertive' + ); + const containerPolite = document.getElementById( 'a11y-speak-polite' ); + + if ( containerAssertive && ariaLive === 'assertive' ) { + containerAssertive.textContent = message; + } else if ( containerPolite ) { + containerPolite.textContent = message; + } + + /* + * Make the explanatory text available to assistive technologies by removing + * the 'hidden' HTML attribute. + */ + if ( introText ) { + introText.removeAttribute( 'hidden' ); + } +} From 7dd871b5c769e769689771ae817effda0817e99d Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 5 Sep 2024 19:18:42 +0200 Subject: [PATCH 02/21] Create a11y module --- packages/a11y/package.json | 1 + packages/a11y/src/module/index.ts | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 packages/a11y/src/module/index.ts diff --git a/packages/a11y/package.json b/packages/a11y/package.json index 88123b3c6c712..ff876dedb6bab 100644 --- a/packages/a11y/package.json +++ b/packages/a11y/package.json @@ -26,6 +26,7 @@ }, "main": "build/index.js", "module": "build-module/index.js", + "wp-module": "build-module/module/index.js", "react-native": "src/index", "types": "build-types", "dependencies": { diff --git a/packages/a11y/src/module/index.ts b/packages/a11y/src/module/index.ts new file mode 100644 index 0000000000000..f5e7daaf837b2 --- /dev/null +++ b/packages/a11y/src/module/index.ts @@ -0,0 +1,25 @@ +/** + * Internal dependencies + */ +import { makeSetupFunction } from '../shared/index'; +export { speak } from '../shared/index'; + +// Without an i18n script-module, "Notifications" (the only localized text used in this module) +// will be trasnlated on the server and provided as script-module data. +let notificationsText = 'Notifications'; +try { + const textContent = document.getElementById( + 'wp-script-module-data-@wordpress/a11y' + )?.textContent; + if ( textContent ) { + const parsed = JSON.parse( textContent ); + notificationsText = parsed?.i18n?.Notifications ?? notificationsText; + } +} catch {} + +/** + * Create the live regions. + */ +export const setup = makeSetupFunction( notificationsText ); + +setup(); From cc3e85e5fd5765ff330969addbd2f72e4574895d Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 5 Sep 2024 20:39:27 +0200 Subject: [PATCH 03/21] Register @wordpress/a11y script module --- lib/experimental/script-modules.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/experimental/script-modules.php b/lib/experimental/script-modules.php index 5a14e1418ed6d..61dabbe324536 100644 --- a/lib/experimental/script-modules.php +++ b/lib/experimental/script-modules.php @@ -200,3 +200,28 @@ function gutenberg_dequeue_module( $module_identifier ) { _deprecated_function( __FUNCTION__, 'Gutenberg 17.6.0', 'wp_dequeue_script_module' ); wp_script_modules()->dequeue( $module_identifier ); } + +/** + * Registers vendor JavaScript files to be used as dependencies of the editor + * and plugins. + * + * This function is called from a script during the plugin build process, so it + * should not call any WordPress PHP functions. + * + * @since 13.0 + * + * @param WP_Scripts $scripts WP_Scripts instance. + */ +function gutenberg_register_script_modules() { + // When in production, use the plugin's version as the default asset version; + // else (for development or test) default to use the current time. + $default_version = defined( 'GUTENBERG_VERSION' ) && ! SCRIPT_DEBUG ? GUTENBERG_VERSION : time(); + + wp_register_script_module( + '@wordpress/a11y', + gutenberg_url( 'build-module/a11y.min.js' ), + array(), + $default_version + ); +} +add_action( 'init', 'gutenberg_register_script_modules' ); From ef29e8181a5bc334f7e2b4734b8bee1019c41313 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 5 Sep 2024 20:51:16 +0200 Subject: [PATCH 04/21] Add translation for "Notifications" --- lib/experimental/script-modules.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/experimental/script-modules.php b/lib/experimental/script-modules.php index 61dabbe324536..e4f809ad26b4f 100644 --- a/lib/experimental/script-modules.php +++ b/lib/experimental/script-modules.php @@ -223,5 +223,12 @@ function gutenberg_register_script_modules() { array(), $default_version ); + add_filter( + 'script_module_data_@wordpress/a11y', + function ( $data ) { + $data['i18n'] = array( 'Notifications' => __( 'Notifications', 'gutenberg' ) ); + return $data; + } + ); } add_action( 'init', 'gutenberg_register_script_modules' ); From a138eb81bef4cafc74bbf282e18d7e85c3168e8f Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 6 Sep 2024 13:45:47 +0200 Subject: [PATCH 05/21] Improve ariaLive param type --- packages/a11y/README.md | 2 +- packages/a11y/src/shared/index.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/a11y/README.md b/packages/a11y/README.md index 0f40d9edd010e..755396d2bb8f0 100644 --- a/packages/a11y/README.md +++ b/packages/a11y/README.md @@ -39,7 +39,7 @@ speak( 'The message you want to send to the ARIA live region', 'assertive' ); _Parameters_ - _message_ `string`: The message to be announced by assistive technologies. -- _ariaLive_ `[string]`: The politeness level for aria-live; default: 'polite'. +- _ariaLive_ `['polite'|'assertive']`: The politeness level for aria-live; default: 'polite'. diff --git a/packages/a11y/src/shared/index.js b/packages/a11y/src/shared/index.js index 5136d63c01221..9d2fb5926b4a1 100644 --- a/packages/a11y/src/shared/index.js +++ b/packages/a11y/src/shared/index.js @@ -36,8 +36,8 @@ export function makeSetupFunction( introTextContent ) { * Allows you to easily announce dynamic interface updates to screen readers using ARIA live regions. * This module is inspired by the `speak` function in `wp-a11y.js`. * - * @param {string} message The message to be announced by assistive technologies. - * @param {string} [ariaLive] The politeness level for aria-live; default: 'polite'. + * @param {string} message The message to be announced by assistive technologies. + * @param {'polite'|'assertive'} [ariaLive] The politeness level for aria-live; default: 'polite'. * * @example * ```js From 7e0e75bee121524b428c19cc4aa027af0382706b Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Fri, 6 Sep 2024 13:46:08 +0200 Subject: [PATCH 06/21] Add a11y to known wp script modules --- packages/dependency-extraction-webpack-plugin/lib/util.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/dependency-extraction-webpack-plugin/lib/util.js b/packages/dependency-extraction-webpack-plugin/lib/util.js index ee5c2face1b9d..5af910535f220 100644 --- a/packages/dependency-extraction-webpack-plugin/lib/util.js +++ b/packages/dependency-extraction-webpack-plugin/lib/util.js @@ -89,9 +89,10 @@ function defaultRequestToExternalModule( request ) { return `module ${ request }`; } - if ( request === '@wordpress/interactivity-router' ) { - // Assumes this is usually going to be used as a dynamic import. - return `import ${ request }`; + switch ( request ) { + case '@wordpress/interactivity-router': + case '@wordpress/a11y': + return `import ${ request }`; } const isWordPressScript = Boolean( defaultRequestToExternal( request ) ); From 89745a62f07c67df39e5f2c62613233d4ea19dba Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 10 Sep 2024 11:20:11 +0200 Subject: [PATCH 07/21] Use wp-script-module in a11y package --- packages/a11y/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/a11y/package.json b/packages/a11y/package.json index ff876dedb6bab..805a3d9efbdb5 100644 --- a/packages/a11y/package.json +++ b/packages/a11y/package.json @@ -26,7 +26,7 @@ }, "main": "build/index.js", "module": "build-module/index.js", - "wp-module": "build-module/module/index.js", + "wp-script-module": "build-module/module/index.js", "react-native": "src/index", "types": "build-types", "dependencies": { From b8b2c42ba4df5e8112bec73ae154a9b1e9fc8230 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 10 Sep 2024 11:43:13 +0200 Subject: [PATCH 08/21] Use wp-script-module-exports for a11y --- packages/a11y/package.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/a11y/package.json b/packages/a11y/package.json index 805a3d9efbdb5..d1f454cf579e3 100644 --- a/packages/a11y/package.json +++ b/packages/a11y/package.json @@ -26,9 +26,11 @@ }, "main": "build/index.js", "module": "build-module/index.js", - "wp-script-module": "build-module/module/index.js", "react-native": "src/index", "types": "build-types", + "wp-script-module-exports": { + ".": "./build-module/module/index.js" + }, "dependencies": { "@babel/runtime": "^7.16.0", "@wordpress/dom-ready": "file:../dom-ready", From b76dd7fadefcb0f6b452dc958c653dfdae96ad50 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 10 Sep 2024 17:07:38 +0200 Subject: [PATCH 09/21] Use wpScriptModuleExports field --- packages/a11y/package.json | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/a11y/package.json b/packages/a11y/package.json index d1f454cf579e3..327d6b9ff0716 100644 --- a/packages/a11y/package.json +++ b/packages/a11y/package.json @@ -28,9 +28,7 @@ "module": "build-module/index.js", "react-native": "src/index", "types": "build-types", - "wp-script-module-exports": { - ".": "./build-module/module/index.js" - }, + "wpScriptModuleExports": "./build-module/module/index.js", "dependencies": { "@babel/runtime": "^7.16.0", "@wordpress/dom-ready": "file:../dom-ready", From 22d76aa81a99909d033eedfbc14c9063305ebc78 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 10 Sep 2024 17:30:49 +0200 Subject: [PATCH 10/21] Fix a11y module url --- lib/experimental/script-modules.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/experimental/script-modules.php b/lib/experimental/script-modules.php index e4f809ad26b4f..49385dc8e0e88 100644 --- a/lib/experimental/script-modules.php +++ b/lib/experimental/script-modules.php @@ -219,7 +219,7 @@ function gutenberg_register_script_modules() { wp_register_script_module( '@wordpress/a11y', - gutenberg_url( 'build-module/a11y.min.js' ), + gutenberg_url( 'build-module/a11y/index.min.js' ), array(), $default_version ); From 03bdb31df460a8bf38075975827ebc6f3fb53cc1 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 10 Sep 2024 20:10:09 +0200 Subject: [PATCH 11/21] Simplify a11y shared import paths --- packages/a11y/src/shared/index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/a11y/src/shared/index.js b/packages/a11y/src/shared/index.js index 9d2fb5926b4a1..a05f891f42856 100644 --- a/packages/a11y/src/shared/index.js +++ b/packages/a11y/src/shared/index.js @@ -1,10 +1,10 @@ /** * Internal dependencies */ -import addContainer from '../shared/add-container'; -import addIntroText from '../shared/add-intro-text'; -import clear from '../shared/clear'; -import filterMessage from '../shared/filter-message'; +import addContainer from './add-container'; +import addIntroText from './add-intro-text'; +import clear from './clear'; +import filterMessage from './filter-message'; /** * Create the live regions. From 41d105d01b7a2da93fb276397ff9b043df89ba8f Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 10 Sep 2024 20:21:25 +0200 Subject: [PATCH 12/21] Fix a11y tests for restructred package --- packages/a11y/src/{ => shared}/test/add-container.test.js | 0 packages/a11y/src/{ => shared}/test/clear.test.js | 0 .../a11y/src/{ => shared}/test/filter-message.test.js | 0 packages/a11y/src/test/index.test.js | 8 ++++---- 4 files changed, 4 insertions(+), 4 deletions(-) rename packages/a11y/src/{ => shared}/test/add-container.test.js (100%) rename packages/a11y/src/{ => shared}/test/clear.test.js (100%) rename packages/a11y/src/{ => shared}/test/filter-message.test.js (100%) diff --git a/packages/a11y/src/test/add-container.test.js b/packages/a11y/src/shared/test/add-container.test.js similarity index 100% rename from packages/a11y/src/test/add-container.test.js rename to packages/a11y/src/shared/test/add-container.test.js diff --git a/packages/a11y/src/test/clear.test.js b/packages/a11y/src/shared/test/clear.test.js similarity index 100% rename from packages/a11y/src/test/clear.test.js rename to packages/a11y/src/shared/test/clear.test.js diff --git a/packages/a11y/src/test/filter-message.test.js b/packages/a11y/src/shared/test/filter-message.test.js similarity index 100% rename from packages/a11y/src/test/filter-message.test.js rename to packages/a11y/src/shared/test/filter-message.test.js diff --git a/packages/a11y/src/test/index.test.js b/packages/a11y/src/test/index.test.js index 4815baa220504..0f6b9d0bd572e 100644 --- a/packages/a11y/src/test/index.test.js +++ b/packages/a11y/src/test/index.test.js @@ -7,10 +7,10 @@ import domReady from '@wordpress/dom-ready'; * Internal dependencies */ import { setup, speak } from '../'; -import clear from '../clear'; -import filterMessage from '../filter-message'; +import clear from '../shared/clear'; +import filterMessage from '../shared/filter-message'; -jest.mock( '../clear', () => { +jest.mock( '../shared/clear', () => { return jest.fn(); } ); jest.mock( '@wordpress/dom-ready', () => { @@ -18,7 +18,7 @@ jest.mock( '@wordpress/dom-ready', () => { callback(); } ); } ); -jest.mock( '../filter-message', () => { +jest.mock( '../shared/filter-message', () => { return jest.fn( ( message ) => { return message; } ); From 90d7d1095856c875d91bf59bee1cec0dfa63a01b Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 11 Sep 2024 13:27:23 +0200 Subject: [PATCH 13/21] Update script module registration PHPDoc --- lib/experimental/script-modules.php | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/lib/experimental/script-modules.php b/lib/experimental/script-modules.php index 49385dc8e0e88..ea313053eed11 100644 --- a/lib/experimental/script-modules.php +++ b/lib/experimental/script-modules.php @@ -202,15 +202,9 @@ function gutenberg_dequeue_module( $module_identifier ) { } /** - * Registers vendor JavaScript files to be used as dependencies of the editor - * and plugins. + * Registers Gutenberg Script Modules. * - * This function is called from a script during the plugin build process, so it - * should not call any WordPress PHP functions. - * - * @since 13.0 - * - * @param WP_Scripts $scripts WP_Scripts instance. + * @since 19.3 */ function gutenberg_register_script_modules() { // When in production, use the plugin's version as the default asset version; From f3db5bc096909c745c7981ef6c57438df51ea1e2 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 11 Sep 2024 16:11:09 +0200 Subject: [PATCH 14/21] Remove 'gutenberg' text domain from __ --- lib/experimental/script-modules.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/experimental/script-modules.php b/lib/experimental/script-modules.php index ea313053eed11..bf1db3e7cb05b 100644 --- a/lib/experimental/script-modules.php +++ b/lib/experimental/script-modules.php @@ -220,7 +220,7 @@ function gutenberg_register_script_modules() { add_filter( 'script_module_data_@wordpress/a11y', function ( $data ) { - $data['i18n'] = array( 'Notifications' => __( 'Notifications', 'gutenberg' ) ); + $data['i18n'] = array( 'Notifications' => __( 'Notifications' ) ); return $data; } ); From d3b54f4750219a9be7a45a6411abbd88d23b4a0a Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 12 Sep 2024 11:42:41 +0200 Subject: [PATCH 15/21] Use mts/mjs extension for wp module --- packages/a11y/package.json | 2 +- packages/a11y/src/module/{index.ts => index.mts} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename packages/a11y/src/module/{index.ts => index.mts} (100%) diff --git a/packages/a11y/package.json b/packages/a11y/package.json index 327d6b9ff0716..e2c182b6452c7 100644 --- a/packages/a11y/package.json +++ b/packages/a11y/package.json @@ -28,7 +28,7 @@ "module": "build-module/index.js", "react-native": "src/index", "types": "build-types", - "wpScriptModuleExports": "./build-module/module/index.js", + "wpScriptModuleExports": "./build-module/module/index.mjs", "dependencies": { "@babel/runtime": "^7.16.0", "@wordpress/dom-ready": "file:../dom-ready", diff --git a/packages/a11y/src/module/index.ts b/packages/a11y/src/module/index.mts similarity index 100% rename from packages/a11y/src/module/index.ts rename to packages/a11y/src/module/index.mts From 317247291feda703fc5de094cc82ceea27ee1b99 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 12 Sep 2024 11:52:23 +0200 Subject: [PATCH 16/21] PICKME: Add support for mjs, cjs, mts, and cts extensions --- bin/packages/build-worker.js | 6 +++++- bin/packages/build.js | 2 +- packages/scripts/CHANGELOG.md | 6 +++++- packages/scripts/config/webpack.config.js | 15 ++++++++++++--- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/bin/packages/build-worker.js b/bin/packages/build-worker.js index 06e30efc6c6dc..5c431730fab42 100644 --- a/bin/packages/build-worker.js +++ b/bin/packages/build-worker.js @@ -153,7 +153,7 @@ async function buildJS( file ) { JS_ENVIRONMENTS ) ) { const destPath = getBuildPath( - file.replace( /\.tsx?$/, '.js' ), + file.replace( /\.([mt])?tsx?$/, '.$1js' ), buildDir ); const babelOptions = getBabelConfig( @@ -189,6 +189,10 @@ const BUILD_TASK_BY_EXTENSION = { '.js': buildJS, '.ts': buildJS, '.tsx': buildJS, + '.cjs': buildJS, + '.cts': buildJS, + '.mjs': buildJS, + '.mts': buildJS, }; module.exports = async ( file, callback ) => { diff --git a/bin/packages/build.js b/bin/packages/build.js index 61914bab5700a..a1ad5dba37e23 100755 --- a/bin/packages/build.js +++ b/bin/packages/build.js @@ -215,7 +215,7 @@ if ( files.length ) { stream = glob.stream( [ - `${ PACKAGES_DIR }/*/src/**/*.{js,ts,tsx}`, + `${ PACKAGES_DIR }/*/src/**/*.{js,ts,tsx,mts,mjs,cjs,cts}`, `${ PACKAGES_DIR }/*/src/*.scss`, `${ PACKAGES_DIR }/block-library/src/**/*.js`, `${ PACKAGES_DIR }/block-library/src/*/style.scss`, diff --git a/packages/scripts/CHANGELOG.md b/packages/scripts/CHANGELOG.md index ea8a59d1ad2ad..fd61be5aa3722 100644 --- a/packages/scripts/CHANGELOG.md +++ b/packages/scripts/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Enhancements + +- Add support for mjs, cjs, mts, and cts file extensions ([#65101](https://github.com/WordPress/gutenberg/pull/65101)). + ## 29.0.0 (2024-09-05) ### Breaking Changes @@ -10,7 +14,7 @@ ### Enhancements -- Inlines CSS files imported from other CSS files before optimization in the `build` command ([#61121](https://github.com/WordPress/gutenberg/pull/61121)). +- Inlines CSS files imported from other CSS files before optimization in the `build` command ([#61121](https://github.com/WordPress/gutenberg/pull/61121)). ### Bug Fixes diff --git a/packages/scripts/config/webpack.config.js b/packages/scripts/config/webpack.config.js index 91ef19fc27ed6..6c73114213fce 100644 --- a/packages/scripts/config/webpack.config.js +++ b/packages/scripts/config/webpack.config.js @@ -111,7 +111,16 @@ const baseConfig = { alias: { 'lodash-es': 'lodash', }, - extensions: [ '.jsx', '.ts', '.tsx', '...' ], + extensions: [ + '.jsx', + '.ts', + '.tsx', + '.mjs', + '.mts', + '.cjs', + '.cts', + '...', + ], }, optimization: { // Only concatenate modules in production, when not analyzing bundles. @@ -154,7 +163,7 @@ const baseConfig = { module: { rules: [ { - test: /\.m?(j|t)sx?$/, + test: /\.(m|c)?(j|t)sx?$/, exclude: /node_modules/, use: [ { @@ -209,7 +218,7 @@ const baseConfig = { }, { test: /\.svg$/, - issuer: /\.(j|t)sx?$/, + issuer: /\.(m|c)?(j|t)sx?$/, use: [ '@svgr/webpack', 'url-loader' ], type: 'javascript/auto', }, From 0282bf53264d72d2f8a0142448038bc3427867bc Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 12 Sep 2024 12:00:51 +0200 Subject: [PATCH 17/21] Revert "PICKME: Add support for mjs, cjs, mts, and cts extensions" This reverts commit 317247291feda703fc5de094cc82ceea27ee1b99. --- bin/packages/build-worker.js | 6 +----- bin/packages/build.js | 2 +- packages/scripts/CHANGELOG.md | 6 +----- packages/scripts/config/webpack.config.js | 15 +++------------ 4 files changed, 6 insertions(+), 23 deletions(-) diff --git a/bin/packages/build-worker.js b/bin/packages/build-worker.js index 5c431730fab42..06e30efc6c6dc 100644 --- a/bin/packages/build-worker.js +++ b/bin/packages/build-worker.js @@ -153,7 +153,7 @@ async function buildJS( file ) { JS_ENVIRONMENTS ) ) { const destPath = getBuildPath( - file.replace( /\.([mt])?tsx?$/, '.$1js' ), + file.replace( /\.tsx?$/, '.js' ), buildDir ); const babelOptions = getBabelConfig( @@ -189,10 +189,6 @@ const BUILD_TASK_BY_EXTENSION = { '.js': buildJS, '.ts': buildJS, '.tsx': buildJS, - '.cjs': buildJS, - '.cts': buildJS, - '.mjs': buildJS, - '.mts': buildJS, }; module.exports = async ( file, callback ) => { diff --git a/bin/packages/build.js b/bin/packages/build.js index a1ad5dba37e23..61914bab5700a 100755 --- a/bin/packages/build.js +++ b/bin/packages/build.js @@ -215,7 +215,7 @@ if ( files.length ) { stream = glob.stream( [ - `${ PACKAGES_DIR }/*/src/**/*.{js,ts,tsx,mts,mjs,cjs,cts}`, + `${ PACKAGES_DIR }/*/src/**/*.{js,ts,tsx}`, `${ PACKAGES_DIR }/*/src/*.scss`, `${ PACKAGES_DIR }/block-library/src/**/*.js`, `${ PACKAGES_DIR }/block-library/src/*/style.scss`, diff --git a/packages/scripts/CHANGELOG.md b/packages/scripts/CHANGELOG.md index fd61be5aa3722..ea8a59d1ad2ad 100644 --- a/packages/scripts/CHANGELOG.md +++ b/packages/scripts/CHANGELOG.md @@ -2,10 +2,6 @@ ## Unreleased -### Enhancements - -- Add support for mjs, cjs, mts, and cts file extensions ([#65101](https://github.com/WordPress/gutenberg/pull/65101)). - ## 29.0.0 (2024-09-05) ### Breaking Changes @@ -14,7 +10,7 @@ ### Enhancements -- Inlines CSS files imported from other CSS files before optimization in the `build` command ([#61121](https://github.com/WordPress/gutenberg/pull/61121)). +- Inlines CSS files imported from other CSS files before optimization in the `build` command ([#61121](https://github.com/WordPress/gutenberg/pull/61121)). ### Bug Fixes diff --git a/packages/scripts/config/webpack.config.js b/packages/scripts/config/webpack.config.js index 6c73114213fce..91ef19fc27ed6 100644 --- a/packages/scripts/config/webpack.config.js +++ b/packages/scripts/config/webpack.config.js @@ -111,16 +111,7 @@ const baseConfig = { alias: { 'lodash-es': 'lodash', }, - extensions: [ - '.jsx', - '.ts', - '.tsx', - '.mjs', - '.mts', - '.cjs', - '.cts', - '...', - ], + extensions: [ '.jsx', '.ts', '.tsx', '...' ], }, optimization: { // Only concatenate modules in production, when not analyzing bundles. @@ -163,7 +154,7 @@ const baseConfig = { module: { rules: [ { - test: /\.(m|c)?(j|t)sx?$/, + test: /\.m?(j|t)sx?$/, exclude: /node_modules/, use: [ { @@ -218,7 +209,7 @@ const baseConfig = { }, { test: /\.svg$/, - issuer: /\.(m|c)?(j|t)sx?$/, + issuer: /\.(j|t)sx?$/, use: [ '@svgr/webpack', 'url-loader' ], type: 'javascript/auto', }, From ca3060a766248a71823e8b0a1e7fca3c042b76d2 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 12 Sep 2024 12:00:53 +0200 Subject: [PATCH 18/21] Revert "Use mts/mjs extension for wp module" This reverts commit d3b54f4750219a9be7a45a6411abbd88d23b4a0a. --- packages/a11y/package.json | 2 +- packages/a11y/src/module/{index.mts => index.ts} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename packages/a11y/src/module/{index.mts => index.ts} (100%) diff --git a/packages/a11y/package.json b/packages/a11y/package.json index e2c182b6452c7..327d6b9ff0716 100644 --- a/packages/a11y/package.json +++ b/packages/a11y/package.json @@ -28,7 +28,7 @@ "module": "build-module/index.js", "react-native": "src/index", "types": "build-types", - "wpScriptModuleExports": "./build-module/module/index.mjs", + "wpScriptModuleExports": "./build-module/module/index.js", "dependencies": { "@babel/runtime": "^7.16.0", "@wordpress/dom-ready": "file:../dom-ready", diff --git a/packages/a11y/src/module/index.mts b/packages/a11y/src/module/index.ts similarity index 100% rename from packages/a11y/src/module/index.mts rename to packages/a11y/src/module/index.ts From 6552c6b2386f084a6885338b89878c44ac4649f6 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 12 Sep 2024 12:06:34 +0200 Subject: [PATCH 19/21] Fix typos and improve comment --- packages/a11y/src/module/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/a11y/src/module/index.ts b/packages/a11y/src/module/index.ts index f5e7daaf837b2..a2c87f397f487 100644 --- a/packages/a11y/src/module/index.ts +++ b/packages/a11y/src/module/index.ts @@ -4,8 +4,8 @@ import { makeSetupFunction } from '../shared/index'; export { speak } from '../shared/index'; -// Without an i18n script-module, "Notifications" (the only localized text used in this module) -// will be trasnlated on the server and provided as script-module data. +// Without an i18n Script Module, "Notifications" (the only localized text used in this module) +// will be translated on the server and provided as script-module data. let notificationsText = 'Notifications'; try { const textContent = document.getElementById( From 704a7cc69875eca851d9d8b7c5e336989b8196f8 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 12 Sep 2024 12:15:57 +0200 Subject: [PATCH 20/21] Add script module deregister --- lib/experimental/script-modules.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/experimental/script-modules.php b/lib/experimental/script-modules.php index bf1db3e7cb05b..1b85e39d07940 100644 --- a/lib/experimental/script-modules.php +++ b/lib/experimental/script-modules.php @@ -211,6 +211,7 @@ function gutenberg_register_script_modules() { // else (for development or test) default to use the current time. $default_version = defined( 'GUTENBERG_VERSION' ) && ! SCRIPT_DEBUG ? GUTENBERG_VERSION : time(); + wp_deregister_script_module( '@wordpress/a11y' ); wp_register_script_module( '@wordpress/a11y', gutenberg_url( 'build-module/a11y/index.min.js' ), From 1b86c49013ec204cf980d6cbaa1f0a122a4e7c1e Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 12 Sep 2024 12:22:38 +0200 Subject: [PATCH 21/21] Add default domain for Core translation --- lib/experimental/script-modules.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/experimental/script-modules.php b/lib/experimental/script-modules.php index 1b85e39d07940..a113df02b9d75 100644 --- a/lib/experimental/script-modules.php +++ b/lib/experimental/script-modules.php @@ -221,7 +221,7 @@ function gutenberg_register_script_modules() { add_filter( 'script_module_data_@wordpress/a11y', function ( $data ) { - $data['i18n'] = array( 'Notifications' => __( 'Notifications' ) ); + $data['i18n'] = array( 'Notifications' => __( 'Notifications', 'default' ) ); return $data; } );