mirror of
https://github.com/flarum/core.git
synced 2025-07-23 09:41:26 +02:00
Frontend refactor (#1471)
Refactor Frontend + Asset code - Use Laravel's Filesystem component for asset IO, meaning theoretically assets should be storable on S3 etc. - More reliable checking for asset recompilation when debug mode is on, so you don't have to constantly delete the compiled assets to force a recompile. Should also fix issues with locale JS files being recompiled with the same name and cached. - Remove JavaScript minification, because it will be done by Webpack (exception is for the TextFormatter JS). - Add support for JS sourcemaps. - Separate frontend view and assets completely. This is an important distinction because frontend assets are compiled independent of a request, whereas putting together a view depends on a request. - Bind frontend view/asset factory instances to the container (in service providers) rather than subclassing. Asset and content populators can be added to these factories – these are simply objects that populate the asset compilers or the view with information. - Add RouteHandlerFactory functions that make it easy to hook up a frontend controller with a frontend instance ± some content. - Remove the need for "nojs" - Fix cache:clear command - Recompile assets when settings/enabled extensions change
This commit is contained in:
@@ -12,29 +12,37 @@
|
||||
namespace Flarum\Extend;
|
||||
|
||||
use Flarum\Extension\Extension;
|
||||
use Flarum\Frontend\Event\Rendering;
|
||||
use Flarum\Frontend\Asset\ExtensionAssets;
|
||||
use Flarum\Frontend\CompilerFactory;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Illuminate\Events\Dispatcher;
|
||||
|
||||
class Assets implements ExtenderInterface
|
||||
{
|
||||
protected $appName;
|
||||
protected $frontend;
|
||||
|
||||
protected $assets = [];
|
||||
protected $css = [];
|
||||
protected $js;
|
||||
|
||||
public function __construct($appName)
|
||||
public function __construct($frontend)
|
||||
{
|
||||
$this->appName = $appName;
|
||||
$this->frontend = $frontend;
|
||||
}
|
||||
|
||||
public function asset($path)
|
||||
public function css($path)
|
||||
{
|
||||
$this->assets[] = $path;
|
||||
$this->css[] = $path;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
public function asset($path)
|
||||
{
|
||||
return $this->css($path);
|
||||
}
|
||||
|
||||
public function js($path)
|
||||
{
|
||||
$this->js = $path;
|
||||
@@ -44,35 +52,13 @@ class Assets implements ExtenderInterface
|
||||
|
||||
public function __invoke(Container $container, Extension $extension = null)
|
||||
{
|
||||
$container->make(Dispatcher::class)->listen(
|
||||
Rendering::class,
|
||||
function (Rendering $event) use ($extension) {
|
||||
if (! $this->matches($event)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$event->addAssets($this->assets);
|
||||
|
||||
if ($this->js) {
|
||||
$event->view->getJs()->addString(function () use ($extension) {
|
||||
$name = $extension->getId();
|
||||
|
||||
return 'var module={};'.file_get_contents($this->js).";\nflarum.extensions['$name']=module.exports";
|
||||
});
|
||||
}
|
||||
$container->resolving(
|
||||
"flarum.$this->frontend.assets",
|
||||
function (CompilerFactory $assets) use ($extension) {
|
||||
$assets->add(function () use ($extension) {
|
||||
return new ExtensionAssets($extension, $this->css, $this->js);
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private function matches(Rendering $event)
|
||||
{
|
||||
switch ($this->appName) {
|
||||
case 'admin':
|
||||
return $event->isAdmin();
|
||||
case 'forum':
|
||||
return $event->isForum();
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user