-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.php
126 lines (105 loc) · 4.03 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* Laravel Mix helper for the Kirby CMS
*/
Kirby::plugin('diverently/laravel-mix-kirby', []);
if (! function_exists('mix')) {
/**
* Get the appropriate HTML tag with the right path for the (versioned) Mix file.
*
* @param string|array $path Path as it appears in the mix-manifest.json or an
* array of paths to look for
* @param string|bool|array $options Pass an array of attributes for the tag
* or a string/bool. A string behaves in the same way as in Kirby's `css()`
* and `js()` helper functions: for css files it will be used as the value
* of the media attribute, for js files it will determine wether or not the
* script is async.
* @return string Either a <link> or a <script> tag, depending on the $path
*
* @throws \Exception
*/
function mix($path, $options = null)
{
$kirby = kirby();
// Handle arrays
if (is_array($path)) {
$assets = [];
foreach($path as $p) {
$assets[] = mix($p, $options);
}
return implode(PHP_EOL, $assets) . PHP_EOL;
}
static $manifest = [];
$isAuto = Str::contains($path, '@auto');
// Get the correct $path
if (!Str::startsWith($path, '/') && !$isAuto) {
$path = "/{$path}";
}
// Get the correct $manifestPath
$manifestPath = option('diverently.laravel-mix-kirby.manifestPath', 'assets/mix-manifest.json');
if (Str::startsWith($manifestPath, '/')) {
$manifestPath = Str::substr($manifestPath, 1);
}
// Get the correct $assetsDirectory
$assetsDirectory = option('diverently.laravel-mix-kirby.assetsDirectory', '/assets');
// Get the manifest contents
if (!$manifest) {
if (! F::exists($manifestPath)) {
if (option('debug')) {
throw new Exception('The Mix manifest does not exist.');
} else {
return false;
}
}
$manifest = json_decode(F::read($manifestPath), 'json');
}
// Get auto templates
if ($isAuto) {
if ($path == '@autocss') {
$type = 'css';
} else if($path == '@autojs') {
$type = 'js';
} else {
if(option('debug')) {
throw new Exception("File type not recognized");
} else {
return false;
}
}
$path = '/'.$type.'/templates/'.$kirby->site()->page()->intendedTemplate().'.'.$type;
}
// Check if the manifest contains the given $path
if (!array_key_exists($path, $manifest)) {
if (option('debug') && !$isAuto) {
throw new Exception("Unable to locate Mix file: {$path}.");
} else {
return false;
}
}
// Check if Mix is in hmr mode
$mixHmrFile = $kirby->root('index') . $assetsDirectory . '/hot';
// Get the actual file path for the given $path
if (F::exists($mixHmrFile)) {
// Remove white space from string and remove ending forward slash
$hmrHost = preg_replace('/\s+/', '', F::read($mixHmrFile));
$hmrHost = substr($hmrHost, 0, -1);
$mixFilePath = $hmrHost . $manifest[$path];
} else {
$mixFilePath = $assetsDirectory . $manifest[$path];
}
// Use the appropriate Kirby helper method to get the correct HTML tag
$pathExtension = F::extension($mixFilePath);
if (Str::contains($pathExtension, 'css')) {
$mixFileLink = css($mixFilePath, $options);
} elseif (Str::contains($pathExtension, 'js')) {
$mixFileLink = js($mixFilePath, $options);
} else {
if (option('debug')) {
throw new Exception("File type not recognized");
} else {
return false;
}
}
return $mixFileLink;
}
}