Skip to content

Commit

Permalink
Add support for *.asset.php files
Browse files Browse the repository at this point in the history
This builds off of the initial code from @jesseeproductions and makes it automatic, along with some safety nets, defaults, and tests.

The additional tests will act as the artifacts for this changeset
  • Loading branch information
borkweb committed Sep 26, 2024
1 parent 2d5339b commit 017e1f5
Show file tree
Hide file tree
Showing 10 changed files with 393 additions and 112 deletions.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ A library for managing asset registration and enqueuing in WordPress.
* [`remove()`](#remove)
* [Advanced topics](#advanced-topics)
* [Minified files](#minified-files)
* [Support for wp-scripts](#support-for-wp-scripts)
* [Default example](#default-example)
* [Overriding the default asset file location](#overriding-the-default-asset-file-location)
* [Conditional enqueuing](#conditional-enqueuing)
* [Firing a callback after enqueuing occurs](#firing-a-callback-after-enqueuing-occurs)
* [Output JS data](#output-js-data)
Expand Down Expand Up @@ -225,6 +228,7 @@ Asset::add( 'my-asset', 'js/some-asset.js', $an_optional_version, $an_optional_p
->set_as_async( true )
->set_as_deferred( true )
->set_as_module( true )
->set_asset_file( 'other-asset-directory/some-asset' ) // This allows you to manually set the path to a *.asset.php file.
->set_condition( // This can be any callable that returns a boolean.
static function() {
return is_front_page() || is_single();
Expand Down Expand Up @@ -357,6 +361,50 @@ Asset::add( 'my-asset', 'js/some-asset.js' )
->register();
```

### Support for wp-scripts

This library supports `*.asset.php` files generated by `wp-scripts` out of the box. It will attempt to find the `*.asset.php` file in the same directory as the asset file you're registering, however, you can manually set the path to the asset file via `::set_asset_file()` if you need to.

#### Default example

Assume you have a `something.asset.php` file in the same directory as your `something.js` file. Within that asset file is the standard asset array that contains `dependencies` and `version` keys.

```php
Asset::add( 'my-thing', 'js/something.js' )
->register();
```

This will automatically use the `something.asset.php` file's `dependencies` and `version` values for the asset.

Shown below is an example of the directory structure:

```
my-plugin/
├── src/
│ ├── assets/
│ │ ├── js/
│ │ │ ├── something.js
│ │ │ └── something.asset.php
```

Within the `something.asset.php` file, you have the following:

```php
<?php return array('dependencies' => array('some-dependency'), 'version' => '5.0.0');
```

#### Overriding the default asset file location

You may need to override the default location of the asset file. You can do this by using the `::set_asset_file()` method.

```php
Asset::add( 'my-thing', 'js/something.js' )
->set_asset_file( 'other-asset-directory/something' )
->register();
```

Note: You can provide the JS file extension (`other-asset-directory/something.js`), the asset file extension (`other-asset-directory/something.asset.php`), or leave it off entirely (`other-asset-directory/something`).

### Conditional enqueuing

It is rare that you will want to enqueue an asset on every page load. Luckily, you can specify a condition for when an
Expand Down
Loading

0 comments on commit 017e1f5

Please sign in to comment.