Commit 60e07e0d by Qiang Xue

new asset WIP: renabled asset publishing

parent 2f526521
{ {
"directory" : "assets" "directory" : "vendor/bower"
} }
...@@ -41,10 +41,6 @@ return [ ...@@ -41,10 +41,6 @@ return [
'backend/config/main-local.php', 'backend/config/main-local.php',
'frontend/config/main-local.php', 'frontend/config/main-local.php',
], ],
'createSymlink' => [
'frontend/web/assets' => 'assets',
'backend/web/assets' => 'assets',
],
], ],
'Production' => [ 'Production' => [
'path' => 'prod', 'path' => 'prod',
...@@ -61,9 +57,5 @@ return [ ...@@ -61,9 +57,5 @@ return [
'backend/config/main-local.php', 'backend/config/main-local.php',
'frontend/config/main-local.php', 'frontend/config/main-local.php',
], ],
'createSymlink' => [
'frontend/web/assets' => 'assets',
'backend/web/assets' => 'assets',
],
], ],
]; ];
{ {
"directory" : "web/assets" "directory" : "vendor/bower"
} }
\ No newline at end of file
...@@ -106,10 +106,13 @@ Yii predefines a set of aliases to easily reference commonly used file paths and ...@@ -106,10 +106,13 @@ Yii predefines a set of aliases to easily reference commonly used file paths and
- `@yii`, the directory where the `BaseYii.php` file is located (also called the framework directory) - `@yii`, the directory where the `BaseYii.php` file is located (also called the framework directory)
- `@app`, the [[yii\base\Application::basePath|base path]] of the currently running application - `@app`, the [[yii\base\Application::basePath|base path]] of the currently running application
- `@runtime`, the [[yii\base\Application::runtimePath|runtime path]] of the currently running application - `@runtime`, the [[yii\base\Application::runtimePath|runtime path]] of the currently running application. Defaults to `@app/runtime`.
- `@vendor`, the [[yii\base\Application::vendorPath|Composer vendor directory]] - `@webroot`, the Web root directory of the currently running Web application. It is determined based on the directory
- `@webroot`, the Web root directory of the currently running Web application containing the entry script.
- `@web`, the base URL of the currently running Web application - `@web`, the base URL of the currently running Web application. It has the same value as [[yii\web\Request::baseUrl]].
- `@vendor`, the [[yii\base\Application::vendorPath|Composer vendor directory]]. Defaults to `@app/vendor`.
- `@bower`, the root directory that contains [bower packages](http://bower.io/). Defaults to `@vendor/bower`.
- `@npm`, the root directory that contains [npm packages](https://www.npmjs.org/). Defaults to `@vendor/npm`.
The `@yii` alias is defined when you include the `Yii.php` file in your [entry script](structure-entry-scripts.md). The rest of the aliases are defined in the application constructor when applying the application The `@yii` alias is defined when you include the `Yii.php` file in your [entry script](structure-entry-scripts.md). The rest of the aliases are defined in the application constructor when applying the application
[configuration](concept-configurations.md). [configuration](concept-configurations.md).
......
...@@ -17,11 +17,12 @@ use yii\web\AssetBundle; ...@@ -17,11 +17,12 @@ use yii\web\AssetBundle;
*/ */
class AuthChoiceAsset extends AssetBundle class AuthChoiceAsset extends AssetBundle
{ {
public $sourcePath = '@vendor/yii2-authclient/assets';
public $js = [ public $js = [
'yii2-authclient/assets/authchoice.js', 'authchoice.js',
]; ];
public $css = [ public $css = [
'yii2-authclient/assets/authchoice.css', 'authchoice.css',
]; ];
public $depends = [ public $depends = [
'yii\web\YiiAsset', 'yii\web\YiiAsset',
......
...@@ -17,7 +17,8 @@ use yii\web\AssetBundle; ...@@ -17,7 +17,8 @@ use yii\web\AssetBundle;
*/ */
class BootstrapAsset extends AssetBundle class BootstrapAsset extends AssetBundle
{ {
public $sourcePath = '@bower/bootstrap';
public $css = [ public $css = [
'bootstrap/dist/css/bootstrap.css', 'dist/css/bootstrap.css',
]; ];
} }
...@@ -17,8 +17,9 @@ use yii\web\AssetBundle; ...@@ -17,8 +17,9 @@ use yii\web\AssetBundle;
*/ */
class BootstrapPluginAsset extends AssetBundle class BootstrapPluginAsset extends AssetBundle
{ {
public $sourcePath = '@bower/bootstrap';
public $js = [ public $js = [
'bootstrap/dist/js/bootstrap.js', 'dist/js/bootstrap.js',
]; ];
public $depends = [ public $depends = [
'yii\web\JqueryAsset', 'yii\web\JqueryAsset',
......
...@@ -17,8 +17,9 @@ use yii\web\AssetBundle; ...@@ -17,8 +17,9 @@ use yii\web\AssetBundle;
*/ */
class BootstrapThemeAsset extends AssetBundle class BootstrapThemeAsset extends AssetBundle
{ {
public $sourcePath = '@bower/bootstrap';
public $css = [ public $css = [
'bootstrap/dist/css/bootstrap-theme.css', 'dist/css/bootstrap-theme.css',
]; ];
public $depends = [ public $depends = [
'yii\bootstrap\BootstrapAsset', 'yii\bootstrap\BootstrapAsset',
......
...@@ -17,9 +17,10 @@ use yii\web\AssetBundle; ...@@ -17,9 +17,10 @@ use yii\web\AssetBundle;
*/ */
class DebugAsset extends AssetBundle class DebugAsset extends AssetBundle
{ {
public $sourcePath = '@yii/debug/assets';
public $css = [ public $css = [
'yii2-debug/assets/main.css', 'main.css',
'yii2-debug/assets/toolbar.css', 'toolbar.css',
]; ];
public $depends = [ public $depends = [
'yii\web\YiiAsset', 'yii\web\YiiAsset',
......
...@@ -17,18 +17,17 @@ use yii\web\AssetBundle; ...@@ -17,18 +17,17 @@ use yii\web\AssetBundle;
*/ */
class GiiAsset extends AssetBundle class GiiAsset extends AssetBundle
{ {
public $sourcePath = '@yii/gii/assets';
public $css = [ public $css = [
'yii2-gii/assets/main.css', 'main.css',
]; ];
public $js = [ public $js = [
'yii2-gii/assets/gii.js', 'gii.js',
'typeahead.js/dist/typeahead.bundle.js',
]; ];
public $depends = [ public $depends = [
'yii\web\YiiAsset', 'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset', 'yii\bootstrap\BootstrapAsset',
'yii\bootstrap\BootstrapPluginAsset', 'yii\bootstrap\BootstrapPluginAsset',
'yii\gii\TypeAheadAsset',
]; ];
} }
...@@ -5,20 +5,23 @@ ...@@ -5,20 +5,23 @@
* @license http://www.yiiframework.com/license/ * @license http://www.yiiframework.com/license/
*/ */
namespace yii\jui; namespace yii\gii;
use yii\web\AssetBundle; use yii\web\AssetBundle;
/** /**
*
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
*/ */
class ButtonAsset extends AssetBundle class TypeAheadAsset extends AssetBundle
{ {
public $sourcePath = '@bower/typeahead.js';
public $js = [ public $js = [
'jquery.ui/ui/button.js', 'dist/typeahead.bundle.js',
]; ];
public $depends = [ public $depends = [
'yii\jui\CoreAsset', 'yii\bootstrap\BootstrapAsset',
'yii\bootstrap\BootstrapPluginAsset',
]; ];
} }
...@@ -22,7 +22,7 @@ $asset = yii\gii\GiiAsset::register($this); ...@@ -22,7 +22,7 @@ $asset = yii\gii\GiiAsset::register($this);
<?php $this->beginBody() ?> <?php $this->beginBody() ?>
<?php <?php
NavBar::begin([ NavBar::begin([
'brandLabel' => Html::img('@web/assets/yii2-gii/assets/logo.png'), 'brandLabel' => Html::img($asset->baseUrl . '/logo.png'),
'brandUrl' => ['default/index'], 'brandUrl' => ['default/index'],
'options' => ['class' => 'navbar-inverse navbar-fixed-top'], 'options' => ['class' => 'navbar-inverse navbar-fixed-top'],
]); ]);
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\jui;
use yii\web\AssetBundle;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class CoreAsset extends AssetBundle
{
public $js = [
'jquery.ui/ui/core.js',
'jquery.ui/ui/widget.js',
'jquery.ui/ui/position.js',
'jquery.ui/ui/mouse.js',
];
public $depends = [
'yii\web\JqueryAsset',
];
}
...@@ -86,8 +86,7 @@ class DatePicker extends InputWidget ...@@ -86,8 +86,7 @@ class DatePicker extends InputWidget
$view = $this->getView(); $view = $this->getView();
$bundle = DatePickerLanguageAsset::register($view); $bundle = DatePickerLanguageAsset::register($view);
if ($bundle->autoGenerate) { if ($bundle->autoGenerate) {
$am = $view->getAssetManager(); $view->registerJsFile($bundle->baseUrl . "/ui/i18n/datepicker-$language.js", [
$view->registerJsFile($am->getAssetUrl("jquery-ui/ui/i18n/datepicker-$language.js"), [
'depends' => [JuiAsset::className()], 'depends' => [JuiAsset::className()],
]); ]);
} }
......
...@@ -15,6 +15,7 @@ use yii\web\AssetBundle; ...@@ -15,6 +15,7 @@ use yii\web\AssetBundle;
*/ */
class DatePickerLanguageAsset extends AssetBundle class DatePickerLanguageAsset extends AssetBundle
{ {
public $sourcePath = '@bower/jquery-ui';
/** /**
* @var boolean whether to automatically generate the needed language js files. * @var boolean whether to automatically generate the needed language js files.
* If this is true, the language js files will be determined based on the actual usage of [[DatePicker]] * If this is true, the language js files will be determined based on the actual usage of [[DatePicker]]
......
...@@ -15,11 +15,12 @@ use yii\web\AssetBundle; ...@@ -15,11 +15,12 @@ use yii\web\AssetBundle;
*/ */
class JuiAsset extends AssetBundle class JuiAsset extends AssetBundle
{ {
public $sourcePath = '@bower/jquery-ui';
public $js = [ public $js = [
'jquery-ui/jquery-ui.js', 'jquery-ui.js',
]; ];
public $css = [ public $css = [
'jquery-ui/themes/smoothness/jquery-ui.css', 'themes/smoothness/jquery-ui.css',
]; ];
public $depends = [ public $depends = [
'yii\web\JqueryAsset', 'yii\web\JqueryAsset',
......
...@@ -449,6 +449,8 @@ abstract class Application extends Module ...@@ -449,6 +449,8 @@ abstract class Application extends Module
{ {
$this->_vendorPath = Yii::getAlias($path); $this->_vendorPath = Yii::getAlias($path);
Yii::setAlias('@vendor', $this->_vendorPath); Yii::setAlias('@vendor', $this->_vendorPath);
Yii::setAlias('@bower', $this->_vendorPath . DIRECTORY_SEPARATOR . 'bower');
Yii::setAlias('@npm', $this->_vendorPath . DIRECTORY_SEPARATOR . 'npm');
} }
/** /**
......
...@@ -17,8 +17,9 @@ use yii\web\AssetBundle; ...@@ -17,8 +17,9 @@ use yii\web\AssetBundle;
*/ */
class CaptchaAsset extends AssetBundle class CaptchaAsset extends AssetBundle
{ {
public $sourcePath = '@yii/assets';
public $js = [ public $js = [
'yii2/assets/yii.captcha.js', 'yii.captcha.js',
]; ];
public $depends = [ public $depends = [
'yii\web\YiiAsset', 'yii\web\YiiAsset',
......
...@@ -17,8 +17,9 @@ use yii\web\AssetBundle; ...@@ -17,8 +17,9 @@ use yii\web\AssetBundle;
*/ */
class GridViewAsset extends AssetBundle class GridViewAsset extends AssetBundle
{ {
public $sourcePath = '@yii/assets';
public $js = [ public $js = [
'yii2/assets/yii.gridView.js', 'yii.gridView.js',
]; ];
public $depends = [ public $depends = [
'yii\web\YiiAsset', 'yii\web\YiiAsset',
......
...@@ -17,7 +17,8 @@ use yii\web\AssetBundle; ...@@ -17,7 +17,8 @@ use yii\web\AssetBundle;
*/ */
class PunycodeAsset extends AssetBundle class PunycodeAsset extends AssetBundle
{ {
public $sourcePath = '@bower/punycode';
public $js = [ public $js = [
'punycode/punycode.js', 'punycode.js',
]; ];
} }
...@@ -17,8 +17,9 @@ use yii\web\AssetBundle; ...@@ -17,8 +17,9 @@ use yii\web\AssetBundle;
*/ */
class ValidationAsset extends AssetBundle class ValidationAsset extends AssetBundle
{ {
public $sourcePath = '@yii/assets';
public $js = [ public $js = [
'yii2/assets/yii.validation.js', 'yii.validation.js',
]; ];
public $depends = [ public $depends = [
'yii\web\YiiAsset', 'yii\web\YiiAsset',
......
...@@ -7,9 +7,9 @@ ...@@ -7,9 +7,9 @@
namespace yii\web; namespace yii\web;
use Yii;
use yii\base\Object; use yii\base\Object;
use yii\helpers\Url; use yii\helpers\Url;
use Yii;
/** /**
* AssetBundle represents a collection of asset files, such as CSS, JS, images. * AssetBundle represents a collection of asset files, such as CSS, JS, images.
...@@ -27,11 +27,23 @@ use yii\helpers\Url; ...@@ -27,11 +27,23 @@ use yii\helpers\Url;
class AssetBundle extends Object class AssetBundle extends Object
{ {
/** /**
* @var string the directory that contains the asset files in this bundle. * @var string the directory that contains the source asset files for this asset bundle.
* A source asset file is a file that is part of your source code repository of your Web application.
*
* You must set this property if the directory containing the source asset files is not Web accessible.
* By setting this property, [[AssetManager]] will publish the source asset files
* to a Web-accessible directory automatically when the asset bundle is registered on a page.
*
* If you do not set this property, it means the source asset files are located under [[basePath]].
*
* You can use either a directory or an alias of the directory.
*/
public $sourcePath;
/**
* @var string the Web-accessible directory that contains the asset files in this bundle.
* *
* The value of this property can be prefixed to every relative asset file path listed in [[js]] and [[css]] * If [[sourcePath]] is set, this property will be *overwritten* by [[AssetManager]]
* to form an absolute file path. If this property is null (meaning not set), it will be filled with the value of * when it publishes the asset files from [[sourcePath]].
* [[AssetManager::basePath]] when the bundle is being loaded by [[AssetManager::getBundle()]].
* *
* You can use either a directory or an alias of the directory. * You can use either a directory or an alias of the directory.
*/ */
...@@ -39,10 +51,8 @@ class AssetBundle extends Object ...@@ -39,10 +51,8 @@ class AssetBundle extends Object
/** /**
* @var string the base URL for the relative asset files listed in [[js]] and [[css]]. * @var string the base URL for the relative asset files listed in [[js]] and [[css]].
* *
* The value of this property will be prefixed to every relative asset file path listed in [[js]] and [[css]] * If [[sourcePath]] is set, this property will be *overwritten* by [[AssetManager]]
* when they are being registered in a view so that they can be Web accessible. * when it publishes the asset files from [[sourcePath]].
* If this property is null (meaning not set), it will be filled with the value of
* [[AssetManager::baseUrl]] when the bundle is being loaded by [[AssetManager::getBundle()]].
* *
* You can use either a URL or an alias of the URL. * You can use either a URL or an alias of the URL.
*/ */
...@@ -65,11 +75,11 @@ class AssetBundle extends Object ...@@ -65,11 +75,11 @@ class AssetBundle extends Object
* specified in one of the following formats: * specified in one of the following formats:
* *
* - an absolute URL representing an external asset. For example, * - an absolute URL representing an external asset. For example,
* `//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js` or * `http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js` or
* `http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js`. * `//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js`.
* - a path relative to [[basePath]] and [[baseUrl]]: for example, `js/main.js`. There should be no leading slash. * - a relative path representing a local asset (e.g. `js/main.js`). The actual file path of a local
* - a path relative to [[AssetManager::basePath]] and [[AssetManager::baseUrl]]: for example, * asset can be determined by prefixing [[basePath]] to the relative path, and the actual URL
* `@/jquery/dist/jquery.js`. The path must begin with `@/`. * of the asset can be determined by prefixing [[baseUrl]] to the relative path.
* *
* Note that only forward slash "/" should be used as directory separators. * Note that only forward slash "/" should be used as directory separators.
*/ */
...@@ -91,6 +101,11 @@ class AssetBundle extends Object ...@@ -91,6 +101,11 @@ class AssetBundle extends Object
* when registering the CSS files in this bundle. * when registering the CSS files in this bundle.
*/ */
public $cssOptions = []; public $cssOptions = [];
/**
* @var array the options to be passed to [[AssetManager::publish()]] when the asset bundle
* is being published. This property is used only when [[sourcePath]] is set.
*/
public $publishOptions = [];
/** /**
...@@ -109,6 +124,9 @@ class AssetBundle extends Object ...@@ -109,6 +124,9 @@ class AssetBundle extends Object
*/ */
public function init() public function init()
{ {
if ($this->sourcePath !== null) {
$this->sourcePath = rtrim(Yii::getAlias($this->sourcePath), '/\\');
}
if ($this->basePath !== null) { if ($this->basePath !== null) {
$this->basePath = rtrim(Yii::getAlias($this->basePath), '/\\'); $this->basePath = rtrim(Yii::getAlias($this->basePath), '/\\');
} }
...@@ -118,59 +136,43 @@ class AssetBundle extends Object ...@@ -118,59 +136,43 @@ class AssetBundle extends Object
} }
/** /**
* @param View $view * Registers the CSS and JS files with the given view.
* @param \yii\web\View $view the view that the asset files are to be registered with.
*/ */
public function registerAssetFiles($view) public function registerAssetFiles($view)
{ {
$manager = $view->getAssetManager(); $manager = $view->getAssetManager();
foreach ($this->js as $js) { foreach ($this->js as $js) {
$view->registerJsFile($this->getAssetUrl($js, $manager), $this->jsOptions); $view->registerJsFile($manager->getAssetUrl($this, $js), $this->jsOptions);
} }
foreach ($this->css as $css) { foreach ($this->css as $css) {
$view->registerCssFile($this->getAssetUrl($css, $manager), $this->cssOptions); $view->registerCssFile($manager->getAssetUrl($this, $css), $this->cssOptions);
}
}
/**
* Returns the actual URL for the specified asset.
* The actual URL is obtained by prepending either [[baseUrl]] or [[AssetManager::baseUrl]] to the given asset path.
* @param string $asset the asset path. This should be one of the assets listed in [[js]] or [[css]].
* @param AssetManager $manager the asset manager
* @return string the actual URL for the specified asset.
*/
protected function getAssetUrl($asset, $manager)
{
if (($actualAsset = $manager->resolveAsset($asset)) !== false) {
return Url::isRelative($actualAsset) ? $manager->baseUrl . '/' . $actualAsset : $actualAsset;
}
if (strncmp($asset, '@/', 2) === 0) {
return $manager->baseUrl . substr($asset, 1);
} elseif (Url::isRelative($asset)) {
return $this->baseUrl . '/' . $asset;
} else {
return $asset;
} }
} }
/** /**
* Returns the actual file path for the specified asset. * Publishes the asset bundle if its source code is not under Web-accessible directory.
* @param string $asset the asset path. This should be one of the assets listed in [[js]] or [[css]]. * It will also try to convert non-CSS or JS files (e.g. LESS, Sass) into the corresponding
* @param AssetManager $manager the asset manager * CSS or JS files using [[AssetManager::converter|asset converter]].
* @return string|boolean the actual file path, or false if the asset is specified as an absolute URL * @param AssetManager $am the asset manager to perform the asset publishing
*/ */
public function getAssetPath($asset, $manager) public function publish($am)
{ {
if (($actualAsset = $manager->resolveAsset($asset)) !== false) { if ($this->sourcePath !== null && !isset($this->basePath, $this->baseUrl)) {
return Url::isRelative($actualAsset) ? $manager->basePath . '/' . $actualAsset : false; list ($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions);
} }
if (strncmp($asset, '@/', 2) === 0) { if (isset($this->basePath, $this->baseUrl) && ($converter = $am->getConverter()) !== null) {
return $manager->basePath . substr($asset, 1); foreach ($this->js as $i => $js) {
} elseif (Url::isRelative($asset)) { if (Url::isRelative($js)) {
return $this->basePath . '/' . $asset; $this->js[$i] = $converter->convert($js, $this->basePath);
} else { }
return false; }
foreach ($this->css as $i => $css) {
if (Url::isRelative($css)) {
$this->css[$i] = $converter->convert($css, $this->basePath);
}
}
} }
} }
} }
...@@ -15,7 +15,8 @@ namespace yii\web; ...@@ -15,7 +15,8 @@ namespace yii\web;
*/ */
class JqueryAsset extends AssetBundle class JqueryAsset extends AssetBundle
{ {
public $sourcePath = '@bower/jquery';
public $js = [ public $js = [
'jquery/dist/jquery.js', 'dist/jquery.js',
]; ];
} }
...@@ -15,8 +15,9 @@ namespace yii\web; ...@@ -15,8 +15,9 @@ namespace yii\web;
*/ */
class YiiAsset extends AssetBundle class YiiAsset extends AssetBundle
{ {
public $sourcePath = '@yii/assets';
public $js = [ public $js = [
'yii2/assets/yii.js', 'yii.js',
]; ];
public $depends = [ public $depends = [
'yii\web\JqueryAsset', 'yii\web\JqueryAsset',
......
...@@ -15,8 +15,9 @@ use yii\web\AssetBundle; ...@@ -15,8 +15,9 @@ use yii\web\AssetBundle;
*/ */
class ActiveFormAsset extends AssetBundle class ActiveFormAsset extends AssetBundle
{ {
public $sourcePath = '@yii/assets';
public $js = [ public $js = [
'yii2/assets/yii.activeForm.js', 'yii.activeForm.js',
]; ];
public $depends = [ public $depends = [
'yii\web\YiiAsset', 'yii\web\YiiAsset',
......
...@@ -19,8 +19,9 @@ use yii\web\AssetBundle; ...@@ -19,8 +19,9 @@ use yii\web\AssetBundle;
*/ */
class MaskedInputAsset extends AssetBundle class MaskedInputAsset extends AssetBundle
{ {
public $sourcePath = '@bower/jquery.inputmask';
public $js = [ public $js = [
'jquery.inputmask/dist/jquery.inputmask.bundle.js' 'dist/jquery.inputmask.bundle.js'
]; ];
public $depends = [ public $depends = [
'yii\web\YiiAsset' 'yii\web\YiiAsset'
......
...@@ -17,8 +17,9 @@ use yii\web\AssetBundle; ...@@ -17,8 +17,9 @@ use yii\web\AssetBundle;
*/ */
class PjaxAsset extends AssetBundle class PjaxAsset extends AssetBundle
{ {
public $sourcePath = '@bower/yii2-pjax';
public $js = [ public $js = [
'yii2-pjax/jquery.pjax.js', 'jquery.pjax.js',
]; ];
public $depends = [ public $depends = [
'yii\web\YiiAsset', 'yii\web\YiiAsset',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment