简体   繁体   English

如何从Assetic使用Symfony中的CSS和JS缩小

[英]How to use CSS and JS minification in Symfony from assetic

I am trying to get CSS and JS minification set up in a Symfony project using assetic. 我正在尝试使用Assetic在Symfony项目中设置CSS和JS最小化。 I can not use java or node.js or anything like that, so I was following the directions at Symfony2 minify without java or node by userlond to get the filters in assetic that use minify to do it for me. 我不能使用java或node.js或类似的东西,所以我遵循Symfony2 minify上的指示, 没有 userlond使用java或node来获取使用minify为我做的资产过滤器。

I followed the instructions and have installed minify via composer, but I keep getting the following when I do an assetic dump: 我已按照说明进行操作,并已通过composer安装了minify,但是在进行资产转储时,我不断收到以下内容:

php.CRITICAL: Fatal Error: Class 'JSMinPlus' not found {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalErrorException(code: 0): Error: Class 'JSMinPlus' not found at /var/www/html/new_test_1/vendor/kriswallsmith/assetic/src/Assetic/Filter/JSMinPlusFilter.php:32)"}

[Symfony\Component\Debug\Exception\ClassNotFoundException]      
  Attempted to load class "JSMinPlus" from the global namespace.  
  Did you forget a "use" statement?

If I don't try use the jsminplus filter, but am still using the minifycsscompressor filter, it all seems to work fine (and I get my CSS minified). 如果我不尝试使用jsminplus过滤器,但仍在使用minifycsscompressor过滤器,那么一切似乎都可以正常工作(并且我的CSS缩小了)。

Has something changed in the minify project that has created an incompatibility or something? 在minify项目中是否发生了某些更改,这些更改造成了不兼容性?

The code I am using in the TWIG template is: 我在TWIG模板中使用的代码是:

{% javascripts
    '@TemplateBundle/Resources/public/media/site-assets/javascript/vendor/jquery-1.12.1.min.js'
    '@TemplateBundle/Resources/public/media/site-assets/javascript/vendor/*.js'
    '@TemplateBundle/Resources/public/media/site-assets/javascript/*.js'
    filter='jsminplus'
%}

    <script src="{{ asset_url }}"></script>

{% endjavascripts %}

If I take out the filter='jsminplus' line, it all works fine (and is just not minified). 如果我删除了filter='jsminplus'行,那么一切都很好(只是没有缩小)。

The answer you are basing this on is outdated, assetic changed since then. 您基于此的答案已过时,此后资产发生了变化。 So unless you are specifically using an older version of Symfony you will need something along the lines of 因此,除非您专门使用旧版本的Symfony,否则您将需要

# app/config/config.yml
assetic:
    filters:
        jsqueeze: ~
        # ...

Then 然后

{% javascripts filter="?jsqueeze" output="js/app.js"
    "@TemplateBundle/Resources/public/media/site-assets/javascript/*.js"
    "@TemplateBundle/Resources/public/media/site-assets/javascript/vendor/*.js"
    "@TemplateBundle/Resources/public/media/site-assets/javascript/*.js"
%}
    <script src="{{ asset_url }}"></script>
{% endjavascripts %}

Watch out that the ? 当心了吗? in ?jsqueeze means this will only be minified in prod environment. ?jsqueeze这意味着只会在产品环境中?jsqueeze

See the documentation for a non javascript minifying solution . 请参阅说明文件以获取非javascript压缩解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM