forked from sourceboat/wp-laravel-mix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
81 lines (70 loc) · 2.25 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/**
* Plugin Name: WP Laravel Mix
* Plugin URI: https://github.com/sourceboat/wp-laravel-mix/
* Description: Get versioned Laravel Mix file paths in WordPress.
* Version: 1.0.0
* Author: Sourceboat
* Author URI: https://sourceboat.com/
* License: MIT License
*/
if (! function_exists('starts_with')) {
/**
* Determine if a given string starts with a given substring.
*
* @param string $haystack
* @param string|array $needles
* @return bool
*/
function starts_with($haystack, $needles)
{
foreach ((array) $needles as $needle) {
if ($needle != '' && substr($haystack, 0, strlen($needle)) === (string) $needle) {
return true;
}
}
return false;
}
}
if (! function_exists('mix')) {
/**
* Get the path to a versioned Mix file.
*
* @param string $path
* @param string $manifestDirectory
* @return \Illuminate\Support\HtmlString
*
* @throws \Exception
*/
function mix($path, $manifestDirectory = '')
{
static $manifest;
if (! starts_with($path, '/')) {
$path = "/{$path}";
}
if ($manifestDirectory && ! starts_with($manifestDirectory, '/')) {
$manifestDirectory = "/{$manifestDirectory}";
}
$rootDir = get_template_directory();
if (file_exists($rootDir . '/' . $manifestDirectory.'/hot')) {
return "http://localhost:8080" . $path;
}
if (! $manifest) {
$manifestPath = $rootDir . $manifestDirectory . 'mix-manifest.json';
if (! file_exists($manifestPath)) {
throw new Exception('The Mix manifest does not exist.');
}
$manifest = json_decode(file_get_contents($manifestPath), true);
}
if (! array_key_exists($path, $manifest)) {
throw new Exception(
"Unable to locate Mix file: {$path}. Please check your ".
'webpack.mix.js output paths and try again.'
);
}
$path = $manifestDirectory . $manifest[$path];
$path = str_replace('/web', '', $path);
$path = str_replace('//', '/', $path);
return get_template_directory_uri() . $path;
}
}