Skip to content

Instantly share code, notes, and snippets.

@LasseRafn
Last active March 8, 2017 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LasseRafn/705501bf676edb54310d8a5088616b53 to your computer and use it in GitHub Desktop.
Save LasseRafn/705501bf676edb54310d8a5088616b53 to your computer and use it in GitHub Desktop.
<?php
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;
$publicPath = $_SERVER['DOCUMENT_ROOT'];
if ($manifestDirectory && ! starts_with($manifestDirectory, '/')) {
$manifestDirectory = "/{$manifestDirectory}";
}
if (! $manifest) {
if (! file_exists($manifestPath = ($manifestDirectory.'/mix-manifest.json') )) {
throw new Exception('The Mix manifest does not exist.');
}
$manifest = json_decode(file_get_contents($manifestPath), true);
}
if (! starts_with($path, '/')) {
$path = "/{$path}";
}
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.'
);
}
return file_exists($publicPath . ($manifestDirectory.'/hot'))
? new Illuminate\Support\HtmlString("http://localhost:8080{$manifest[$path]}")
: new Illuminate\Support\HtmlString($manifestDirectory.$manifest[$path]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment