Skip to content

Functions with signatures more similar to those in WordPress #21

@johnbillion

Description

@johnbillion

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 Support CSS-only entrypoints. #1 by silently rewriting a .css path to .js (which is working for theme stylesheets on the SW project)
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:

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationContains notes which should be added to public-facing documentation

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions