mariusbalcytis / webpack-bundle

Bundle to Integrate Webpack into Symfony
MIT License
122 stars 36 forks source link

Twig namespaces not supported #65

Closed netsuo closed 7 years ago

netsuo commented 7 years ago

Hi,

In one of my projects, I use multiple path per namespaces to override some twig files, as shown here:

https://symfony.com/doc/current/templating/namespaced_paths.html#multiple-paths-per-namespace

The problem is that the webpack compiler doesn't parse them, so in one of the situations, I have a vendor bundle that has some views that uses assetic, but they are overriden in my project by using the namespaced path.

Let's imagine the vendor reference it's assets by using the alias @VendorTest/index.html.twig, this file uses assetic. Now, in my main project's config.yml, I created a new alias @VendorTest that points to my project's resources folder where I created another "index.html.twig" that does not use assetic. I didn't include assetic in my project's AppKernel, I don't want to use it.

Webpack compile doesn't take this into consideration and list all files, my project's views, where he doesn't find any problem and my vendor's views where it crashes because he tries to parse them and they use Assetic.

I think webpack compiler should take the overriden views into consideration using Twigs own template loader, you can see the code here: https://github.com/twigphp/Twig/blob/2.x/lib/Twig/Loader/Filesystem.php#L176

I think this would also solve bundle inheritance problem, as it would use Twig's own template finder and only load used views.

mariusbalcytis commented 7 years ago

I don't think that bundle should look for which files are included or not via twig, parse namespaces etc. It just parses all files which are configured. If it would make assumptions, problems could occur if that file (which uses assetic in your case) would be parsed and included with full path etc.

There are already 2 options for handling the case you've described:

  1. Configuring how twig errors are handled.
  2. Configuring which bundles should be included (what directories to scan for twig files).

Configuration options:

maba_webpack:
    enabled_bundles:
        - ApplicationBundle
    twig:
        suppress_errors:      true              # whether files not found or twig parse errors should be ignored
                                                # defaults to true in dev environment
                                                # defaults to "ignore_unkwowns" in prod - this option ignores
                                                #     unknown functions etc., but fails on syntax errors
                                                # set to false to always fail on any twig error

I would recommend just ignoring twig errors.

In fact, it should already work in your case - maybe there's some issue with that functionality in the bundle. Could you clarify how it crashes - what exact message are you getting? Does it crash on development or production environment? Is suppress_errors configured? What exact version of twig are you using?

netsuo commented 7 years ago

You are right. Upon further tests I saw that it was indeed working even with the error messages. I didn't know about the config though. I will use suppress errors.

mariusbalcytis commented 7 years ago

Closing the issue, as this is already handled by the bundle. Please provide any additional information if you think this could be clearer in docs, thanks!