wp/wp-includes/script-modules.php
changeset 22 8c2e4d02f4ef
parent 21 48c4eec2b7e6
equal deleted inserted replaced
21:48c4eec2b7e6 22:8c2e4d02f4ef
   121  * @param string $id The identifier of the script module.
   121  * @param string $id The identifier of the script module.
   122  */
   122  */
   123 function wp_deregister_script_module( string $id ) {
   123 function wp_deregister_script_module( string $id ) {
   124 	wp_script_modules()->deregister( $id );
   124 	wp_script_modules()->deregister( $id );
   125 }
   125 }
       
   126 
       
   127 /**
       
   128  * Registers all the default WordPress Script Modules.
       
   129  *
       
   130  * @since 6.7.0
       
   131  */
       
   132 function wp_default_script_modules() {
       
   133 	$suffix = defined( 'WP_RUN_CORE_TESTS' ) ? '.min' : wp_scripts_get_suffix();
       
   134 
       
   135 	/*
       
   136 	 * Expects multidimensional array like:
       
   137 	 *
       
   138 	 *     'interactivity/index.min.js' => array('dependencies' => array(…), 'version' => '…'),
       
   139 	 *     'interactivity/debug.min.js' => array('dependencies' => array(…), 'version' => '…'),
       
   140 	 *     'interactivity-router/index.min.js' => …
       
   141 	 */
       
   142 	$assets = include ABSPATH . WPINC . "/assets/script-modules-packages{$suffix}.php";
       
   143 
       
   144 	foreach ( $assets as $file_name => $script_module_data ) {
       
   145 		/*
       
   146 		 * Build the WordPress Script Module ID from the file name.
       
   147 		 * Prepend `@wordpress/` and remove extensions and `/index` if present:
       
   148 		 *   - interactivity/index.min.js  => @wordpress/interactivity
       
   149 		 *   - interactivity/debug.min.js  => @wordpress/interactivity/debug
       
   150 		 *   - block-library/query/view.js => @wordpress/block-library/query/view
       
   151 		 */
       
   152 		$script_module_id = '@wordpress/' . preg_replace( '~(?:/index)?(?:\.min)?\.js$~D', '', $file_name, 1 );
       
   153 
       
   154 		switch ( $script_module_id ) {
       
   155 			/*
       
   156 			 * Interactivity exposes two entrypoints, "/index" and "/debug".
       
   157 			 * "/debug" should replace "/index" in development.
       
   158 			 */
       
   159 			case '@wordpress/interactivity/debug':
       
   160 				if ( ! SCRIPT_DEBUG ) {
       
   161 					continue 2;
       
   162 				}
       
   163 				$script_module_id = '@wordpress/interactivity';
       
   164 				break;
       
   165 			case '@wordpress/interactivity':
       
   166 				if ( SCRIPT_DEBUG ) {
       
   167 					continue 2;
       
   168 				}
       
   169 				break;
       
   170 		}
       
   171 
       
   172 		$path = includes_url( "js/dist/script-modules/{$file_name}" );
       
   173 		wp_register_script_module( $script_module_id, $path, $script_module_data['dependencies'], $script_module_data['version'] );
       
   174 	}
       
   175 }