WordPress/wp-includes/script-modules.php

126 lines
5.9 KiB
PHP
Raw Normal View History

JavaScript: Add new Modules API. This changeset adds a new API for WordPress, designed to work with native ES Modules and Import Maps. It introduces functions such as `wp_register_module`, and `wp_enqueue_module`. The API aims to provide a familiar experience to the existing `WP_Scripts` class, offering similar functionality. However, **it's not intended to duplicate the exact functionality of `WP_Scripts`**; rather, it is carefully tailored to address the specific needs and capabilities of ES modules. For this initial version, **the current proposal is intentionally simplistic**, covering only the essential features needed to work with ES modules. Other enhancements and optimizations can be added later as the community identifies additional requirements and use cases. == Differences Between WP_Script_Modules and WP_Scripts === Dependency Specification With `WP_Script_Modules`, the array of dependencies supports not only strings but also arrays that include the dependency import type (`static` or `dynamic`). This design choice allows for future extensions of dependency properties, such as adding a `version` property to support "scopes" within import maps. === Module Identifier Instead of a handle, `WP_Script_Modules` utilizes the module identifier, aligning with the module identifiers used in JavaScript files and import maps. === Deregistration There is no equivalent of `wp_deregister_script` at this stage. == API === `wp_register_module( $module_identifier, $src, $deps, $version )` Registers a module. {{{ // Registers a module with dependencies and versioning. wp_register_module( 'my-module', '/path/to/my-module.js', array( 'static-dependency-1', 'static-dependency-2' ), '1.2.3' ); }}} {{{ // my-module.js import { ... } from 'static-dependency-1'; import { ... } from 'static-dependency-2'; // ... }}} {{{ // Registers a module with a dynamic dependency. wp_register_module( 'my-module', '/path/to/my-module.js', array( 'static-dependency', array( 'id' => 'dynamic-dependency', 'import' => 'dynamic' ), ) ); }}} {{{ // my-module.js import { ... } from 'static-dependency'; // ... const dynamicModule = await import('dynamic-dependency'); }}} === `wp_enqueue_module( $module_identifier, $src, $deps, $version )` Enqueues a module. If a source is provided, it will also register the module. {{{ wp_enqueue_module( 'my-module' ); }}} === `wp_dequeue_module( $module_identifier )` Dequeues a module. {{{ wp_dequeue_module( 'my-module' ); }}} == Output - When modules are enqueued, they are printed within script tags containing `type="module"` attributes. - Additionally, static dependencies of enqueued modules utilize `link` tags with `rel="modulepreload"` attributes. - Lastly, an import map is generated and inserted using a `<script type="importmap">` tag. {{{ <script type="module" src="/path/to/my-module.js" id="my-module"></script> <link rel="modulepreload" href="/path/to/static-dependency.js" id="static-dependency" /> <script type="importmap"> { "imports": { "static-dependency": "/path/to/static-dependency.js", "dynamic-dependency": "/path/to/dynamic-dependency.js" } } </script> }}} == Import Map Polyfill Requirement Even though all major browsers already support import maps, an import map polyfill is required until the percentage of users using old browser versions without import map support drops significantly. This work is ongoing and will be added once it's ready. Progress is tracked in #60232. Props luisherranz, idad5, costdev, neffff, joemcgill, jorbin, swissspidy, jonsurrell, flixos90, gziolo, westonruter. Fixes #56313. Built from https://develop.svn.wordpress.org/trunk@57269 git-svn-id: http://core.svn.wordpress.org/trunk@56775 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-11 09:47:14 -05:00
<?php
/**
* Script Modules API: Script Module functions
JavaScript: Add new Modules API. This changeset adds a new API for WordPress, designed to work with native ES Modules and Import Maps. It introduces functions such as `wp_register_module`, and `wp_enqueue_module`. The API aims to provide a familiar experience to the existing `WP_Scripts` class, offering similar functionality. However, **it's not intended to duplicate the exact functionality of `WP_Scripts`**; rather, it is carefully tailored to address the specific needs and capabilities of ES modules. For this initial version, **the current proposal is intentionally simplistic**, covering only the essential features needed to work with ES modules. Other enhancements and optimizations can be added later as the community identifies additional requirements and use cases. == Differences Between WP_Script_Modules and WP_Scripts === Dependency Specification With `WP_Script_Modules`, the array of dependencies supports not only strings but also arrays that include the dependency import type (`static` or `dynamic`). This design choice allows for future extensions of dependency properties, such as adding a `version` property to support "scopes" within import maps. === Module Identifier Instead of a handle, `WP_Script_Modules` utilizes the module identifier, aligning with the module identifiers used in JavaScript files and import maps. === Deregistration There is no equivalent of `wp_deregister_script` at this stage. == API === `wp_register_module( $module_identifier, $src, $deps, $version )` Registers a module. {{{ // Registers a module with dependencies and versioning. wp_register_module( 'my-module', '/path/to/my-module.js', array( 'static-dependency-1', 'static-dependency-2' ), '1.2.3' ); }}} {{{ // my-module.js import { ... } from 'static-dependency-1'; import { ... } from 'static-dependency-2'; // ... }}} {{{ // Registers a module with a dynamic dependency. wp_register_module( 'my-module', '/path/to/my-module.js', array( 'static-dependency', array( 'id' => 'dynamic-dependency', 'import' => 'dynamic' ), ) ); }}} {{{ // my-module.js import { ... } from 'static-dependency'; // ... const dynamicModule = await import('dynamic-dependency'); }}} === `wp_enqueue_module( $module_identifier, $src, $deps, $version )` Enqueues a module. If a source is provided, it will also register the module. {{{ wp_enqueue_module( 'my-module' ); }}} === `wp_dequeue_module( $module_identifier )` Dequeues a module. {{{ wp_dequeue_module( 'my-module' ); }}} == Output - When modules are enqueued, they are printed within script tags containing `type="module"` attributes. - Additionally, static dependencies of enqueued modules utilize `link` tags with `rel="modulepreload"` attributes. - Lastly, an import map is generated and inserted using a `<script type="importmap">` tag. {{{ <script type="module" src="/path/to/my-module.js" id="my-module"></script> <link rel="modulepreload" href="/path/to/static-dependency.js" id="static-dependency" /> <script type="importmap"> { "imports": { "static-dependency": "/path/to/static-dependency.js", "dynamic-dependency": "/path/to/dynamic-dependency.js" } } </script> }}} == Import Map Polyfill Requirement Even though all major browsers already support import maps, an import map polyfill is required until the percentage of users using old browser versions without import map support drops significantly. This work is ongoing and will be added once it's ready. Progress is tracked in #60232. Props luisherranz, idad5, costdev, neffff, joemcgill, jorbin, swissspidy, jonsurrell, flixos90, gziolo, westonruter. Fixes #56313. Built from https://develop.svn.wordpress.org/trunk@57269 git-svn-id: http://core.svn.wordpress.org/trunk@56775 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-11 09:47:14 -05:00
*
* @since 6.5.0
*
* @package WordPress
* @subpackage Script Modules
*/
/**
* Retrieves the main WP_Script_Modules instance.
*
* This function provides access to the WP_Script_Modules instance, creating one
* if it doesn't exist yet.
*
* @global WP_Script_Modules $wp_script_modules
*
JavaScript: Add new Modules API. This changeset adds a new API for WordPress, designed to work with native ES Modules and Import Maps. It introduces functions such as `wp_register_module`, and `wp_enqueue_module`. The API aims to provide a familiar experience to the existing `WP_Scripts` class, offering similar functionality. However, **it's not intended to duplicate the exact functionality of `WP_Scripts`**; rather, it is carefully tailored to address the specific needs and capabilities of ES modules. For this initial version, **the current proposal is intentionally simplistic**, covering only the essential features needed to work with ES modules. Other enhancements and optimizations can be added later as the community identifies additional requirements and use cases. == Differences Between WP_Script_Modules and WP_Scripts === Dependency Specification With `WP_Script_Modules`, the array of dependencies supports not only strings but also arrays that include the dependency import type (`static` or `dynamic`). This design choice allows for future extensions of dependency properties, such as adding a `version` property to support "scopes" within import maps. === Module Identifier Instead of a handle, `WP_Script_Modules` utilizes the module identifier, aligning with the module identifiers used in JavaScript files and import maps. === Deregistration There is no equivalent of `wp_deregister_script` at this stage. == API === `wp_register_module( $module_identifier, $src, $deps, $version )` Registers a module. {{{ // Registers a module with dependencies and versioning. wp_register_module( 'my-module', '/path/to/my-module.js', array( 'static-dependency-1', 'static-dependency-2' ), '1.2.3' ); }}} {{{ // my-module.js import { ... } from 'static-dependency-1'; import { ... } from 'static-dependency-2'; // ... }}} {{{ // Registers a module with a dynamic dependency. wp_register_module( 'my-module', '/path/to/my-module.js', array( 'static-dependency', array( 'id' => 'dynamic-dependency', 'import' => 'dynamic' ), ) ); }}} {{{ // my-module.js import { ... } from 'static-dependency'; // ... const dynamicModule = await import('dynamic-dependency'); }}} === `wp_enqueue_module( $module_identifier, $src, $deps, $version )` Enqueues a module. If a source is provided, it will also register the module. {{{ wp_enqueue_module( 'my-module' ); }}} === `wp_dequeue_module( $module_identifier )` Dequeues a module. {{{ wp_dequeue_module( 'my-module' ); }}} == Output - When modules are enqueued, they are printed within script tags containing `type="module"` attributes. - Additionally, static dependencies of enqueued modules utilize `link` tags with `rel="modulepreload"` attributes. - Lastly, an import map is generated and inserted using a `<script type="importmap">` tag. {{{ <script type="module" src="/path/to/my-module.js" id="my-module"></script> <link rel="modulepreload" href="/path/to/static-dependency.js" id="static-dependency" /> <script type="importmap"> { "imports": { "static-dependency": "/path/to/static-dependency.js", "dynamic-dependency": "/path/to/dynamic-dependency.js" } } </script> }}} == Import Map Polyfill Requirement Even though all major browsers already support import maps, an import map polyfill is required until the percentage of users using old browser versions without import map support drops significantly. This work is ongoing and will be added once it's ready. Progress is tracked in #60232. Props luisherranz, idad5, costdev, neffff, joemcgill, jorbin, swissspidy, jonsurrell, flixos90, gziolo, westonruter. Fixes #56313. Built from https://develop.svn.wordpress.org/trunk@57269 git-svn-id: http://core.svn.wordpress.org/trunk@56775 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-11 09:47:14 -05:00
* @since 6.5.0
*
* @return WP_Script_Modules The main WP_Script_Modules instance.
*/
function wp_script_modules(): WP_Script_Modules {
global $wp_script_modules;
if ( ! ( $wp_script_modules instanceof WP_Script_Modules ) ) {
$wp_script_modules = new WP_Script_Modules();
JavaScript: Add new Modules API. This changeset adds a new API for WordPress, designed to work with native ES Modules and Import Maps. It introduces functions such as `wp_register_module`, and `wp_enqueue_module`. The API aims to provide a familiar experience to the existing `WP_Scripts` class, offering similar functionality. However, **it's not intended to duplicate the exact functionality of `WP_Scripts`**; rather, it is carefully tailored to address the specific needs and capabilities of ES modules. For this initial version, **the current proposal is intentionally simplistic**, covering only the essential features needed to work with ES modules. Other enhancements and optimizations can be added later as the community identifies additional requirements and use cases. == Differences Between WP_Script_Modules and WP_Scripts === Dependency Specification With `WP_Script_Modules`, the array of dependencies supports not only strings but also arrays that include the dependency import type (`static` or `dynamic`). This design choice allows for future extensions of dependency properties, such as adding a `version` property to support "scopes" within import maps. === Module Identifier Instead of a handle, `WP_Script_Modules` utilizes the module identifier, aligning with the module identifiers used in JavaScript files and import maps. === Deregistration There is no equivalent of `wp_deregister_script` at this stage. == API === `wp_register_module( $module_identifier, $src, $deps, $version )` Registers a module. {{{ // Registers a module with dependencies and versioning. wp_register_module( 'my-module', '/path/to/my-module.js', array( 'static-dependency-1', 'static-dependency-2' ), '1.2.3' ); }}} {{{ // my-module.js import { ... } from 'static-dependency-1'; import { ... } from 'static-dependency-2'; // ... }}} {{{ // Registers a module with a dynamic dependency. wp_register_module( 'my-module', '/path/to/my-module.js', array( 'static-dependency', array( 'id' => 'dynamic-dependency', 'import' => 'dynamic' ), ) ); }}} {{{ // my-module.js import { ... } from 'static-dependency'; // ... const dynamicModule = await import('dynamic-dependency'); }}} === `wp_enqueue_module( $module_identifier, $src, $deps, $version )` Enqueues a module. If a source is provided, it will also register the module. {{{ wp_enqueue_module( 'my-module' ); }}} === `wp_dequeue_module( $module_identifier )` Dequeues a module. {{{ wp_dequeue_module( 'my-module' ); }}} == Output - When modules are enqueued, they are printed within script tags containing `type="module"` attributes. - Additionally, static dependencies of enqueued modules utilize `link` tags with `rel="modulepreload"` attributes. - Lastly, an import map is generated and inserted using a `<script type="importmap">` tag. {{{ <script type="module" src="/path/to/my-module.js" id="my-module"></script> <link rel="modulepreload" href="/path/to/static-dependency.js" id="static-dependency" /> <script type="importmap"> { "imports": { "static-dependency": "/path/to/static-dependency.js", "dynamic-dependency": "/path/to/dynamic-dependency.js" } } </script> }}} == Import Map Polyfill Requirement Even though all major browsers already support import maps, an import map polyfill is required until the percentage of users using old browser versions without import map support drops significantly. This work is ongoing and will be added once it's ready. Progress is tracked in #60232. Props luisherranz, idad5, costdev, neffff, joemcgill, jorbin, swissspidy, jonsurrell, flixos90, gziolo, westonruter. Fixes #56313. Built from https://develop.svn.wordpress.org/trunk@57269 git-svn-id: http://core.svn.wordpress.org/trunk@56775 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-11 09:47:14 -05:00
}
return $wp_script_modules;
JavaScript: Add new Modules API. This changeset adds a new API for WordPress, designed to work with native ES Modules and Import Maps. It introduces functions such as `wp_register_module`, and `wp_enqueue_module`. The API aims to provide a familiar experience to the existing `WP_Scripts` class, offering similar functionality. However, **it's not intended to duplicate the exact functionality of `WP_Scripts`**; rather, it is carefully tailored to address the specific needs and capabilities of ES modules. For this initial version, **the current proposal is intentionally simplistic**, covering only the essential features needed to work with ES modules. Other enhancements and optimizations can be added later as the community identifies additional requirements and use cases. == Differences Between WP_Script_Modules and WP_Scripts === Dependency Specification With `WP_Script_Modules`, the array of dependencies supports not only strings but also arrays that include the dependency import type (`static` or `dynamic`). This design choice allows for future extensions of dependency properties, such as adding a `version` property to support "scopes" within import maps. === Module Identifier Instead of a handle, `WP_Script_Modules` utilizes the module identifier, aligning with the module identifiers used in JavaScript files and import maps. === Deregistration There is no equivalent of `wp_deregister_script` at this stage. == API === `wp_register_module( $module_identifier, $src, $deps, $version )` Registers a module. {{{ // Registers a module with dependencies and versioning. wp_register_module( 'my-module', '/path/to/my-module.js', array( 'static-dependency-1', 'static-dependency-2' ), '1.2.3' ); }}} {{{ // my-module.js import { ... } from 'static-dependency-1'; import { ... } from 'static-dependency-2'; // ... }}} {{{ // Registers a module with a dynamic dependency. wp_register_module( 'my-module', '/path/to/my-module.js', array( 'static-dependency', array( 'id' => 'dynamic-dependency', 'import' => 'dynamic' ), ) ); }}} {{{ // my-module.js import { ... } from 'static-dependency'; // ... const dynamicModule = await import('dynamic-dependency'); }}} === `wp_enqueue_module( $module_identifier, $src, $deps, $version )` Enqueues a module. If a source is provided, it will also register the module. {{{ wp_enqueue_module( 'my-module' ); }}} === `wp_dequeue_module( $module_identifier )` Dequeues a module. {{{ wp_dequeue_module( 'my-module' ); }}} == Output - When modules are enqueued, they are printed within script tags containing `type="module"` attributes. - Additionally, static dependencies of enqueued modules utilize `link` tags with `rel="modulepreload"` attributes. - Lastly, an import map is generated and inserted using a `<script type="importmap">` tag. {{{ <script type="module" src="/path/to/my-module.js" id="my-module"></script> <link rel="modulepreload" href="/path/to/static-dependency.js" id="static-dependency" /> <script type="importmap"> { "imports": { "static-dependency": "/path/to/static-dependency.js", "dynamic-dependency": "/path/to/dynamic-dependency.js" } } </script> }}} == Import Map Polyfill Requirement Even though all major browsers already support import maps, an import map polyfill is required until the percentage of users using old browser versions without import map support drops significantly. This work is ongoing and will be added once it's ready. Progress is tracked in #60232. Props luisherranz, idad5, costdev, neffff, joemcgill, jorbin, swissspidy, jonsurrell, flixos90, gziolo, westonruter. Fixes #56313. Built from https://develop.svn.wordpress.org/trunk@57269 git-svn-id: http://core.svn.wordpress.org/trunk@56775 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-11 09:47:14 -05:00
}
/**
* Registers the script module if no script module with that script module
* identifier has already been registered.
JavaScript: Add new Modules API. This changeset adds a new API for WordPress, designed to work with native ES Modules and Import Maps. It introduces functions such as `wp_register_module`, and `wp_enqueue_module`. The API aims to provide a familiar experience to the existing `WP_Scripts` class, offering similar functionality. However, **it's not intended to duplicate the exact functionality of `WP_Scripts`**; rather, it is carefully tailored to address the specific needs and capabilities of ES modules. For this initial version, **the current proposal is intentionally simplistic**, covering only the essential features needed to work with ES modules. Other enhancements and optimizations can be added later as the community identifies additional requirements and use cases. == Differences Between WP_Script_Modules and WP_Scripts === Dependency Specification With `WP_Script_Modules`, the array of dependencies supports not only strings but also arrays that include the dependency import type (`static` or `dynamic`). This design choice allows for future extensions of dependency properties, such as adding a `version` property to support "scopes" within import maps. === Module Identifier Instead of a handle, `WP_Script_Modules` utilizes the module identifier, aligning with the module identifiers used in JavaScript files and import maps. === Deregistration There is no equivalent of `wp_deregister_script` at this stage. == API === `wp_register_module( $module_identifier, $src, $deps, $version )` Registers a module. {{{ // Registers a module with dependencies and versioning. wp_register_module( 'my-module', '/path/to/my-module.js', array( 'static-dependency-1', 'static-dependency-2' ), '1.2.3' ); }}} {{{ // my-module.js import { ... } from 'static-dependency-1'; import { ... } from 'static-dependency-2'; // ... }}} {{{ // Registers a module with a dynamic dependency. wp_register_module( 'my-module', '/path/to/my-module.js', array( 'static-dependency', array( 'id' => 'dynamic-dependency', 'import' => 'dynamic' ), ) ); }}} {{{ // my-module.js import { ... } from 'static-dependency'; // ... const dynamicModule = await import('dynamic-dependency'); }}} === `wp_enqueue_module( $module_identifier, $src, $deps, $version )` Enqueues a module. If a source is provided, it will also register the module. {{{ wp_enqueue_module( 'my-module' ); }}} === `wp_dequeue_module( $module_identifier )` Dequeues a module. {{{ wp_dequeue_module( 'my-module' ); }}} == Output - When modules are enqueued, they are printed within script tags containing `type="module"` attributes. - Additionally, static dependencies of enqueued modules utilize `link` tags with `rel="modulepreload"` attributes. - Lastly, an import map is generated and inserted using a `<script type="importmap">` tag. {{{ <script type="module" src="/path/to/my-module.js" id="my-module"></script> <link rel="modulepreload" href="/path/to/static-dependency.js" id="static-dependency" /> <script type="importmap"> { "imports": { "static-dependency": "/path/to/static-dependency.js", "dynamic-dependency": "/path/to/dynamic-dependency.js" } } </script> }}} == Import Map Polyfill Requirement Even though all major browsers already support import maps, an import map polyfill is required until the percentage of users using old browser versions without import map support drops significantly. This work is ongoing and will be added once it's ready. Progress is tracked in #60232. Props luisherranz, idad5, costdev, neffff, joemcgill, jorbin, swissspidy, jonsurrell, flixos90, gziolo, westonruter. Fixes #56313. Built from https://develop.svn.wordpress.org/trunk@57269 git-svn-id: http://core.svn.wordpress.org/trunk@56775 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-11 09:47:14 -05:00
*
* @since 6.5.0
*
* @param string $id The identifier of the script module. Should be unique. It will be used in the
* final import map.
* @param string $src Optional. Full URL of the script module, or path of the script module relative
* to the WordPress root directory. If it is provided and the script module has
* not been registered yet, it will be registered.
* @param array $deps {
* Optional. List of dependencies.
*
* @type string|array ...$0 {
* An array of script module identifiers of the dependencies of this script
* module. The dependencies can be strings or arrays. If they are arrays,
* they need an `id` key with the script module identifier, and can contain
* an `import` key with either `static` or `dynamic`. By default,
* dependencies that don't contain an `import` key are considered static.
*
* @type string $id The script module identifier.
* @type string $import Optional. Import type. May be either `static` or
* `dynamic`. Defaults to `static`.
* }
* }
* @param string|false|null $version Optional. String specifying the script module version number. Defaults to false.
* It is added to the URL as a query string for cache busting purposes. If $version
* is set to false, the version number is the currently installed WordPress version.
* If $version is set to null, no version is added.
JavaScript: Add new Modules API. This changeset adds a new API for WordPress, designed to work with native ES Modules and Import Maps. It introduces functions such as `wp_register_module`, and `wp_enqueue_module`. The API aims to provide a familiar experience to the existing `WP_Scripts` class, offering similar functionality. However, **it's not intended to duplicate the exact functionality of `WP_Scripts`**; rather, it is carefully tailored to address the specific needs and capabilities of ES modules. For this initial version, **the current proposal is intentionally simplistic**, covering only the essential features needed to work with ES modules. Other enhancements and optimizations can be added later as the community identifies additional requirements and use cases. == Differences Between WP_Script_Modules and WP_Scripts === Dependency Specification With `WP_Script_Modules`, the array of dependencies supports not only strings but also arrays that include the dependency import type (`static` or `dynamic`). This design choice allows for future extensions of dependency properties, such as adding a `version` property to support "scopes" within import maps. === Module Identifier Instead of a handle, `WP_Script_Modules` utilizes the module identifier, aligning with the module identifiers used in JavaScript files and import maps. === Deregistration There is no equivalent of `wp_deregister_script` at this stage. == API === `wp_register_module( $module_identifier, $src, $deps, $version )` Registers a module. {{{ // Registers a module with dependencies and versioning. wp_register_module( 'my-module', '/path/to/my-module.js', array( 'static-dependency-1', 'static-dependency-2' ), '1.2.3' ); }}} {{{ // my-module.js import { ... } from 'static-dependency-1'; import { ... } from 'static-dependency-2'; // ... }}} {{{ // Registers a module with a dynamic dependency. wp_register_module( 'my-module', '/path/to/my-module.js', array( 'static-dependency', array( 'id' => 'dynamic-dependency', 'import' => 'dynamic' ), ) ); }}} {{{ // my-module.js import { ... } from 'static-dependency'; // ... const dynamicModule = await import('dynamic-dependency'); }}} === `wp_enqueue_module( $module_identifier, $src, $deps, $version )` Enqueues a module. If a source is provided, it will also register the module. {{{ wp_enqueue_module( 'my-module' ); }}} === `wp_dequeue_module( $module_identifier )` Dequeues a module. {{{ wp_dequeue_module( 'my-module' ); }}} == Output - When modules are enqueued, they are printed within script tags containing `type="module"` attributes. - Additionally, static dependencies of enqueued modules utilize `link` tags with `rel="modulepreload"` attributes. - Lastly, an import map is generated and inserted using a `<script type="importmap">` tag. {{{ <script type="module" src="/path/to/my-module.js" id="my-module"></script> <link rel="modulepreload" href="/path/to/static-dependency.js" id="static-dependency" /> <script type="importmap"> { "imports": { "static-dependency": "/path/to/static-dependency.js", "dynamic-dependency": "/path/to/dynamic-dependency.js" } } </script> }}} == Import Map Polyfill Requirement Even though all major browsers already support import maps, an import map polyfill is required until the percentage of users using old browser versions without import map support drops significantly. This work is ongoing and will be added once it's ready. Progress is tracked in #60232. Props luisherranz, idad5, costdev, neffff, joemcgill, jorbin, swissspidy, jonsurrell, flixos90, gziolo, westonruter. Fixes #56313. Built from https://develop.svn.wordpress.org/trunk@57269 git-svn-id: http://core.svn.wordpress.org/trunk@56775 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-11 09:47:14 -05:00
*/
function wp_register_script_module( string $id, string $src, array $deps = array(), $version = false ) {
wp_script_modules()->register( $id, $src, $deps, $version );
JavaScript: Add new Modules API. This changeset adds a new API for WordPress, designed to work with native ES Modules and Import Maps. It introduces functions such as `wp_register_module`, and `wp_enqueue_module`. The API aims to provide a familiar experience to the existing `WP_Scripts` class, offering similar functionality. However, **it's not intended to duplicate the exact functionality of `WP_Scripts`**; rather, it is carefully tailored to address the specific needs and capabilities of ES modules. For this initial version, **the current proposal is intentionally simplistic**, covering only the essential features needed to work with ES modules. Other enhancements and optimizations can be added later as the community identifies additional requirements and use cases. == Differences Between WP_Script_Modules and WP_Scripts === Dependency Specification With `WP_Script_Modules`, the array of dependencies supports not only strings but also arrays that include the dependency import type (`static` or `dynamic`). This design choice allows for future extensions of dependency properties, such as adding a `version` property to support "scopes" within import maps. === Module Identifier Instead of a handle, `WP_Script_Modules` utilizes the module identifier, aligning with the module identifiers used in JavaScript files and import maps. === Deregistration There is no equivalent of `wp_deregister_script` at this stage. == API === `wp_register_module( $module_identifier, $src, $deps, $version )` Registers a module. {{{ // Registers a module with dependencies and versioning. wp_register_module( 'my-module', '/path/to/my-module.js', array( 'static-dependency-1', 'static-dependency-2' ), '1.2.3' ); }}} {{{ // my-module.js import { ... } from 'static-dependency-1'; import { ... } from 'static-dependency-2'; // ... }}} {{{ // Registers a module with a dynamic dependency. wp_register_module( 'my-module', '/path/to/my-module.js', array( 'static-dependency', array( 'id' => 'dynamic-dependency', 'import' => 'dynamic' ), ) ); }}} {{{ // my-module.js import { ... } from 'static-dependency'; // ... const dynamicModule = await import('dynamic-dependency'); }}} === `wp_enqueue_module( $module_identifier, $src, $deps, $version )` Enqueues a module. If a source is provided, it will also register the module. {{{ wp_enqueue_module( 'my-module' ); }}} === `wp_dequeue_module( $module_identifier )` Dequeues a module. {{{ wp_dequeue_module( 'my-module' ); }}} == Output - When modules are enqueued, they are printed within script tags containing `type="module"` attributes. - Additionally, static dependencies of enqueued modules utilize `link` tags with `rel="modulepreload"` attributes. - Lastly, an import map is generated and inserted using a `<script type="importmap">` tag. {{{ <script type="module" src="/path/to/my-module.js" id="my-module"></script> <link rel="modulepreload" href="/path/to/static-dependency.js" id="static-dependency" /> <script type="importmap"> { "imports": { "static-dependency": "/path/to/static-dependency.js", "dynamic-dependency": "/path/to/dynamic-dependency.js" } } </script> }}} == Import Map Polyfill Requirement Even though all major browsers already support import maps, an import map polyfill is required until the percentage of users using old browser versions without import map support drops significantly. This work is ongoing and will be added once it's ready. Progress is tracked in #60232. Props luisherranz, idad5, costdev, neffff, joemcgill, jorbin, swissspidy, jonsurrell, flixos90, gziolo, westonruter. Fixes #56313. Built from https://develop.svn.wordpress.org/trunk@57269 git-svn-id: http://core.svn.wordpress.org/trunk@56775 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-11 09:47:14 -05:00
}
/**
* Marks the script module to be enqueued in the page.
JavaScript: Add new Modules API. This changeset adds a new API for WordPress, designed to work with native ES Modules and Import Maps. It introduces functions such as `wp_register_module`, and `wp_enqueue_module`. The API aims to provide a familiar experience to the existing `WP_Scripts` class, offering similar functionality. However, **it's not intended to duplicate the exact functionality of `WP_Scripts`**; rather, it is carefully tailored to address the specific needs and capabilities of ES modules. For this initial version, **the current proposal is intentionally simplistic**, covering only the essential features needed to work with ES modules. Other enhancements and optimizations can be added later as the community identifies additional requirements and use cases. == Differences Between WP_Script_Modules and WP_Scripts === Dependency Specification With `WP_Script_Modules`, the array of dependencies supports not only strings but also arrays that include the dependency import type (`static` or `dynamic`). This design choice allows for future extensions of dependency properties, such as adding a `version` property to support "scopes" within import maps. === Module Identifier Instead of a handle, `WP_Script_Modules` utilizes the module identifier, aligning with the module identifiers used in JavaScript files and import maps. === Deregistration There is no equivalent of `wp_deregister_script` at this stage. == API === `wp_register_module( $module_identifier, $src, $deps, $version )` Registers a module. {{{ // Registers a module with dependencies and versioning. wp_register_module( 'my-module', '/path/to/my-module.js', array( 'static-dependency-1', 'static-dependency-2' ), '1.2.3' ); }}} {{{ // my-module.js import { ... } from 'static-dependency-1'; import { ... } from 'static-dependency-2'; // ... }}} {{{ // Registers a module with a dynamic dependency. wp_register_module( 'my-module', '/path/to/my-module.js', array( 'static-dependency', array( 'id' => 'dynamic-dependency', 'import' => 'dynamic' ), ) ); }}} {{{ // my-module.js import { ... } from 'static-dependency'; // ... const dynamicModule = await import('dynamic-dependency'); }}} === `wp_enqueue_module( $module_identifier, $src, $deps, $version )` Enqueues a module. If a source is provided, it will also register the module. {{{ wp_enqueue_module( 'my-module' ); }}} === `wp_dequeue_module( $module_identifier )` Dequeues a module. {{{ wp_dequeue_module( 'my-module' ); }}} == Output - When modules are enqueued, they are printed within script tags containing `type="module"` attributes. - Additionally, static dependencies of enqueued modules utilize `link` tags with `rel="modulepreload"` attributes. - Lastly, an import map is generated and inserted using a `<script type="importmap">` tag. {{{ <script type="module" src="/path/to/my-module.js" id="my-module"></script> <link rel="modulepreload" href="/path/to/static-dependency.js" id="static-dependency" /> <script type="importmap"> { "imports": { "static-dependency": "/path/to/static-dependency.js", "dynamic-dependency": "/path/to/dynamic-dependency.js" } } </script> }}} == Import Map Polyfill Requirement Even though all major browsers already support import maps, an import map polyfill is required until the percentage of users using old browser versions without import map support drops significantly. This work is ongoing and will be added once it's ready. Progress is tracked in #60232. Props luisherranz, idad5, costdev, neffff, joemcgill, jorbin, swissspidy, jonsurrell, flixos90, gziolo, westonruter. Fixes #56313. Built from https://develop.svn.wordpress.org/trunk@57269 git-svn-id: http://core.svn.wordpress.org/trunk@56775 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-11 09:47:14 -05:00
*
* If a src is provided and the script module has not been registered yet, it
* will be registered.
JavaScript: Add new Modules API. This changeset adds a new API for WordPress, designed to work with native ES Modules and Import Maps. It introduces functions such as `wp_register_module`, and `wp_enqueue_module`. The API aims to provide a familiar experience to the existing `WP_Scripts` class, offering similar functionality. However, **it's not intended to duplicate the exact functionality of `WP_Scripts`**; rather, it is carefully tailored to address the specific needs and capabilities of ES modules. For this initial version, **the current proposal is intentionally simplistic**, covering only the essential features needed to work with ES modules. Other enhancements and optimizations can be added later as the community identifies additional requirements and use cases. == Differences Between WP_Script_Modules and WP_Scripts === Dependency Specification With `WP_Script_Modules`, the array of dependencies supports not only strings but also arrays that include the dependency import type (`static` or `dynamic`). This design choice allows for future extensions of dependency properties, such as adding a `version` property to support "scopes" within import maps. === Module Identifier Instead of a handle, `WP_Script_Modules` utilizes the module identifier, aligning with the module identifiers used in JavaScript files and import maps. === Deregistration There is no equivalent of `wp_deregister_script` at this stage. == API === `wp_register_module( $module_identifier, $src, $deps, $version )` Registers a module. {{{ // Registers a module with dependencies and versioning. wp_register_module( 'my-module', '/path/to/my-module.js', array( 'static-dependency-1', 'static-dependency-2' ), '1.2.3' ); }}} {{{ // my-module.js import { ... } from 'static-dependency-1'; import { ... } from 'static-dependency-2'; // ... }}} {{{ // Registers a module with a dynamic dependency. wp_register_module( 'my-module', '/path/to/my-module.js', array( 'static-dependency', array( 'id' => 'dynamic-dependency', 'import' => 'dynamic' ), ) ); }}} {{{ // my-module.js import { ... } from 'static-dependency'; // ... const dynamicModule = await import('dynamic-dependency'); }}} === `wp_enqueue_module( $module_identifier, $src, $deps, $version )` Enqueues a module. If a source is provided, it will also register the module. {{{ wp_enqueue_module( 'my-module' ); }}} === `wp_dequeue_module( $module_identifier )` Dequeues a module. {{{ wp_dequeue_module( 'my-module' ); }}} == Output - When modules are enqueued, they are printed within script tags containing `type="module"` attributes. - Additionally, static dependencies of enqueued modules utilize `link` tags with `rel="modulepreload"` attributes. - Lastly, an import map is generated and inserted using a `<script type="importmap">` tag. {{{ <script type="module" src="/path/to/my-module.js" id="my-module"></script> <link rel="modulepreload" href="/path/to/static-dependency.js" id="static-dependency" /> <script type="importmap"> { "imports": { "static-dependency": "/path/to/static-dependency.js", "dynamic-dependency": "/path/to/dynamic-dependency.js" } } </script> }}} == Import Map Polyfill Requirement Even though all major browsers already support import maps, an import map polyfill is required until the percentage of users using old browser versions without import map support drops significantly. This work is ongoing and will be added once it's ready. Progress is tracked in #60232. Props luisherranz, idad5, costdev, neffff, joemcgill, jorbin, swissspidy, jonsurrell, flixos90, gziolo, westonruter. Fixes #56313. Built from https://develop.svn.wordpress.org/trunk@57269 git-svn-id: http://core.svn.wordpress.org/trunk@56775 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-11 09:47:14 -05:00
*
* @since 6.5.0
*
* @param string $id The identifier of the script module. Should be unique. It will be used in the
* final import map.
* @param string $src Optional. Full URL of the script module, or path of the script module relative
* to the WordPress root directory. If it is provided and the script module has
* not been registered yet, it will be registered.
* @param array $deps {
* Optional. List of dependencies.
*
* @type string|array ...$0 {
* An array of script module identifiers of the dependencies of this script
* module. The dependencies can be strings or arrays. If they are arrays,
* they need an `id` key with the script module identifier, and can contain
* an `import` key with either `static` or `dynamic`. By default,
* dependencies that don't contain an `import` key are considered static.
*
* @type string $id The script module identifier.
* @type string $import Optional. Import type. May be either `static` or
* `dynamic`. Defaults to `static`.
* }
* }
* @param string|false|null $version Optional. String specifying the script module version number. Defaults to false.
* It is added to the URL as a query string for cache busting purposes. If $version
* is set to false, the version number is the currently installed WordPress version.
* If $version is set to null, no version is added.
JavaScript: Add new Modules API. This changeset adds a new API for WordPress, designed to work with native ES Modules and Import Maps. It introduces functions such as `wp_register_module`, and `wp_enqueue_module`. The API aims to provide a familiar experience to the existing `WP_Scripts` class, offering similar functionality. However, **it's not intended to duplicate the exact functionality of `WP_Scripts`**; rather, it is carefully tailored to address the specific needs and capabilities of ES modules. For this initial version, **the current proposal is intentionally simplistic**, covering only the essential features needed to work with ES modules. Other enhancements and optimizations can be added later as the community identifies additional requirements and use cases. == Differences Between WP_Script_Modules and WP_Scripts === Dependency Specification With `WP_Script_Modules`, the array of dependencies supports not only strings but also arrays that include the dependency import type (`static` or `dynamic`). This design choice allows for future extensions of dependency properties, such as adding a `version` property to support "scopes" within import maps. === Module Identifier Instead of a handle, `WP_Script_Modules` utilizes the module identifier, aligning with the module identifiers used in JavaScript files and import maps. === Deregistration There is no equivalent of `wp_deregister_script` at this stage. == API === `wp_register_module( $module_identifier, $src, $deps, $version )` Registers a module. {{{ // Registers a module with dependencies and versioning. wp_register_module( 'my-module', '/path/to/my-module.js', array( 'static-dependency-1', 'static-dependency-2' ), '1.2.3' ); }}} {{{ // my-module.js import { ... } from 'static-dependency-1'; import { ... } from 'static-dependency-2'; // ... }}} {{{ // Registers a module with a dynamic dependency. wp_register_module( 'my-module', '/path/to/my-module.js', array( 'static-dependency', array( 'id' => 'dynamic-dependency', 'import' => 'dynamic' ), ) ); }}} {{{ // my-module.js import { ... } from 'static-dependency'; // ... const dynamicModule = await import('dynamic-dependency'); }}} === `wp_enqueue_module( $module_identifier, $src, $deps, $version )` Enqueues a module. If a source is provided, it will also register the module. {{{ wp_enqueue_module( 'my-module' ); }}} === `wp_dequeue_module( $module_identifier )` Dequeues a module. {{{ wp_dequeue_module( 'my-module' ); }}} == Output - When modules are enqueued, they are printed within script tags containing `type="module"` attributes. - Additionally, static dependencies of enqueued modules utilize `link` tags with `rel="modulepreload"` attributes. - Lastly, an import map is generated and inserted using a `<script type="importmap">` tag. {{{ <script type="module" src="/path/to/my-module.js" id="my-module"></script> <link rel="modulepreload" href="/path/to/static-dependency.js" id="static-dependency" /> <script type="importmap"> { "imports": { "static-dependency": "/path/to/static-dependency.js", "dynamic-dependency": "/path/to/dynamic-dependency.js" } } </script> }}} == Import Map Polyfill Requirement Even though all major browsers already support import maps, an import map polyfill is required until the percentage of users using old browser versions without import map support drops significantly. This work is ongoing and will be added once it's ready. Progress is tracked in #60232. Props luisherranz, idad5, costdev, neffff, joemcgill, jorbin, swissspidy, jonsurrell, flixos90, gziolo, westonruter. Fixes #56313. Built from https://develop.svn.wordpress.org/trunk@57269 git-svn-id: http://core.svn.wordpress.org/trunk@56775 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-11 09:47:14 -05:00
*/
function wp_enqueue_script_module( string $id, string $src = '', array $deps = array(), $version = false ) {
wp_script_modules()->enqueue( $id, $src, $deps, $version );
JavaScript: Add new Modules API. This changeset adds a new API for WordPress, designed to work with native ES Modules and Import Maps. It introduces functions such as `wp_register_module`, and `wp_enqueue_module`. The API aims to provide a familiar experience to the existing `WP_Scripts` class, offering similar functionality. However, **it's not intended to duplicate the exact functionality of `WP_Scripts`**; rather, it is carefully tailored to address the specific needs and capabilities of ES modules. For this initial version, **the current proposal is intentionally simplistic**, covering only the essential features needed to work with ES modules. Other enhancements and optimizations can be added later as the community identifies additional requirements and use cases. == Differences Between WP_Script_Modules and WP_Scripts === Dependency Specification With `WP_Script_Modules`, the array of dependencies supports not only strings but also arrays that include the dependency import type (`static` or `dynamic`). This design choice allows for future extensions of dependency properties, such as adding a `version` property to support "scopes" within import maps. === Module Identifier Instead of a handle, `WP_Script_Modules` utilizes the module identifier, aligning with the module identifiers used in JavaScript files and import maps. === Deregistration There is no equivalent of `wp_deregister_script` at this stage. == API === `wp_register_module( $module_identifier, $src, $deps, $version )` Registers a module. {{{ // Registers a module with dependencies and versioning. wp_register_module( 'my-module', '/path/to/my-module.js', array( 'static-dependency-1', 'static-dependency-2' ), '1.2.3' ); }}} {{{ // my-module.js import { ... } from 'static-dependency-1'; import { ... } from 'static-dependency-2'; // ... }}} {{{ // Registers a module with a dynamic dependency. wp_register_module( 'my-module', '/path/to/my-module.js', array( 'static-dependency', array( 'id' => 'dynamic-dependency', 'import' => 'dynamic' ), ) ); }}} {{{ // my-module.js import { ... } from 'static-dependency'; // ... const dynamicModule = await import('dynamic-dependency'); }}} === `wp_enqueue_module( $module_identifier, $src, $deps, $version )` Enqueues a module. If a source is provided, it will also register the module. {{{ wp_enqueue_module( 'my-module' ); }}} === `wp_dequeue_module( $module_identifier )` Dequeues a module. {{{ wp_dequeue_module( 'my-module' ); }}} == Output - When modules are enqueued, they are printed within script tags containing `type="module"` attributes. - Additionally, static dependencies of enqueued modules utilize `link` tags with `rel="modulepreload"` attributes. - Lastly, an import map is generated and inserted using a `<script type="importmap">` tag. {{{ <script type="module" src="/path/to/my-module.js" id="my-module"></script> <link rel="modulepreload" href="/path/to/static-dependency.js" id="static-dependency" /> <script type="importmap"> { "imports": { "static-dependency": "/path/to/static-dependency.js", "dynamic-dependency": "/path/to/dynamic-dependency.js" } } </script> }}} == Import Map Polyfill Requirement Even though all major browsers already support import maps, an import map polyfill is required until the percentage of users using old browser versions without import map support drops significantly. This work is ongoing and will be added once it's ready. Progress is tracked in #60232. Props luisherranz, idad5, costdev, neffff, joemcgill, jorbin, swissspidy, jonsurrell, flixos90, gziolo, westonruter. Fixes #56313. Built from https://develop.svn.wordpress.org/trunk@57269 git-svn-id: http://core.svn.wordpress.org/trunk@56775 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-11 09:47:14 -05:00
}
/**
* Unmarks the script module so it is no longer enqueued in the page.
JavaScript: Add new Modules API. This changeset adds a new API for WordPress, designed to work with native ES Modules and Import Maps. It introduces functions such as `wp_register_module`, and `wp_enqueue_module`. The API aims to provide a familiar experience to the existing `WP_Scripts` class, offering similar functionality. However, **it's not intended to duplicate the exact functionality of `WP_Scripts`**; rather, it is carefully tailored to address the specific needs and capabilities of ES modules. For this initial version, **the current proposal is intentionally simplistic**, covering only the essential features needed to work with ES modules. Other enhancements and optimizations can be added later as the community identifies additional requirements and use cases. == Differences Between WP_Script_Modules and WP_Scripts === Dependency Specification With `WP_Script_Modules`, the array of dependencies supports not only strings but also arrays that include the dependency import type (`static` or `dynamic`). This design choice allows for future extensions of dependency properties, such as adding a `version` property to support "scopes" within import maps. === Module Identifier Instead of a handle, `WP_Script_Modules` utilizes the module identifier, aligning with the module identifiers used in JavaScript files and import maps. === Deregistration There is no equivalent of `wp_deregister_script` at this stage. == API === `wp_register_module( $module_identifier, $src, $deps, $version )` Registers a module. {{{ // Registers a module with dependencies and versioning. wp_register_module( 'my-module', '/path/to/my-module.js', array( 'static-dependency-1', 'static-dependency-2' ), '1.2.3' ); }}} {{{ // my-module.js import { ... } from 'static-dependency-1'; import { ... } from 'static-dependency-2'; // ... }}} {{{ // Registers a module with a dynamic dependency. wp_register_module( 'my-module', '/path/to/my-module.js', array( 'static-dependency', array( 'id' => 'dynamic-dependency', 'import' => 'dynamic' ), ) ); }}} {{{ // my-module.js import { ... } from 'static-dependency'; // ... const dynamicModule = await import('dynamic-dependency'); }}} === `wp_enqueue_module( $module_identifier, $src, $deps, $version )` Enqueues a module. If a source is provided, it will also register the module. {{{ wp_enqueue_module( 'my-module' ); }}} === `wp_dequeue_module( $module_identifier )` Dequeues a module. {{{ wp_dequeue_module( 'my-module' ); }}} == Output - When modules are enqueued, they are printed within script tags containing `type="module"` attributes. - Additionally, static dependencies of enqueued modules utilize `link` tags with `rel="modulepreload"` attributes. - Lastly, an import map is generated and inserted using a `<script type="importmap">` tag. {{{ <script type="module" src="/path/to/my-module.js" id="my-module"></script> <link rel="modulepreload" href="/path/to/static-dependency.js" id="static-dependency" /> <script type="importmap"> { "imports": { "static-dependency": "/path/to/static-dependency.js", "dynamic-dependency": "/path/to/dynamic-dependency.js" } } </script> }}} == Import Map Polyfill Requirement Even though all major browsers already support import maps, an import map polyfill is required until the percentage of users using old browser versions without import map support drops significantly. This work is ongoing and will be added once it's ready. Progress is tracked in #60232. Props luisherranz, idad5, costdev, neffff, joemcgill, jorbin, swissspidy, jonsurrell, flixos90, gziolo, westonruter. Fixes #56313. Built from https://develop.svn.wordpress.org/trunk@57269 git-svn-id: http://core.svn.wordpress.org/trunk@56775 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-11 09:47:14 -05:00
*
* @since 6.5.0
*
* @param string $id The identifier of the script module.
JavaScript: Add new Modules API. This changeset adds a new API for WordPress, designed to work with native ES Modules and Import Maps. It introduces functions such as `wp_register_module`, and `wp_enqueue_module`. The API aims to provide a familiar experience to the existing `WP_Scripts` class, offering similar functionality. However, **it's not intended to duplicate the exact functionality of `WP_Scripts`**; rather, it is carefully tailored to address the specific needs and capabilities of ES modules. For this initial version, **the current proposal is intentionally simplistic**, covering only the essential features needed to work with ES modules. Other enhancements and optimizations can be added later as the community identifies additional requirements and use cases. == Differences Between WP_Script_Modules and WP_Scripts === Dependency Specification With `WP_Script_Modules`, the array of dependencies supports not only strings but also arrays that include the dependency import type (`static` or `dynamic`). This design choice allows for future extensions of dependency properties, such as adding a `version` property to support "scopes" within import maps. === Module Identifier Instead of a handle, `WP_Script_Modules` utilizes the module identifier, aligning with the module identifiers used in JavaScript files and import maps. === Deregistration There is no equivalent of `wp_deregister_script` at this stage. == API === `wp_register_module( $module_identifier, $src, $deps, $version )` Registers a module. {{{ // Registers a module with dependencies and versioning. wp_register_module( 'my-module', '/path/to/my-module.js', array( 'static-dependency-1', 'static-dependency-2' ), '1.2.3' ); }}} {{{ // my-module.js import { ... } from 'static-dependency-1'; import { ... } from 'static-dependency-2'; // ... }}} {{{ // Registers a module with a dynamic dependency. wp_register_module( 'my-module', '/path/to/my-module.js', array( 'static-dependency', array( 'id' => 'dynamic-dependency', 'import' => 'dynamic' ), ) ); }}} {{{ // my-module.js import { ... } from 'static-dependency'; // ... const dynamicModule = await import('dynamic-dependency'); }}} === `wp_enqueue_module( $module_identifier, $src, $deps, $version )` Enqueues a module. If a source is provided, it will also register the module. {{{ wp_enqueue_module( 'my-module' ); }}} === `wp_dequeue_module( $module_identifier )` Dequeues a module. {{{ wp_dequeue_module( 'my-module' ); }}} == Output - When modules are enqueued, they are printed within script tags containing `type="module"` attributes. - Additionally, static dependencies of enqueued modules utilize `link` tags with `rel="modulepreload"` attributes. - Lastly, an import map is generated and inserted using a `<script type="importmap">` tag. {{{ <script type="module" src="/path/to/my-module.js" id="my-module"></script> <link rel="modulepreload" href="/path/to/static-dependency.js" id="static-dependency" /> <script type="importmap"> { "imports": { "static-dependency": "/path/to/static-dependency.js", "dynamic-dependency": "/path/to/dynamic-dependency.js" } } </script> }}} == Import Map Polyfill Requirement Even though all major browsers already support import maps, an import map polyfill is required until the percentage of users using old browser versions without import map support drops significantly. This work is ongoing and will be added once it's ready. Progress is tracked in #60232. Props luisherranz, idad5, costdev, neffff, joemcgill, jorbin, swissspidy, jonsurrell, flixos90, gziolo, westonruter. Fixes #56313. Built from https://develop.svn.wordpress.org/trunk@57269 git-svn-id: http://core.svn.wordpress.org/trunk@56775 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-11 09:47:14 -05:00
*/
function wp_dequeue_script_module( string $id ) {
wp_script_modules()->dequeue( $id );
JavaScript: Add new Modules API. This changeset adds a new API for WordPress, designed to work with native ES Modules and Import Maps. It introduces functions such as `wp_register_module`, and `wp_enqueue_module`. The API aims to provide a familiar experience to the existing `WP_Scripts` class, offering similar functionality. However, **it's not intended to duplicate the exact functionality of `WP_Scripts`**; rather, it is carefully tailored to address the specific needs and capabilities of ES modules. For this initial version, **the current proposal is intentionally simplistic**, covering only the essential features needed to work with ES modules. Other enhancements and optimizations can be added later as the community identifies additional requirements and use cases. == Differences Between WP_Script_Modules and WP_Scripts === Dependency Specification With `WP_Script_Modules`, the array of dependencies supports not only strings but also arrays that include the dependency import type (`static` or `dynamic`). This design choice allows for future extensions of dependency properties, such as adding a `version` property to support "scopes" within import maps. === Module Identifier Instead of a handle, `WP_Script_Modules` utilizes the module identifier, aligning with the module identifiers used in JavaScript files and import maps. === Deregistration There is no equivalent of `wp_deregister_script` at this stage. == API === `wp_register_module( $module_identifier, $src, $deps, $version )` Registers a module. {{{ // Registers a module with dependencies and versioning. wp_register_module( 'my-module', '/path/to/my-module.js', array( 'static-dependency-1', 'static-dependency-2' ), '1.2.3' ); }}} {{{ // my-module.js import { ... } from 'static-dependency-1'; import { ... } from 'static-dependency-2'; // ... }}} {{{ // Registers a module with a dynamic dependency. wp_register_module( 'my-module', '/path/to/my-module.js', array( 'static-dependency', array( 'id' => 'dynamic-dependency', 'import' => 'dynamic' ), ) ); }}} {{{ // my-module.js import { ... } from 'static-dependency'; // ... const dynamicModule = await import('dynamic-dependency'); }}} === `wp_enqueue_module( $module_identifier, $src, $deps, $version )` Enqueues a module. If a source is provided, it will also register the module. {{{ wp_enqueue_module( 'my-module' ); }}} === `wp_dequeue_module( $module_identifier )` Dequeues a module. {{{ wp_dequeue_module( 'my-module' ); }}} == Output - When modules are enqueued, they are printed within script tags containing `type="module"` attributes. - Additionally, static dependencies of enqueued modules utilize `link` tags with `rel="modulepreload"` attributes. - Lastly, an import map is generated and inserted using a `<script type="importmap">` tag. {{{ <script type="module" src="/path/to/my-module.js" id="my-module"></script> <link rel="modulepreload" href="/path/to/static-dependency.js" id="static-dependency" /> <script type="importmap"> { "imports": { "static-dependency": "/path/to/static-dependency.js", "dynamic-dependency": "/path/to/dynamic-dependency.js" } } </script> }}} == Import Map Polyfill Requirement Even though all major browsers already support import maps, an import map polyfill is required until the percentage of users using old browser versions without import map support drops significantly. This work is ongoing and will be added once it's ready. Progress is tracked in #60232. Props luisherranz, idad5, costdev, neffff, joemcgill, jorbin, swissspidy, jonsurrell, flixos90, gziolo, westonruter. Fixes #56313. Built from https://develop.svn.wordpress.org/trunk@57269 git-svn-id: http://core.svn.wordpress.org/trunk@56775 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-11 09:47:14 -05:00
}
/**
* Deregisters the script module.
*
* @since 6.5.0
*
* @param string $id The identifier of the script module.
*/
function wp_deregister_script_module( string $id ) {
wp_script_modules()->deregister( $id );
}