twig-webpack-extension icon indicating copy to clipboard operation
twig-webpack-extension copied to clipboard

Dynamic value for webpack_entry_css

Open danijel-bjelancevic opened this issue 1 year ago • 4 comments

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?

danijel-bjelancevic avatar Mar 14 '23 10:03 danijel-bjelancevic

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.

fullpipe avatar Mar 14 '23 15:03 fullpipe

Same Problem here. Is there any soloution now?

david-linke avatar Apr 19 '24 12:04 david-linke

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 %}

fullpipe avatar Apr 22 '24 16:04 fullpipe

May be just add function like {{ webpack_entry_path('landing_foo.css') }} -> /build/landing_foo.css?

fullpipe avatar Apr 22 '24 16:04 fullpipe