Skip to content

Commit

Permalink
Add mix_any()
Browse files Browse the repository at this point in the history
  • Loading branch information
gchtr committed Feb 8, 2019
1 parent f37cc14 commit 603dc67
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 28 deletions.
44 changes: 37 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The mix function is useful if you want to enable **cache busting** for your them
}
```

The ID parameter is a hash of the file contents that changes every time that you make a change to a file. The `mix()` function provided in this package allows you to use these hashed URLs for enqueueing your assets in your WordPress theme.
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

Expand All @@ -38,19 +38,26 @@ If the mix function can’t find your asset file in the manifest file, it will r

## Functions

| Name | Summary | Type | Returns/Description |
| --- | --- | --- | --- |
| [mix](#mix) | Get the path to a versioned Mix file. | `string` | The file URL. |
| Name | Return Type | Summary/Returns |
| --- | --- | --- |
| [mix](#mix) | `string` | Gets the path to a versioned Mix file in a theme.<br><br>*Returns:* The versioned file URL. |
| [mix_any](#mix_any) | `string` | Gets the path to a versioned Mix file.<br><br>*Returns:* The versioned file URL. |

### mix

<p class="summary">Gets the path to a versioned Mix file.</p>
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/>
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 file URL.
**Returns:** `string` The versioned file URL.

| Name | Type | Description |
| --- | --- | --- |
Expand All @@ -59,6 +66,29 @@ Inspired by <https://www.sitepoint.com/use-laravel-mix-non-laravel-projects/>

---

### 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.
36 changes: 18 additions & 18 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
{
"name": "mindkomm/theme-lib-mix",
"type": "library",
"description": "A Laravel Mix function for WordPress themes",
"license": "MIT",
"require": {
"php": ">=7.0.0"
},
"autoload": {
"files": [
"mix.php"
]
},
"authors": [
{
"name": "Lukas Gaechter",
"email": "[email protected]",
"homepage": "https://www.mind.ch"
}
"name": "mindkomm/theme-lib-mix",
"type": "library",
"description": "Laravel Mix functions for WordPress projects",
"license": "MIT",
"require": {
"php": ">=7.0.0"
},
"autoload": {
"files": [
"mix.php"
]
},
"authors": [
{
"name": "Lukas Gaechter",
"email": "[email protected]",
"homepage": "https://www.mind.ch"
}
]
}
61 changes: 58 additions & 3 deletions mix.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
<?php

/**
* Gets the path to a versioned Mix file.
* 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 cant work with different mix locations.
* For that, youd need to use `mix_any()`.
*
* Inspired by <https://www.sitepoint.com/use-laravel-mix-non-laravel-projects/>
* Inspired by <https://www.sitepoint.com/use-laravel-mix-non-laravel-projects/>.
*
* @since 1.0.0
*
* @param string $path The relative path to the file.
* @param string $manifest_directory Optional. Custom path to manifest directory. Default 'build'.
*
* @return string The file URL.
* @return string The versioned file URL.
*/
function mix( $path, $manifest_directory = 'build' ) {
static $manifest;
Expand Down Expand Up @@ -47,3 +51,54 @@ function mix( $path, $manifest_directory = 'build' ) {

return get_theme_file_uri( trailingslashit( $manifest_directory ) . $path );
}

/**
* 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 its 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
*
* @param string $path The full path to the file.
* @param string $manifest_directory The full path to the manifest directory.
* @param string $manifest_name Optional. The name of the manifest file in
* `$manifest_directory`. Default
* `mix-manifest.json`.
* @return string The versioned file URL.
*/
function mix_any( $path, $manifest_directory, $manifest_name = 'mix-manifest.json' ) {
$file_url = str_replace(
trailingslashit( ABSPATH ),
trailingslashit( site_url() ),
$path
);

$manifest_path = trailingslashit( $manifest_directory ) . $manifest_name;

// Bailout with file URL if manifest couldnt be found.
if ( ! file_exists( $manifest_path ) ) {
return $file_url;
}

// @codingStandardsIgnoreLine
$manifest = json_decode( file_get_contents( $manifest_path ), true );
$manifest_entry = str_replace( $manifest_directory, '', $path );

// Make sure theres a leading slash.
$manifest_entry = '/' . ltrim( $manifest_entry, '/' );

// Bailout with file URL could not be found in manifest.
if ( ! array_key_exists( $manifest_entry, $manifest ) ) {
return $file_url;
}

$file_path = $manifest[ $manifest_entry ];
// Make sure theres a leading slash.
$file_path = '/' . ltrim( $file_path, '/' );

// Add hash and return.
return str_replace( $manifest_entry, $file_path, $file_url );
}

0 comments on commit 603dc67

Please sign in to comment.