Skip to content

Commit

Permalink
Include a way to ignore SCRIPT_DEBUG
Browse files Browse the repository at this point in the history
  • Loading branch information
bordoni committed Jun 12, 2024
1 parent 3e097e1 commit d63e91f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
2 changes: 1 addition & 1 deletion assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Plugin Name: Assets
* Description: Asset library with a plugin bootstrap file for automated testing.
* Version: 1.0.0
* Version: 1.2.3
* Author: StellarWP
* Author URI: https://stellarwp.com
*/
4 changes: 2 additions & 2 deletions src/Assets/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ public function maybe_get_min_file( $url ) {
// Strip the plugin URL and make this relative.
$relative_location = str_replace( $base_url . '/', '', $url );

if ( $script_debug ) {
if ( Config::should_ignore_script_debug() || $script_debug ) {
// Add the actual url after having the min file added.
$urls[] = $relative_location;
}
Expand All @@ -934,7 +934,7 @@ public function maybe_get_min_file( $url ) {
}
}

if ( ! $script_debug ) {
if ( Config::should_ignore_script_debug() || ! $script_debug ) {
// Add the actual url after having the min file added.
$urls[] = $relative_location;
}
Expand Down
29 changes: 29 additions & 0 deletions src/Assets/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ class Config {
*/
protected static array $path_urls = [];

/**
* Determine if we should ignore SCRIPT_DEBUG when including `.min` files in the asset loading.
*
* @var bool
*/
protected static bool $ignore_script_debug = false;

/**
* Gets the hook prefix.
*
Expand Down Expand Up @@ -64,6 +71,15 @@ public static function get_relative_asset_path(): string {
return static::$relative_asset_path;
}

/**
* Gets wether this project shiuld ignore SCRIPT_DEBUG.
*
* @return bool
*/
public static function should_ignore_script_debug(): bool {
return static::$ignore_script_debug;
}

/**
* Gets the root path of the project.
*
Expand Down Expand Up @@ -95,6 +111,8 @@ public static function reset() {
static::$root_path = '';
static::$path_urls = [];
static::$version = '';

static::set_ignore_script_debug( false );
}

/**
Expand Down Expand Up @@ -147,6 +165,17 @@ public static function set_path( string $path ) {
static::$root_path = trailingslashit( $path );
}

/**
* Sets the ignore script debug of the project.
*
* @param bool $value The value to set for ignoring SCRIPT_DEBUG.
*
* @return void
*/
public static function set_ignore_script_debug( bool $value ) {
static::$ignore_script_debug = $value;
}

/**
* Sets the version of the project.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/wpunit/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ public function it_should_locate_minified_versions_of_external_assets() {
}
}

/**
* @test
*/
public function it_should_locate_minified_versions_of_external_assets() {

Asset::add( 'fake3-script', 'fake3.js' )->register();

$this->assertTrue( Assets::init()->exists( 'fake3-script' ) );
$this->assertTrue( wp_script_is( 'fake3-script', 'registered' ) );
$this->assertEquals( 'fake3-script', Assets::init()->get( 'fake3-script' )->get_slug() );

$this->assert_minified_found( 'fake3', true, true, true );
}

/**
* @test
*/
Expand Down
11 changes: 11 additions & 0 deletions tests/wpunit/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ public function should_set_version() {
$this->assertEquals( '1.1.0', Config::get_version() );
}

/**
* @test
*/
public function should_set_ignore_script_debug() {
Config::set_ignore_script_debug( true );

$this->assertTrue( Config::should_ignore_script_debug() );
}

/**
* @test
*/
Expand All @@ -58,10 +67,12 @@ public function should_reset() {
Config::set_version( '1.1.0' );
Config::set_path( dirname( dirname( __DIR__ ) ) );
Config::set_relative_asset_path( 'src/resources/' );
Config::set_ignore_script_debug( true );
Config::reset();

$this->assertEquals( 'src/assets/', Config::get_relative_asset_path() );
$this->assertEquals( '', Config::get_version() );
$this->assertFalse( Config::should_ignore_script_debug() );

try {
Config::get_hook_prefix();
Expand Down

0 comments on commit d63e91f

Please sign in to comment.