mirror of
https://github.com/a2nt/silverstripe-webpack.git
synced 2024-10-22 15:05:31 +00:00
Fontawesome updates
This commit is contained in:
parent
f201054f78
commit
f1cdf0fb50
@ -8,7 +8,9 @@ Site\Templates\DeferredRequirements:
|
|||||||
static_domain: false
|
static_domain: false
|
||||||
deferred: true
|
deferred: true
|
||||||
jquery_version: '3.4.1'
|
jquery_version: '3.4.1'
|
||||||
fontawesome_version: '5.10.2'
|
|
||||||
|
SilverStripe\FontAwesome\FontAwesomeField:
|
||||||
|
version: '5.12.0'
|
||||||
|
|
||||||
SilverStripe\View\Requirements:
|
SilverStripe\View\Requirements:
|
||||||
disable_flush_combined: true
|
disable_flush_combined: true
|
||||||
|
@ -10,6 +10,7 @@ use SilverStripe\View\Requirements;
|
|||||||
use SilverStripe\Core\Config\Config;
|
use SilverStripe\Core\Config\Config;
|
||||||
use SilverStripe\Control\Director;
|
use SilverStripe\Control\Director;
|
||||||
use SilverStripe\Core\Path;
|
use SilverStripe\Core\Path;
|
||||||
|
use SilverStripe\FontAwesome\FontAwesomeField;
|
||||||
|
|
||||||
class DeferredRequirements implements TemplateGlobalProvider
|
class DeferredRequirements implements TemplateGlobalProvider
|
||||||
{
|
{
|
||||||
@ -21,7 +22,6 @@ class DeferredRequirements implements TemplateGlobalProvider
|
|||||||
private static $nojquery = false;
|
private static $nojquery = false;
|
||||||
private static $jquery_version = '3.4.1';
|
private static $jquery_version = '3.4.1';
|
||||||
private static $nofontawesome = false;
|
private static $nofontawesome = false;
|
||||||
private static $fontawesome_version = '5.10.2';
|
|
||||||
private static $custom_requirements = [];
|
private static $custom_requirements = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -33,6 +33,7 @@ class DeferredRequirements implements TemplateGlobalProvider
|
|||||||
'AutoRequirements' => 'Auto',
|
'AutoRequirements' => 'Auto',
|
||||||
'DeferedCSS' => 'loadCSS',
|
'DeferedCSS' => 'loadCSS',
|
||||||
'DeferedJS' => 'loadJS',
|
'DeferedJS' => 'loadJS',
|
||||||
|
'WebpackActive' => 'webpackActive',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,10 +70,11 @@ class DeferredRequirements implements TemplateGlobalProvider
|
|||||||
}
|
}
|
||||||
// App libs
|
// App libs
|
||||||
if (!$config['nofontawesome']) {
|
if (!$config['nofontawesome']) {
|
||||||
self::loadCSS(
|
$v = !isset($config['fontawesome_version']) || !$config['fontawesome_version']
|
||||||
'//use.fontawesome.com/releases/v'
|
? Config::inst()->get(FontAwesomeField::class, 'version')
|
||||||
.$config['fontawesome_version'].'/css/all.css'
|
: $config['fontawesome_version'];
|
||||||
);
|
|
||||||
|
self::loadCSS('//use.fontawesome.com/releases/v'.$v.'/css/all.css');
|
||||||
}
|
}
|
||||||
|
|
||||||
self::loadCSS($mainTheme.'.css');
|
self::loadCSS($mainTheme.'.css');
|
||||||
@ -116,7 +118,7 @@ class DeferredRequirements implements TemplateGlobalProvider
|
|||||||
public static function loadCSS($css): void
|
public static function loadCSS($css): void
|
||||||
{
|
{
|
||||||
$external = (mb_strpos($css, '//') === 0 || mb_strpos($css, 'http') === 0);
|
$external = (mb_strpos($css, '//') === 0 || mb_strpos($css, 'http') === 0);
|
||||||
if ($external || (self::getDeferred() && !self::_webpackActive())) {
|
if ($external || (self::getDeferred() && !self::webpackActive())) {
|
||||||
self::$css[] = $css;
|
self::$css[] = $css;
|
||||||
} else {
|
} else {
|
||||||
WebpackTemplateProvider::loadCSS($css);
|
WebpackTemplateProvider::loadCSS($css);
|
||||||
@ -128,14 +130,14 @@ class DeferredRequirements implements TemplateGlobalProvider
|
|||||||
/*$external = (mb_substr($js, 0, 2) === '//' || mb_substr($js, 0, 4) === 'http');
|
/*$external = (mb_substr($js, 0, 2) === '//' || mb_substr($js, 0, 4) === 'http');
|
||||||
if ($external || (self::getDeferred() && !self::_webpackActive())) {*/
|
if ($external || (self::getDeferred() && !self::_webpackActive())) {*/
|
||||||
// webpack supposed to load external JS
|
// webpack supposed to load external JS
|
||||||
if (self::getDeferred() && !self::_webpackActive()) {
|
if (self::getDeferred() && !self::webpackActive()) {
|
||||||
self::$js[] = $js;
|
self::$js[] = $js;
|
||||||
} else {
|
} else {
|
||||||
WebpackTemplateProvider::loadJS($js);
|
WebpackTemplateProvider::loadJS($js);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static function _webpackActive(): bool
|
public static function webpackActive(): bool
|
||||||
{
|
{
|
||||||
return WebpackTemplateProvider::isActive();
|
return WebpackTemplateProvider::isActive();
|
||||||
}
|
}
|
||||||
|
@ -1,28 +1,36 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/** @noinspection PhpUnusedPrivateFieldInspection */
|
/** @noinspection PhpUnusedPrivateFieldInspection */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Directs assets requests to Webpack server or to static files
|
||||||
|
*/
|
||||||
|
|
||||||
namespace Site\Templates;
|
namespace Site\Templates;
|
||||||
|
|
||||||
use SilverStripe\Control\Controller;
|
use SilverStripe\Core\Manifest\ModuleManifest;
|
||||||
|
use SilverStripe\View\SSViewer;
|
||||||
use SilverStripe\View\TemplateGlobalProvider;
|
use SilverStripe\View\TemplateGlobalProvider;
|
||||||
use SilverStripe\View\Requirements;
|
use SilverStripe\View\Requirements;
|
||||||
use SilverStripe\Core\Config\Config;
|
|
||||||
use SilverStripe\Control\Director;
|
use SilverStripe\Control\Director;
|
||||||
use SilverStripe\Core\Path;
|
use SilverStripe\Control\Controller;
|
||||||
|
use SilverStripe\Core\Config\Config;
|
||||||
|
|
||||||
class DeferredRequirements implements TemplateGlobalProvider
|
class WebpackTemplateProvider implements TemplateGlobalProvider
|
||||||
{
|
{
|
||||||
private static $css = [];
|
/**
|
||||||
private static $js = [];
|
* @var int port number
|
||||||
private static $deferred = false;
|
*/
|
||||||
private static $static_domain;
|
private static $port = 3000;
|
||||||
private static $version;
|
|
||||||
private static $nojquery = false;
|
/**
|
||||||
private static $jquery_version = '3.4.1';
|
* @var string host name
|
||||||
private static $nofontawesome = false;
|
*/
|
||||||
private static $fontawesome_version = '5.11.0';
|
private static $hostname = 'localhost';
|
||||||
private static $custom_requirements = [];
|
|
||||||
|
/**
|
||||||
|
* @var string assets static files directory
|
||||||
|
*/
|
||||||
|
private static $dist = 'client/dist';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
@ -30,172 +38,97 @@ class DeferredRequirements implements TemplateGlobalProvider
|
|||||||
public static function get_template_global_variables(): array
|
public static function get_template_global_variables(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'AutoRequirements' => 'Auto',
|
'WebpackDevServer' => 'isActive',
|
||||||
'DeferedCSS' => 'loadCSS',
|
'WebpackCSS' => 'loadCSS',
|
||||||
'DeferedJS' => 'loadJS',
|
'WebpackJS' => 'loadJS',
|
||||||
'WebpackActive' => '_webpackActive',
|
'ResourcesURL' => 'resourcesURL',
|
||||||
|
'ProjectName' => 'themeName',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function Auto($class = false): string
|
/**
|
||||||
|
* Load CSS file
|
||||||
|
* @param $path
|
||||||
|
*/
|
||||||
|
public static function loadCSS($path): void
|
||||||
{
|
{
|
||||||
$config = Config::inst()->get(self::class);
|
if (self::isActive()) {
|
||||||
$projectName = WebpackTemplateProvider::projectName();
|
return;
|
||||||
$mainTheme = WebpackTemplateProvider::mainTheme();
|
}
|
||||||
$mainTheme = $mainTheme ?: $projectName;
|
|
||||||
|
|
||||||
$dir = Path::join(
|
Requirements::css(self::_getPath($path));
|
||||||
Director::publicFolder(),
|
}
|
||||||
RESOURCES_DIR,
|
|
||||||
$projectName,
|
/**
|
||||||
'client',
|
* Load JS file
|
||||||
'dist'
|
* @param $path
|
||||||
|
*/
|
||||||
|
public static function loadJS($path): void
|
||||||
|
{
|
||||||
|
Requirements::javascript(self::_getPath($path), ['type' => '']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function projectName(): string
|
||||||
|
{
|
||||||
|
return Config::inst()->get(ModuleManifest::class, 'project');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function mainTheme()
|
||||||
|
{
|
||||||
|
$themes = Config::inst()->get(SSViewer::class, 'themes');
|
||||||
|
return is_array($themes) && $themes[0] !== '$public' && $themes[0] !== '$default' ? $themes[0] : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function resourcesURL($link = null): string
|
||||||
|
{
|
||||||
|
return Controller::join_links(Director::baseURL(), '/resources/'.self::projectName().'/client/dist/img/', $link);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if dev mode is enabled and if webpack server is online
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function isActive(): bool
|
||||||
|
{
|
||||||
|
$cfg = self::config();
|
||||||
|
return Director::isDev() && @fsockopen(
|
||||||
|
$cfg['HOSTNAME'],
|
||||||
|
$cfg['PORT']
|
||||||
);
|
);
|
||||||
$cssPath = Path::join($dir, 'css');
|
|
||||||
$jsPath = Path::join($dir, 'js');
|
|
||||||
|
|
||||||
// Initialization
|
|
||||||
Requirements::block(THIRDPARTY_DIR.'/jquery/jquery.js');
|
|
||||||
/*if (defined('FONT_AWESOME_DIR')) {
|
|
||||||
Requirements::block(FONT_AWESOME_DIR.'/css/lib/font-awesome.min.css');
|
|
||||||
}*/
|
|
||||||
Requirements::set_force_js_to_bottom(true);
|
|
||||||
|
|
||||||
// Main libs
|
|
||||||
if (!$config['nojquery']) {
|
|
||||||
self::loadJS(
|
|
||||||
'//ajax.googleapis.com/ajax/libs/jquery/'
|
|
||||||
.$config['jquery_version'].'/jquery.min.js'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
// App libs
|
|
||||||
if (!$config['nofontawesome']) {
|
|
||||||
self::loadCSS(
|
|
||||||
'//use.fontawesome.com/releases/v'
|
|
||||||
.$config['fontawesome_version'].'/css/all.css'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
self::loadCSS($mainTheme.'.css');
|
|
||||||
self::loadJS($mainTheme.'.js');
|
|
||||||
|
|
||||||
// Custom controller requirements
|
|
||||||
$curr_class = $class ?: get_class(Controller::curr());
|
|
||||||
if (isset($config['custom_requirements'][$curr_class])) {
|
|
||||||
foreach ($config['custom_requirements'][$curr_class] as $file) {
|
|
||||||
if (strpos($file, '.css')) {
|
|
||||||
self::loadCSS($file);
|
|
||||||
}
|
|
||||||
if (strpos($file, '.js')) {
|
|
||||||
self::loadJS($file);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$curr_class = str_replace('\\', '.', $curr_class);
|
|
||||||
|
|
||||||
// Controller requirements
|
|
||||||
$themePath = Path::join($cssPath, $mainTheme.'_'.$curr_class . '.css');
|
|
||||||
$projectPath = Path::join($cssPath, $projectName.'_'.$curr_class . '.css');
|
|
||||||
if ($mainTheme && file_exists($themePath)) {
|
|
||||||
self::loadCSS($mainTheme.'_'.$curr_class . '.css');
|
|
||||||
} elseif (file_exists($projectPath)) {
|
|
||||||
self::loadCSS($projectName.'_'.$curr_class . '.css');
|
|
||||||
}
|
|
||||||
|
|
||||||
$themePath = Path::join($jsPath, $mainTheme.'_'.$curr_class . '.js');
|
|
||||||
$projectPath = Path::join($jsPath, $projectName.'_'.$curr_class . '.js');
|
|
||||||
if ($mainTheme && file_exists($themePath)) {
|
|
||||||
self::loadJS($mainTheme.'_'.$curr_class . '.js');
|
|
||||||
} elseif (file_exists($projectPath)) {
|
|
||||||
self::loadJS($projectName.'_'.$curr_class . '.js');
|
|
||||||
}
|
|
||||||
|
|
||||||
return self::forTemplate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function loadCSS($css): void
|
protected static function _getPath($path): string
|
||||||
{
|
{
|
||||||
$external = (mb_strpos($css, '//') === 0 || mb_strpos($css, 'http') === 0);
|
return self::isActive() && strpos($path, '//') === false ?
|
||||||
if ($external || (self::getDeferred() && !self::_webpackActive())) {
|
self::_toDevServerPath($path) :
|
||||||
self::$css[] = $css;
|
self::toPublicPath($path);
|
||||||
} else {
|
|
||||||
WebpackTemplateProvider::loadCSS($css);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function loadJS($js): void
|
protected static function _toDevServerPath($path): string
|
||||||
{
|
{
|
||||||
/*$external = (mb_substr($js, 0, 2) === '//' || mb_substr($js, 0, 4) === 'http');
|
$cfg = self::config();
|
||||||
if ($external || (self::getDeferred() && !self::_webpackActive())) {*/
|
return sprintf(
|
||||||
// webpack supposed to load external JS
|
'%s%s:%s/%s',
|
||||||
if (self::getDeferred() && !self::_webpackActive()) {
|
Director::protocol(),
|
||||||
self::$js[] = $js;
|
$cfg['HOSTNAME'],
|
||||||
} else {
|
$cfg['PORT'],
|
||||||
WebpackTemplateProvider::loadJS($js);
|
basename($path)
|
||||||
}
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static function _webpackActive(): bool
|
public static function toPublicPath($path): string
|
||||||
{
|
{
|
||||||
return WebpackTemplateProvider::isActive();
|
$cfg = self::config();
|
||||||
}
|
return strpos($path, '//') === false ?
|
||||||
|
Controller::join_links(
|
||||||
public static function setDeferred($bool): void
|
RESOURCES_DIR,
|
||||||
{
|
self::projectName(),
|
||||||
Config::inst()->set(__CLASS__, 'deferred', $bool);
|
$cfg['DIST'],
|
||||||
}
|
(strpos($path, '.css') ? 'css' : 'js'),
|
||||||
|
$path
|
||||||
public static function getDeferred(): bool
|
)
|
||||||
{
|
: $path;
|
||||||
return self::config()['deferred'];
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function forTemplate(): string
|
|
||||||
{
|
|
||||||
$result = '';
|
|
||||||
self::$css = array_unique(self::$css);
|
|
||||||
foreach (self::$css as $css) {
|
|
||||||
$result .= '<i class="defer-cs" data-load="' . self::get_url($css) . '"></i>';
|
|
||||||
}
|
|
||||||
|
|
||||||
self::$js = array_unique(self::$js);
|
|
||||||
foreach (self::$js as $js) {
|
|
||||||
$result .= '<i class="defer-sc" data-load="' . self::get_url($js) . '"></i>';
|
|
||||||
}
|
|
||||||
|
|
||||||
$result .=
|
|
||||||
'<script>function lsc(a,b){var c=document.createElement("script");c.readyState'
|
|
||||||
.'?c.onreadystatechange=function(){"loaded"!=c.readyState&&"complete"!=c.readyState||(c.onreadystatechange=null,b())}'
|
|
||||||
.':c.onload=function(){b()},c.src=a,document.getElementsByTagName("body")[0].appendChild(c)}'
|
|
||||||
.'function lscd(a){a<s.length-1&&(a++,lsc(s.item(a).getAttribute("data-load"),function(){lscd(a)}))}'
|
|
||||||
.'for(var s=document.getElementsByClassName("defer-cs"),i=0;i<s.length;i++){var b=document.createElement("link");b.rel="stylesheet",'
|
|
||||||
.'b.type="text/css",b.href=s.item(i).getAttribute("data-load"),b.media="all";var c=document.getElementsByTagName("body")[0];'
|
|
||||||
.'c.appendChild(b)}var s=document.getElementsByClassName("defer-sc"),i=0;if(s.item(i)!==null)lsc(s.item(i).getAttribute("data-load"),function(){lscd(i)});'
|
|
||||||
.'</script>';
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static function get_url($url): string
|
|
||||||
{
|
|
||||||
$config = self::config();
|
|
||||||
|
|
||||||
// external URL
|
|
||||||
if (strpos($url, '//') !== false) {
|
|
||||||
return $url;
|
|
||||||
}
|
|
||||||
|
|
||||||
$path = WebpackTemplateProvider::toPublicPath($url);
|
|
||||||
|
|
||||||
$absolutePath = Director::getAbsFile($path);
|
|
||||||
$hash = sha1_file($absolutePath);
|
|
||||||
|
|
||||||
$version = $config['version'] ? '&v='.$config['version'] : '';
|
|
||||||
//$static_domain = $config['static_domain'];
|
|
||||||
//$static_domain = $static_domain ?: '';
|
|
||||||
|
|
||||||
return Controller::join_links(WebpackTemplateProvider::toPublicPath($url), '?m='.$hash.$version);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function config(): array
|
public static function config(): array
|
||||||
|
Loading…
x
Reference in New Issue
Block a user