dpgkg42484 2015-04-01 16:47
浏览 58
已采纳

如何在Heroku上为Yii应用程序组合和缩小JS和CSS?

I tried to follow the instructions in the guide, but I got an error.

http://www.yiiframework.com/doc-2.0/guide-structure-assets.html#combining-and-compressing-assets

$ yii asset assets.php config/assets-prod.php
Loading configuration from 'assets.php'...
Collecting source bundles information...
Creating output bundle 'all':
  Compressing JavaScript files...
PHP Warning 'yii\base\ErrorException' with message 'file_put_contents(/cygdrive/c/Users/Chloe/workspace/xxxxxx/web/assets/js/all-temp.js.tmp): failed to open stream: No such file or directory'

in /cygdrive/c/Users/Chloe/workspace/xxxxxx/vendor/yiisoft/yii2/console/controllers/AssetController.php:545

Stack trace:
#0 [internal function]: yii\base\ErrorHandler->handleError(2, 'file_put_conten...', '/cygdrive/c/Use...', 545, Array)
#1 /cygdrive/c/Users/Chloe/workspace/xxxxx/vendor/yiisoft/yii2/console/controllers/AssetController.php(545): file_put_contents('/cygdrive/c/Use...', '/*** BEGIN FILE...')
#2 /cygdrive/c/Users/Chloe/workspace/xxxxx/vendor/yiisoft/yii2/console/controllers/AssetController.php(487): yii\console\controllers\AssetController->combineJsFiles(Array, '/cygdrive/c/Use...')
#3 /cygdrive/c/Users/Chloe/workspace/xxxxx/vendor/yiisoft/yii2/console/controllers/AssetController.php(343): yii\console\controllers\AssetController->compressJsFiles(Array, '/cygdrive/c/Use...')
#4 /cygdrive/c/Users/Chloe/workspace/xxxxx/vendor/yiisoft/yii2/console/controllers/AssetController.php(182): yii\console\controllers\AssetController->buildTarget(Object(yii\web\AssetBundle), 'js', Array)
#5 [internal function]: yii\console\controllers\AssetController->actionCompress('assets.php', 'config/assets-p...')
#6 /cygdrive/c/Users/Chloe/workspace/xxxxx/vendor/yiisoft/yii2/base/InlineAction.php(55): call_user_func_array(Array, Array)
#7 /cygdrive/c/Users/Chloe/workspace/xxxxx/vendor/yiisoft/yii2/base/Controller.php(151): yii\base\InlineAction->runWithParams(Array)
#8 /cygdrive/c/Users/Chloe/workspace/xxxxx/vendor/yiisoft/yii2/console/Controller.php(91): yii\base\Controller->runAction('', Array)
#9 /cygdrive/c/Users/Chloe/workspace/xxxxx/vendor/yiisoft/yii2/base/Module.php(455): yii\console\Controller->runAction('', Array)
#10 /cygdrive/c/Users/Chloe/workspace/xxxxx/vendor/yiisoft/yii2/console/Application.php(161): yii\base\Module->runAction('asset', Array)
#11 /cygdrive/c/Users/Chloe/workspace/xxxxx/vendor/yiisoft/yii2/console/Application.php(137): yii\console\Application->runAction('asset', Array)
#12 /cygdrive/c/Users/Chloe/workspace/xxxxx/vendor/yiisoft/yii2/base/Application.php(375): yii\console\Application->handleRequest(Object(yii\console\Request))
#13 /cygdrive/c/Users/Chloe/workspace/xxxxx/yii(23): yii\base\Application->run()
#14 {main}

This was the closest I've gotten:

Loading configuration from 'assets.php'...
Collecting source bundles information...
Creating output bundle 'all':
  Compressing JavaScript files...
ERROR - Cannot read: \cygdrive\c\Users\Chloe\workspace\xxxxx\web\assets\all-temp.js.tmp

1 error(s), 0 warning(s)
Error: Unable to compress JavaScript files into '/cygdrive/c/Users/Chloe/workspace/xxxxx/web/assets/all-temp.js'.

This is my assets.php file

assets.php
Yii::setAlias('@webroot', __DIR__ . '/web');
Yii::setAlias('@web', '/');

return [
    // Adjust command/callback for JavaScript files compressing:
    'jsCompressor' => 'java -jar compiler.jar --js {from} --js_output_file {to}',
    // Adjust command/callback for CSS files compressing:
    'cssCompressor' => 'java -jar yuicompressor.jar --type css {from} -o {to}',
    // The list of asset bundles to compress:
    'bundles' => [
        'app\assets\AppAsset',
        'yii\web\YiiAsset',
        'yii\web\JqueryAsset',
    ],
    // Asset bundle for compression output:
    'targets' => [
        'all' => [
            'class' => 'yii\web\AssetBundle',
            'basePath' => '@webroot/assets',
            'baseUrl' => '@web/assets',
            'js' => 'js/all-{hash}.js',    # also without js/
            'css' => 'css/all-{hash}.css', # also without css/
        ],
    ],
    // Asset manager configuration:
    'assetManager' => [
        'basePath' => '@webroot/assets',
        'baseUrl' => '@web/assets',
    ],
];

展开全部

  • 写回答

1条回答 默认 最新

  • duanguanzai6181 2015-05-24 16:21
    关注

    Ok it was mostly because I was using Cygwin. The Java command was a Windows Java command so is not familiar with /cygdrive/c/. Yii deletes the generated temp file so there was no way to know if it was working. I tested to see if Yii generated anything with this:

    'jsCompressor' => 'echo java -jar compiler.jar --js {from} --js_output_file {to}; cp /cygdrive/c/Users/Chloe/workspace/xxx/web/assets/all-temp.js.tmp /tmp/',
    

    Then I could at least look at the combined file. I fixed it by adding a Windows symbolic link (junction would probably work too) in an Administrator console like this

    mkdir \cygdrive
    cd \cygdrive
    C:\cygdrive>mklink /d c C:\
    

    Then /cygdrive/c/Users would exist for Windows apps. (Installing a Cygwin based JRE like OpenJDK and putting it first in the path would probably work too.) I also changed the target to this to get rid of the sub-directories

            'js' => 'all-{hash}.js',    
            'css' => 'all-{hash}.css', 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部