Skip to content

Latest commit

 

History

History
94 lines (62 loc) · 3.37 KB

README.md

File metadata and controls

94 lines (62 loc) · 3.37 KB

Mix

A Laravel Mix function for WordPress themes.

The mix function is useful if you want to enable cache busting for your theme asset files (CSS, JavaScript, images, icon sprites). Laravel Mix allows you to create a mix-manifest.json file, which might look like this:

{
    "/css/styles.css": "/css/styles.css?id=6ed48b0b831e80bd7549",
    "/js/scripts.js": "/js/scripts.js?id=1bdd07b944e933aa88aa",
}

The ID parameter is a hash of the file contents that changes every time that you make a change to a file. The mix() and mix_any() functions provided in this package allow you to use these hashed URLs for enqueueing your assets in your WordPress theme.

Installation

You can install the package via Composer:

composer require mindkomm/theme-lib-mix

Usage

The mix function assumes that you have a mix-manifest.json (generated by the version function of Laravel Mix) in the build folder of your theme.

add_action( 'wp_enqueue_scripts', function() {
    wp_enqueue_style(
        'styles',
        mix( 'build/css/styles.css' )
    );
} );

If the mix function can’t find your asset file in the manifest file, it will return the asset URL through get_theme_file_uri as a fallback.

Functions

Name Return Type Summary/Returns
mix string Gets the path to a versioned Mix file in a theme.

Returns: The versioned file URL.
mix_any string Gets the path to a versioned Mix file.

Returns: The versioned file URL.

mix

Gets the path to a versioned Mix file in a theme.

Use this function if you want to load theme dependencies. This function will cache the contents of the manifest file for you. This also means that you can’t work with different mix locations. For that, you’d need to use mix_any().

Inspired by https://www.sitepoint.com/use-laravel-mix-non-laravel-projects/.

since 1.0.0

mix( string $path, string $manifest_directory = build )

Returns: string The versioned file URL.

Name Type Description
$path string The relative path to the file.
$manifest_directory string Optional. Custom path to manifest directory. Default 'build'.

mix_any

Gets the path to a versioned Mix file.

The difference to the mix() function is that for this function, you need to provide the absolute paths to the file and the manifest directory. The benefit is that it’s more versatile and that you can use it for functionality that might not live in a theme, but in a plugin or a symlinked package.

since 1.1.0

mix_any( string $path, string $manifest_directory, string $manifest_name = mix-manifest.json )

Returns: string The versioned file URL.

Name Type Description
$path string The full path to the file.
$manifest_directory string The full path to the manifest directory.
$manifest_name string Optional. The name of the manifest file in $manifest_directory. Default mix-manifest.json.

Support

This is a library that we use at MIND to develop WordPress themes. You’re free to use it, but currently, we don’t provide any support.