如何将整个目录添加到资产包 yii2 中的 js 部分
How to add whole directory to js section in asset bundle yii2
如何将整个目录添加到资产包 yii2 中的 js 部分
我想这会是这样的
class AppAsset extends AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
'css/site.min.css',
];
public $js = [
'js/myModule/*',
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
}
我想要这个是因为我不想每次(当我缩小我的 css 或缩小我的 js 时)在资产包中写这样的东西
'css/site.min.css',
我不想写 'module.min.js', 'module2.min.js'
,而是想写一些类似正则表达式(添加整个目录)的东西,它将从目录中收集所有文件并将它们添加到我的资产包和我的 $js
数组中接下来看起来像
'js/myModule/*',
那么如何实现呢?有什么 'ready from the box' 解决方案吗?
请帮忙。
我问这个是因为 'is it normal that i (i use gulp and different minificators etc) write in asset bundle module.min.css
or module.min.js
'
依靠 Yii2 docs' assets page 无法立即实现此功能。
但是您可以覆盖 AppAsset
class 的 init()
方法来实现解析文件夹 (vie FileHelper) 并将其添加到 $this->js
数组动态地。
另一种方法(我个人使用)是解析 gulp 文件使用的 assets.json
文件,并添加每个 js/css 文件由 gulp 生成。这是代码示例
<?php
namespace app\assets;
use yii\base\Exception;
use yii\web\AssetBundle;
class AppAsset extends AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [];
public $js = [];
public $depends = [
'yii\web\YiiAsset',
'yii\web\JqueryAsset',
'yii\widgets\ActiveFormAsset',
'yii\validators\ValidationAsset'
];
public $packages = [];
public function init()
{
parent::init();
$content = @file_get_contents(\Yii::getAlias('@app/assets.json'));
if (!$content) {
throw new Exception('Could not read assests from path ' . \Yii::getAlias('@app/assets.json'));
}
$assetsData = json_decode($content, true);
if (!empty($assetsData['scripts'])) {
foreach ($assetsData['scripts'] as $script) {
if (in_array($script['name'], $this->packages, true) && $this->loadInAjax($script)) {
$this->js[] = 'scripts/' . $script['name'] . '.js';
}
}
}
if (!empty($assetsData['styles'])) {
foreach ($assetsData['styles'] as $style) {
if (in_array($style['name'], $this->packages, true) && $this->loadInAjax($style)) {
$this->css[] = 'styles/' . $style['name'] . '.css';
}
}
}
}
}
希望对您有所帮助。
根据@naffiq 的建议,我想出了这个代码:
<?php
namespace frontend\assets;
use yii\web\AssetBundle;
use yii\helpers\FileHelper;
abstract class StarAssetBundle extends AssetBundle
{
private $filePathOptions = [
'only' => ['*.js'],
'recursive' => false,
];
public function init()
{
$realSourcePath = \Yii::getAlias($this->sourcePath);
$fileHelper = new FileHelper();
$jsFiles = $this->js;
$parsedJsFiles = array();
foreach($jsFiles as $uri){
$path = $this->strReturnIfContainsStarAtEnd($uri);
if($path){
$dir = $realSourcePath.'/'.$path;
$arrFiles = $fileHelper->findFiles($dir, $this->filePathOptions);
foreach ($arrFiles as $file) {
$jsFilePath = $path . '/' . basename($file);
array_push($parsedJsFiles, $jsFilePath);
}
}
else
{
array_push($parsedJsFiles, $uri);
}
}
$this->js = $parsedJsFiles;
parent::init();
}
private function strReturnIfContainsStarAtEnd($str){
$strLen = strlen($str);
$pos = strpos($str, "*", $strLen-1);
if($pos !== false){
return substr($str, 0, $strLen-2);
}
else{
return false;
}
}
}
现在我可以添加所有 .js 文件,仅编码路径并在末尾添加 * - 'js/myModule/*'
如何将整个目录添加到资产包 yii2 中的 js 部分
我想这会是这样的
class AppAsset extends AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
'css/site.min.css',
];
public $js = [
'js/myModule/*',
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
}
我想要这个是因为我不想每次(当我缩小我的 css 或缩小我的 js 时)在资产包中写这样的东西
'css/site.min.css',
我不想写 'module.min.js', 'module2.min.js'
,而是想写一些类似正则表达式(添加整个目录)的东西,它将从目录中收集所有文件并将它们添加到我的资产包和我的 $js
数组中接下来看起来像
'js/myModule/*',
那么如何实现呢?有什么 'ready from the box' 解决方案吗?
请帮忙。
我问这个是因为 'is it normal that i (i use gulp and different minificators etc) write in asset bundle module.min.css
or module.min.js
'
依靠 Yii2 docs' assets page 无法立即实现此功能。
但是您可以覆盖 AppAsset
class 的 init()
方法来实现解析文件夹 (vie FileHelper) 并将其添加到 $this->js
数组动态地。
另一种方法(我个人使用)是解析 gulp 文件使用的 assets.json
文件,并添加每个 js/css 文件由 gulp 生成。这是代码示例
<?php
namespace app\assets;
use yii\base\Exception;
use yii\web\AssetBundle;
class AppAsset extends AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [];
public $js = [];
public $depends = [
'yii\web\YiiAsset',
'yii\web\JqueryAsset',
'yii\widgets\ActiveFormAsset',
'yii\validators\ValidationAsset'
];
public $packages = [];
public function init()
{
parent::init();
$content = @file_get_contents(\Yii::getAlias('@app/assets.json'));
if (!$content) {
throw new Exception('Could not read assests from path ' . \Yii::getAlias('@app/assets.json'));
}
$assetsData = json_decode($content, true);
if (!empty($assetsData['scripts'])) {
foreach ($assetsData['scripts'] as $script) {
if (in_array($script['name'], $this->packages, true) && $this->loadInAjax($script)) {
$this->js[] = 'scripts/' . $script['name'] . '.js';
}
}
}
if (!empty($assetsData['styles'])) {
foreach ($assetsData['styles'] as $style) {
if (in_array($style['name'], $this->packages, true) && $this->loadInAjax($style)) {
$this->css[] = 'styles/' . $style['name'] . '.css';
}
}
}
}
}
希望对您有所帮助。
根据@naffiq 的建议,我想出了这个代码:
<?php
namespace frontend\assets;
use yii\web\AssetBundle;
use yii\helpers\FileHelper;
abstract class StarAssetBundle extends AssetBundle
{
private $filePathOptions = [
'only' => ['*.js'],
'recursive' => false,
];
public function init()
{
$realSourcePath = \Yii::getAlias($this->sourcePath);
$fileHelper = new FileHelper();
$jsFiles = $this->js;
$parsedJsFiles = array();
foreach($jsFiles as $uri){
$path = $this->strReturnIfContainsStarAtEnd($uri);
if($path){
$dir = $realSourcePath.'/'.$path;
$arrFiles = $fileHelper->findFiles($dir, $this->filePathOptions);
foreach ($arrFiles as $file) {
$jsFilePath = $path . '/' . basename($file);
array_push($parsedJsFiles, $jsFilePath);
}
}
else
{
array_push($parsedJsFiles, $uri);
}
}
$this->js = $parsedJsFiles;
parent::init();
}
private function strReturnIfContainsStarAtEnd($str){
$strLen = strlen($str);
$pos = strpos($str, "*", $strLen-1);
if($pos !== false){
return substr($str, 0, $strLen-2);
}
else{
return false;
}
}
}
现在我可以添加所有 .js 文件,仅编码路径并在末尾添加 * - 'js/myModule/*'