Skip to content

Instantly share code, notes, and snippets.

@jdltechworks
Created November 5, 2018 03:09
Show Gist options
  • Save jdltechworks/d219183805826f99302a6fe861971523 to your computer and use it in GitHub Desktop.
Save jdltechworks/d219183805826f99302a6fe861971523 to your computer and use it in GitHub Desktop.
{
"app.css": "/css/app.1886ffa0.chunk.css",
"app.js": "/js/app.274af2d6.chunk.js",
"app.js.map": "/js/app.274af2d6.chunk.js.map",
"runtime-app.js": "/js/runtime-app.3e6cdd74.js",
"runtime-app.js.map": "/js/runtime-app.3e6cdd74.js.map",
"css/app.1886ffa0.chunk.css.map": "/css/app.1886ffa0.chunk.css.map",
"precache-manifest.af9e5948aed3b3d8242fe41cdde87545.js": "/precache-manifest.af9e5948aed3b3d8242fe41cdde87545.js",
"service-worker.js": "/service-worker.js"
}
<?php
namespace React;
class ReactBladeDirective {
/**
* Manifest file to read
*/
protected $manifest_files = '';
/**
* CRA generated assets
*/
protected $assets = [];
protected $css_regex = '/css/';
protected $js_regex = '/js/';
protected $exclude = '/(map|html|service-worker)/';
public function __construct() {
$this->public_path = public_path('asset-manifest.json');
$this->manifest_file = file_get_contents($this->public_path);
$this->assets_array = json_decode($this->manifest_file, true);
$this->assets = collect($this->assets_array)->reject(function ($key, $value) {
return preg_match($this->exclude, $key);
});
}
public function css($only) {
$css = $this->assets->reject((function($key, $value) {
return preg_match($this->js_regex, $key);
}))->map(function ($url) {
return '<link rel="stylesheet" type="text/css" href="' . $url . '" />';
})->toArray();
return implode(' ', $css);
}
public function js($only) {
$js = $this->assets->reject((function($key, $value) {
return preg_match($this->css_regex, $value);
}))->map(function ($url) {
return '<script src="' . $url . '"></script>';
})->reverse()->toArray();
return implode(' ', $js);
}
}
namespace React;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
class ReactServiceProvider extends ServiceProvider
{
public function boot()
{
Blade::directive('css', function ($expression) {
return "<?php echo app('" . ReactBladeDirective::class . "')->css($expression); ?>";
});
Blade::directive('js', function ($expression) {
return "<?php echo app('" . ReactBladeDirective::class . "')->js($expression); ?>";
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment