twig-webpack-extension
twig-webpack-extension copied to clipboard
Dynamic value for webpack_entry_css
We have a case where we include a different CSS file based on different conditions.
Let's say we have a cssFileName
variable which is a string. In that case {% webpack_entry_css cssFileName %}
does not work. Is there any other way of doing this?
Hello.
Looks like it is possible to do.
By changing
$entryName = $stream->expect(Token::STRING_TYPE)->getValue();
to
$entryName = $this->parser->getExpressionParser()->parseExpression();
and by changing return node
I'll be able to try this on the next week only.
Same Problem here. Is there any soloution now?
Hello.
The problem with "dynamic" variables is that they are dynamic.
Currently extension makes all file reads at template compile time.
Also it fails at compile time if some files are missing.
If we add something like {{ dyn_webpack_entry_css(yourFileNameVar, true) }}
for inlining yourFileNameVar.css
dynamically.
We will start to make two additional file reads on each template render.
so from performance point of view it is better to use switch
{% switch cssFileName %}
{% case 'landing_bar' %}
{% webpack_entry_css 'bar' %}
{% case 'landing_foo' %}
{% webpack_entry_css 'foo' %}
{% default %}
{% webpack_entry_css 'main' %}
{% endswitch %}
May be just add function like {{ webpack_entry_path('landing_foo.css') }}
-> /build/landing_foo.css
?