humanmade / humanmade/asset-loader
Functions with signatures more similar to those in WordPress
- Dominant language
- PHP
- Stars
- 26
- Forks
- 2
- PR merge metrics
- No merged PRs in 30d
Description
When switching from using `wp_enqueue_script()` and co to using this package's `autoenqueue()`, the difference in the function interfaces is a little jarring. I'd like to recommend the addition of two helper functions which:
* Use signatures a little more familiar to WordPress developers in order to make the transition easier
* Remove the need to specify an asset manifest path which is IMO an implementation detail
* Get around #1 by silently rewriting a `.css` path to `.js` (which is working for theme stylesheets on the SW project)
```php
function register_asset( string $handle, string $file, array $options = [] ) :? array {
$file = str_replace( '.css', '.js', $file );
$manifest = dirname( $file ) . '/build/asset-manifest.json';
$options['handle'] = $handle;
return autoregister( $manifest, basename( $file ), $options );
}
function enqueue_asset( string $handle, string $file, array $options = [] ) : void {
$file = str_replace( '.css', '.js', $file );
$manifest = dirname( $file ) . '/build/asset-manifest.json';
$options['handle'] = $handle;
autoenqueue( $manifest, basename( $file ), $options );
}
```
Usage looks like:
```php
enqueue_asset(
'theme-style',
get_stylesheet_directory(). '/style.css',
[
'styles' => [ 'dashicons' ],
]
);
```
Thoughts? I can work on a PR if we agree these would be useful additions.
Contributor guide
Assessment
This issue has not been assessed yet.