Skip to content

Instantly share code, notes, and snippets.

@torounit
Created November 28, 2018 04:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save torounit/bc411028f7942542e003c64ed08bb2d3 to your computer and use it in GitHub Desktop.
Save torounit/bc411028f7942542e003c64ed08bb2d3 to your computer and use it in GitHub Desktop.
WordPress の管理画面で React が圧縮されるとデバッグがしづらいので、圧縮前のヤツを読み込むようにするプラグイン。
<?php
/**
* Plugin Name: Unminify vendor scripts.
*/
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
add_action( 'admin_print_scripts', function () {
global $wp_scripts;
unminify_execute( [
'react',
'react-dom',
], $wp_scripts );
} );
}
/**
* @param string[] $targets
* @param WP_Dependencies $dependencies
*/
function unminify_execute( array $targets, WP_Dependencies $dependencies ) {
foreach ( $targets as $target ) {
if ( ! empty( $dependencies->registered[ $target ] ) ) {
unminify_replace_src( $dependencies->registered[ $target ] );
}
}
}
/**
* @param _WP_Dependency $dependency
*/
function unminify_replace_src( _WP_Dependency $dependency ) {
if ( ! empty( $dependency->src ) && is_string( $dependency->src ) ) {
$dependency->src = str_replace( '.min', '', $dependency->src );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment