【问题标题】:Unable to override path for NodeJS binary in Assetic config无法在 Assetic 配置中覆盖 NodeJS 二进制文件的路径
【发布时间】:2021-01-13 21:50:32
【问题描述】:

我在覆盖 Assetic 中的 UglifyCSS 过滤器使用的 Node.js 二进制文件的路径时遇到问题。 据说可以在/app/config/config.yml中设置路径(Symfony docs,SO question),但是好像没有任何效果,不管我把覆盖值放在哪里。

为了调试它,我更改了vendor/kriswallsmith/assetic/src/Assetic/Filter/UglifyCssFilter.php 中的第 37-39 行以输出值:

/**
 * @param string $uglifycssBin Absolute path to the uglifycss executable
 * @param string $nodeBin      Absolute path to the folder containg node.js executable
 */
public function __construct($uglifycssBin = '/usr/bin/uglifycss', $nodeBin = null)
{
        throw new \RuntimeException('Path to node executable '. $nodeBin . ' Uglify executable '. $uglifycssBin . "\n\$PATH: ". getenv('PATH'));

这是结果

$ ../bin/console assetic:dump --env=prod

                                                                                                                                                                                
  [RuntimeException]                                                                                                                                                            
  Path to node executable /usr/local/bin/node Uglify executable /home/myuser/dev/ptflow-api/app/../node_modules/.bin/uglifycss                                                
  $PATH: /home/myuser/.sdkman/candidates/java/current/bin:/home/myuser/go/bin:/home/myuser/.local/bin:/home/myuser/bin/Telegram:/home/myuser/.yarn/bin:/home/carleri  
  k/.config/yarn/global/node_modules/.bin:/home/myuser/bin:/home/myuser/.yarn/bin:/home/myuser/.config/yarn/global/node_modules/.bin:/home/myuser/go/bin:/home/carleri  
  k/.go/bin:/home/myuser/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin                                        
                                                                                                                                                                                

这表明$nodeBin 始终具有值/usr/local/bin/node。这实际上是我系统上的正确路径,但我需要能够手动设置它才能在找不到正确可执行文件的 Docker 容器中工作。

到目前为止,我看到的示例配置在是否使用参数路径assetic.nodeassetic.node.bin 方面有所不同,但除了assetic.filters.uglifycss.node 之外,我都设置了:

assetic:
    node: /not/a/path/config.yml/node
        bin: '/usr/bin/node'

我还发现了一个似乎可以提供一些见解的文件:

$ cat ./vendor/symfony/assetic-bundle/Resources/config/filters/uglifycss.xml 
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <parameters>
        <parameter key="assetic.filter.uglifycss.class">Assetic\Filter\UglifyCssFilter</parameter>
        <parameter key="assetic.filter.uglifycss.bin">/usr/bin/uglifycss</parameter>
        <parameter key="assetic.filter.uglifycss.node">%assetic.node.bin%</parameter>
        <parameter key="assetic.filter.uglifycss.timeout">null</parameter>
        <parameter key="assetic.filter.uglifycss.node_paths">%assetic.node.paths%</parameter>
        <parameter key="assetic.filter.uglifycss.expand_vars">false</parameter>
        <parameter key="assetic.filter.uglifycss.ugly_comments">false</parameter>
        <parameter key="assetic.filter.uglifycss.cute_comments">false</parameter>
    </parameters>

    <services>
        <service id="assetic.filter.uglifycss" class="%assetic.filter.uglifycss.class%">
            <tag name="assetic.filter" alias="uglifycss" />
            <argument>%assetic.filter.uglifycss.bin%</argument>
            <argument>%assetic.filter.uglifycss.node%</argument>
            <call method="setTimeout"><argument>%assetic.filter.uglifycss.timeout%</argument></call>
            <call method="setNodePaths"><argument>%assetic.filter.uglifycss.node_paths%</argument></call>
            <call method="setExpandVars"><argument>%assetic.filter.uglifycss.expand_vars%</argument></call>
            <call method="setUglyComments"><argument>%assetic.filter.uglifycss.ugly_comments%</argument></call>
            <call method="setCuteComments"><argument>%assetic.filter.uglifycss.cute_comments%</argument></call>
        </service>
    </services>
</container>

这似乎表明设置assetic.filter.uglifycss.nodeassetic.node.bin 都应该有效。也不... XML文件似乎描述了如何使用反射调用过滤器。我不太精通 PHP 开发(NodeJS、Java、.NET 背景),所以我不确定哪些代码会进一步使用它。

我正在使用 Symfony 3.3 和 Assetic 2.8 捆绑包。

【问题讨论】:

    标签: symfony assetic symfony-3.3


    【解决方案1】:

    天哪,我讨厌这种不直观的设置。似乎不仅构建工件被缓存,配置值也是如此!我通过 grepping 整个项目的 /usr/local 发现它,在 /var/cache 中找到它的引用。

    我必须删除缓存才能进行更改:

    PHP Warning:  count(): Parameter must be an array or an object that implements Countable in /home/myuser/dev/ptflow-api/vendor/symfony/monolog-bundle/DependencyInjection/Configuration.php on line 654
    PHP Warning:  count(): Parameter must be an array or an object that implements Countable in /home/myuser/dev/ptflow-api/vendor/symfony/monolog-bundle/DependencyInjection/Configuration.php on line 654
    
                                                                                   
      [RuntimeException]                                                           
      Path to node executable /tmp/nodejs Uglify executable /home/myuser/dev/pt  
      flow-api/app/../node_modules/.bin/uglifycss                                  
      $PATH: /tmp:/usr/bin                                                         
                                                                                   
    
    assetic:dump [--forks FORKS] [--watch] [--force] [--period PERIOD] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--] <command> [<write_to>]
    

    Symfony 配置中的实际配置路径不是assetic.node.bin,而是assetic.node。过滤器也一样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-18
      • 2013-11-14
      • 2020-11-09
      • 1970-01-01
      • 1970-01-01
      • 2015-10-06
      • 1970-01-01
      • 2019-09-30
      相关资源
      最近更新 更多