资产化并在每次更改时生成新文件名

Laj*_*rek 5 javascript css symfony assetic

嗨,我在symfony 3中使用资产。但是我有问题,我的资产定义如下:

{% stylesheets filter='cssrewrite' filter='?uglifycss'
        'assets/font-awesome-4.6.3/css/font-awesome.min.css'
        'assets/bootstrap-3.3.7/css/bootstrap.min.css'
         ...
%}
Run Code Online (Sandbox Code Playgroud)

在控制台运行 php bin/console assetic:watch

在更改CSS或JS后,它将生成新文件,但名称相同ce9c2ef.css

但这是有问题的,因为部署后,css文件具有更改内容但没有文件名,因此所有人看到的都是旧的CSS ...

问:每次更改CSS时,如何更改生成的文件名?

{%stylesheets%}中有选项'output ='path / filename.js',但我无法添加<?php echo $var; ?>{{ var }}...

更新:

资产配置:

assetic:
debug:          '%kernel.debug%'
use_controller: '%kernel.debug%'
filters:
    cssrewrite: ~
    uglifyjs2:
        bin: "%kernel.root_dir%/Resources/node_modules/uglify-js/bin/uglifyjs"
    uglifycss:
        bin: "%kernel.root_dir%/Resources/node_modules/uglifycss/uglifycss"
Run Code Online (Sandbox Code Playgroud)

模板配置:

templating:
    engines: ['twig']
Run Code Online (Sandbox Code Playgroud)

编辑:

所以我找到了解决方案PARTIAL解决方案:

要配置添加:

assetic:
    workers:
        cache_busting: ~
Run Code Online (Sandbox Code Playgroud)

之后,您的文件将ce9c2ef-d1e35a0_filename.css在开发和ce9c2ef-d1e35a0.css生产中看起来像...

但是在部署中,您必须先清除缓存,以便您有2个散列,第一个ce9c2ef仍然相同(我不明白存在的要点),然后d1e35a0更改,以便最终解决浏览器缓存的问题...。

但是,如果您对CSS进行更改,则assetic:watch会对其进行编译,但是页面会加载旧文件...!

有史以来最糟糕的捆绑软件意味着更改文件名是基本的事情,在互联网上,如何解决它有很多方法,我一天又一天尝试了一次,直到我终于成功...

Jas*_*man 2

When you're using assetic, one way to solve your issue is by giving your assets a version in your framework:templating section of app/config.yml:

assets:
    version: "%application_version%"
Run Code Online (Sandbox Code Playgroud)

Then you can specify your version in your parameters.yml/parameters.yml.dist file:

parameters:
    application_version: 1.0.1
Run Code Online (Sandbox Code Playgroud)

Then you can load your stylesheets or javascript like so:

{% stylesheets output='css/sites.css' filter='cssrewrite'
    'assets/font-awesome-4.6.3/css/font-awesome.min.css'
    'assets/bootstrap-3.3.7/css/bootstrap.min.css'
     ...
%}
    <link href="{{ asset_url }}" type="text/css" rel="stylesheet" media="all" />
{% endstylesheets %}
Run Code Online (Sandbox Code Playgroud)

Now when you dump your assets, it will automatically append ?1.0.1 (or whatever version you are on" to the end of them. For example, in production you would see the following:

<link href="/css/site.css?1.0.1" type="text/css" rel="stylesheet" media="all" />
Run Code Online (Sandbox Code Playgroud)

Note that there are different ways of doing naming strategies, and this can get tricky if you forget to update your assets version every time you make changes to your assets, but there are ways to improve upon that strategy. This should get you up and running though.

如果您注意到我没有手动指定uglify*过滤器 - 您可以通过将其放入app/config_prod.yml来自动应用这些过滤器:

assetic:
    filters:
        uglifycss:
            apply_to: "\.css$"
        uglifyjs2:
            apply_to: "\.js$"
Run Code Online (Sandbox Code Playgroud)