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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?