Commit 60e07e0d by Qiang Xue

new asset WIP: renabled asset publishing

parent 2f526521
{
"directory" : "assets"
"directory" : "vendor/bower"
}
......@@ -41,10 +41,6 @@ return [
'backend/config/main-local.php',
'frontend/config/main-local.php',
],
'createSymlink' => [
'frontend/web/assets' => 'assets',
'backend/web/assets' => 'assets',
],
],
'Production' => [
'path' => 'prod',
......@@ -61,9 +57,5 @@ return [
'backend/config/main-local.php',
'frontend/config/main-local.php',
],
'createSymlink' => [
'frontend/web/assets' => 'assets',
'backend/web/assets' => 'assets',
],
],
];
{
"directory" : "web/assets"
}
\ No newline at end of file
"directory" : "vendor/bower"
}
......@@ -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)
- `@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
- `@vendor`, the [[yii\base\Application::vendorPath|Composer vendor directory]]
- `@webroot`, the Web root directory of the currently running Web application
- `@web`, the base URL of the currently running Web application
- `@runtime`, the [[yii\base\Application::runtimePath|runtime path]] of the currently running application. Defaults to `@app/runtime`.
- `@webroot`, the Web root directory of the currently running Web application. It is determined based on the directory
containing the entry script.
- `@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
[configuration](concept-configurations.md).
......
......@@ -17,11 +17,12 @@ use yii\web\AssetBundle;
*/
class AuthChoiceAsset extends AssetBundle
{
public $sourcePath = '@vendor/yii2-authclient/assets';
public $js = [
'yii2-authclient/assets/authchoice.js',
'authchoice.js',
];
public $css = [
'yii2-authclient/assets/authchoice.css',
'authchoice.css',
];
public $depends = [
'yii\web\YiiAsset',
......
......@@ -17,7 +17,8 @@ use yii\web\AssetBundle;
*/
class BootstrapAsset extends AssetBundle
{
public $sourcePath = '@bower/bootstrap';
public $css = [
'bootstrap/dist/css/bootstrap.css',
'dist/css/bootstrap.css',
];
}
......@@ -17,8 +17,9 @@ use yii\web\AssetBundle;
*/
class BootstrapPluginAsset extends AssetBundle
{
public $sourcePath = '@bower/bootstrap';
public $js = [
'bootstrap/dist/js/bootstrap.js',
'dist/js/bootstrap.js',
];
public $depends = [
'yii\web\JqueryAsset',
......
......@@ -17,8 +17,9 @@ use yii\web\AssetBundle;
*/
class BootstrapThemeAsset extends AssetBundle
{
public $sourcePath = '@bower/bootstrap';
public $css = [
'bootstrap/dist/css/bootstrap-theme.css',
'dist/css/bootstrap-theme.css',
];
public $depends = [
'yii\bootstrap\BootstrapAsset',
......
......@@ -17,9 +17,10 @@ use yii\web\AssetBundle;
*/
class DebugAsset extends AssetBundle
{
public $sourcePath = '@yii/debug/assets';
public $css = [
'yii2-debug/assets/main.css',
'yii2-debug/assets/toolbar.css',
'main.css',
'toolbar.css',
];
public $depends = [
'yii\web\YiiAsset',
......
......@@ -17,18 +17,17 @@ use yii\web\AssetBundle;
*/
class GiiAsset extends AssetBundle
{
public $sourcePath = '@yii/gii/assets';
public $css = [
'yii2-gii/assets/main.css',
'main.css',
];
public $js = [
'yii2-gii/assets/gii.js',
'typeahead.js/dist/typeahead.bundle.js',
'gii.js',
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
'yii\bootstrap\BootstrapPluginAsset',
'yii\gii\TypeAheadAsset',
];
}
......@@ -5,20 +5,23 @@
* @license http://www.yiiframework.com/license/
*/
namespace yii\jui;
namespace yii\gii;
use yii\web\AssetBundle;
/**
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ButtonAsset extends AssetBundle
class TypeAheadAsset extends AssetBundle
{
public $sourcePath = '@bower/typeahead.js';
public $js = [
'jquery.ui/ui/button.js',
'dist/typeahead.bundle.js',
];
public $depends = [
'yii\jui\CoreAsset',
'yii\bootstrap\BootstrapAsset',
'yii\bootstrap\BootstrapPluginAsset',
];
}
......@@ -22,7 +22,7 @@ $asset = yii\gii\GiiAsset::register($this);
<?php $this->beginBody() ?>
<?php
NavBar::begin([
'brandLabel' => Html::img('@web/assets/yii2-gii/assets/logo.png'),
'brandLabel' => Html::img($asset->baseUrl . '/logo.png'),
'brandUrl' => ['default/index'],
'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
$view = $this->getView();
$bundle = DatePickerLanguageAsset::register($view);
if ($bundle->autoGenerate) {
$am = $view->getAssetManager();
$view->registerJsFile($am->getAssetUrl("jquery-ui/ui/i18n/datepicker-$language.js"), [
$view->registerJsFile($bundle->baseUrl . "/ui/i18n/datepicker-$language.js", [
'depends' => [JuiAsset::className()],
]);
}
......
......@@ -15,6 +15,7 @@ use yii\web\AssetBundle;
*/
class DatePickerLanguageAsset extends AssetBundle
{
public $sourcePath = '@bower/jquery-ui';
/**
* @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]]
......
......@@ -15,11 +15,12 @@ use yii\web\AssetBundle;
*/
class JuiAsset extends AssetBundle
{
public $sourcePath = '@bower/jquery-ui';
public $js = [
'jquery-ui/jquery-ui.js',
'jquery-ui.js',
];
public $css = [
'jquery-ui/themes/smoothness/jquery-ui.css',
'themes/smoothness/jquery-ui.css',
];
public $depends = [
'yii\web\JqueryAsset',
......
......@@ -449,6 +449,8 @@ abstract class Application extends Module
{
$this->_vendorPath = Yii::getAlias($path);
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;
*/
class CaptchaAsset extends AssetBundle
{
public $sourcePath = '@yii/assets';
public $js = [
'yii2/assets/yii.captcha.js',
'yii.captcha.js',
];
public $depends = [
'yii\web\YiiAsset',
......
......@@ -17,8 +17,9 @@ use yii\web\AssetBundle;
*/
class GridViewAsset extends AssetBundle
{
public $sourcePath = '@yii/assets';
public $js = [
'yii2/assets/yii.gridView.js',
'yii.gridView.js',
];
public $depends = [
'yii\web\YiiAsset',
......
......@@ -17,7 +17,8 @@ use yii\web\AssetBundle;
*/
class PunycodeAsset extends AssetBundle
{
public $sourcePath = '@bower/punycode';
public $js = [
'punycode/punycode.js',
'punycode.js',
];
}
......@@ -17,8 +17,9 @@ use yii\web\AssetBundle;
*/
class ValidationAsset extends AssetBundle
{
public $sourcePath = '@yii/assets';
public $js = [
'yii2/assets/yii.validation.js',
'yii.validation.js',
];
public $depends = [
'yii\web\YiiAsset',
......
......@@ -7,9 +7,9 @@
namespace yii\web;
use Yii;
use yii\base\Object;
use yii\helpers\Url;
use Yii;
/**
* AssetBundle represents a collection of asset files, such as CSS, JS, images.
......@@ -27,11 +27,23 @@ use yii\helpers\Url;
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]]
* to form an absolute file path. If this property is null (meaning not set), it will be filled with the value of
* [[AssetManager::basePath]] when the bundle is being loaded by [[AssetManager::getBundle()]].
* If [[sourcePath]] is set, this property will be *overwritten* by [[AssetManager]]
* when it publishes the asset files from [[sourcePath]].
*
* You can use either a directory or an alias of the directory.
*/
......@@ -39,10 +51,8 @@ class AssetBundle extends Object
/**
* @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]]
* when they are being registered in a view so that they can be Web accessible.
* 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()]].
* If [[sourcePath]] is set, this property will be *overwritten* by [[AssetManager]]
* when it publishes the asset files from [[sourcePath]].
*
* You can use either a URL or an alias of the URL.
*/
......@@ -65,11 +75,11 @@ class AssetBundle extends Object
* specified in one of the following formats:
*
* - 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`.
* - a path relative to [[basePath]] and [[baseUrl]]: for example, `js/main.js`. There should be no leading slash.
* - a path relative to [[AssetManager::basePath]] and [[AssetManager::baseUrl]]: for example,
* `@/jquery/dist/jquery.js`. The path must begin with `@/`.
* `http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js` or
* `//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js`.
* - a relative path representing a local asset (e.g. `js/main.js`). The actual file path of a local
* asset can be determined by prefixing [[basePath]] to the relative path, and the actual URL
* of the asset can be determined by prefixing [[baseUrl]] to the relative path.
*
* Note that only forward slash "/" should be used as directory separators.
*/
......@@ -91,6 +101,11 @@ class AssetBundle extends Object
* when registering the CSS files in this bundle.
*/
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
*/
public function init()
{
if ($this->sourcePath !== null) {
$this->sourcePath = rtrim(Yii::getAlias($this->sourcePath), '/\\');
}
if ($this->basePath !== null) {
$this->basePath = rtrim(Yii::getAlias($this->basePath), '/\\');
}
......@@ -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)
{
$manager = $view->getAssetManager();
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) {
$view->registerCssFile($this->getAssetUrl($css, $manager), $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;
$view->registerCssFile($manager->getAssetUrl($this, $css), $this->cssOptions);
}
}
/**
* Returns the actual file path for the specified asset.
* @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|boolean the actual file path, or false if the asset is specified as an absolute URL
* Publishes the asset bundle if its source code is not under Web-accessible directory.
* It will also try to convert non-CSS or JS files (e.g. LESS, Sass) into the corresponding
* CSS or JS files using [[AssetManager::converter|asset converter]].
* @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) {
return Url::isRelative($actualAsset) ? $manager->basePath . '/' . $actualAsset : false;
if ($this->sourcePath !== null && !isset($this->basePath, $this->baseUrl)) {
list ($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions);
}
if (strncmp($asset, '@/', 2) === 0) {
return $manager->basePath . substr($asset, 1);
} elseif (Url::isRelative($asset)) {
return $this->basePath . '/' . $asset;
} else {
return false;
if (isset($this->basePath, $this->baseUrl) && ($converter = $am->getConverter()) !== null) {
foreach ($this->js as $i => $js) {
if (Url::isRelative($js)) {
$this->js[$i] = $converter->convert($js, $this->basePath);
}
}
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;
*/
class JqueryAsset extends AssetBundle
{
public $sourcePath = '@bower/jquery';
public $js = [
'jquery/dist/jquery.js',
'dist/jquery.js',
];
}
......@@ -15,8 +15,9 @@ namespace yii\web;
*/
class YiiAsset extends AssetBundle
{
public $sourcePath = '@yii/assets';
public $js = [
'yii2/assets/yii.js',
'yii.js',
];
public $depends = [
'yii\web\JqueryAsset',
......
......@@ -15,8 +15,9 @@ use yii\web\AssetBundle;
*/
class ActiveFormAsset extends AssetBundle
{
public $sourcePath = '@yii/assets';
public $js = [
'yii2/assets/yii.activeForm.js',
'yii.activeForm.js',
];
public $depends = [
'yii\web\YiiAsset',
......
......@@ -19,8 +19,9 @@ use yii\web\AssetBundle;
*/
class MaskedInputAsset extends AssetBundle
{
public $sourcePath = '@bower/jquery.inputmask';
public $js = [
'jquery.inputmask/dist/jquery.inputmask.bundle.js'
'dist/jquery.inputmask.bundle.js'
];
public $depends = [
'yii\web\YiiAsset'
......
......@@ -17,8 +17,9 @@ use yii\web\AssetBundle;
*/
class PjaxAsset extends AssetBundle
{
public $sourcePath = '@bower/yii2-pjax';
public $js = [
'yii2-pjax/jquery.pjax.js',
'jquery.pjax.js',
];
public $depends = [
'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