gpt4 book ai didi

symfony - 在 Symfony 中使用 Assetic 禁用合并和缩小 Assets

转载 作者:行者123 更新时间:2023-12-04 13:20:21 25 4
gpt4 key购买 nike

使用以下配置,即使在开发环境(这是我目前使用的环境)中,我的 css 和 javascript 文件也可以缩小并组合到浏览器中。

我怎样才能让 Assetic 在开发环境中既不合并也不缩小这些文件?

app/config/config/yml

assetic:
debug: %kernel.debug%
use_controller: false
bundles: [ MyFirstBundle, MySecondBundle, MyThirdBundle ]
filters:
cssrewrite: ~
yui_css:
jar: %kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar
yui_js:
jar: %kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar
less:
node: %less_node_bin%
node_paths: [%less_node_modules%]
apply_to: ".less$"
assets:
main_css:
inputs:
- %kernel.root_dir%/../web/css/file01.css
- %kernel.root_dir%/../web/css/file02.css
- %kernel.root_dir%/../web/css/file03.css
output: css/main.css
filter:
- yui_css
- less
other_css:
inputs:
- %kernel.root_dir%/../web/css/file04.css
- %kernel.root_dir%/../web/css/file05.css
- %kernel.root_dir%/../web/css/file06.css
output: css/other.css
filter:
- yui_css
- less
other_js:
inputs:
- %kernel.root_dir%/../src/My/FirstBundle/Resources/public/js/file01.js
- %kernel.root_dir%/../src/My/FirstBundle/Resources/public/js/file02.js
- %kernel.root_dir%/../src/My/FirstBundle/Resources/public/js/file03.js
output: js/other.js
filter:
- yui_js
main_js:
inputs:
- %kernel.root_dir%/../src/My/FirstBundle/Resources/public/js/file01.js
- %kernel.root_dir%/../src/My/FirstBundle/Resources/public/js/file02.js
- %kernel.root_dir%/../src/My/FirstBundle/Resources/public/js/file03.js
output: js/main.js
filter:
- yui_js

app/config/config_dev.yml
assetic:
debug: %kernel.debug%
use_controller: true
bundles: [ MyFirstBundle, MySecondBundle, MyThirdBundle ]
filters:
cssrewrite: ~
yui_css:
jar: %kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar
yui_js:
jar: %kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar
assets:

Twig 模板:
<head>
<meta charset="utf-8">
<title>My title</title>
{% block stylesheets %}
<link rel="stylesheet" href="{{ asset('css/main.css') }}">
{% endblock %}
{% block javascripts %}
<script src="{{ asset('js/main.js') }}"></script>
{% endblock %}
<!--
Also tried this type of block, without success:
{% block javascripts %}
{% javascripts '@main_js' %}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
{% endblock %}
-->
</head>

最佳答案

事情是“config.yml”文件是总是 用于 全部 环境。例如,在开发环境中,“config.yml”和“config_dev.yml”文件被合并以形成最终的配置。

例如我们有 config.yml 文件与此:

foo:
bar: baz

我们有 config_dev.yml 像这样:
foo:
go: run

最终的配置将有
foo:
bar: baz
go: run

你看到问题了吗?哟需要放置 产品不在 中的特定配置参数(您希望仅在 prod 环境中使用,例如在您的情况下使用 js minifiers) config.yml 文件,但在 config_prod.yml 文件。

所以你应该在你的 中有这样的东西config.yml :
assetic:
assets:
my_js_asset:
inputs:
...

在您的 中有类似的内容config_prod.yml 文件
assetic:
assets:
my_js_asset:
filters: [yui_js]
output: scripts.min.js

编辑 1:

但是,这将仅解决缩小(和其他可能的过滤器)问题。

也许我错了,但是,据我所知,现在不可能分离在同一个命名 Assets 中定义的各种输入文件。这背后的逻辑在于相应 Twig 标签(节点)的 Assets 实现,因为它们自己的单独文件 生成 并且可以在dev环境中执行assetic:dump命令后在目标目录中找到(或者如果stylesheets\javascripts twig标签的参数“combine”属性设置为true)。如果你想检查这个并更好地理解实现,这是一个很好的起点: https://github.com/kriswallsmith/assetic/blob/master/src/Assetic/Extension/Twig/AsseticNode.php

关于symfony - 在 Symfony 中使用 Assetic 禁用合并和缩小 Assets ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30871885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com