dpgkg42484 2015-04-02 00: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-25 00: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', 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R