fullpipe / twig-webpack-extension

Inject your webpack entry points into twig templates with easy.
MIT License
51 stars 14 forks source link

Dynamic value for webpack_entry_css #42

Open danijel-bjelancevic opened 1 year ago

danijel-bjelancevic commented 1 year ago

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?

fullpipe commented 1 year ago

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.

david-linke commented 5 months ago

Same Problem here. Is there any soloution now?

fullpipe commented 5 months ago

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 commented 5 months ago

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