mariusbalcytis / webpack-bundle

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

Manifest does not include files referenced in included twig templates #72

Closed lf-jeremy closed 7 years ago

lf-jeremy commented 7 years ago

If a twig template includes another template, and the included template calls webpack_asset with an image reference, that file is not included in the manifest and will throw an exception during rendering:

An exception has been thrown during the rendering of a template ("No information in manifest for @app/file-that-exists.png (key file-that-exists-{file-hash}). Is maba:webpack:dev-server running in the background?").

E.g.,

{# template.html.twig #}
{% block content %}
    {{ 'welcome.welcome_message'|trans }}
    {% include 'sponsors.html.twig' %}
{% endblock %}

{# sponsors.html.twig #}
<ul class="list-unstyled list-inline">
  <li><img src="{{ webpack_asset("@app/foo-logo.png") }}" alt="Foo"></li>
  <li><img src="{{ webpack_asset("@app/bar-logo.png") }}" alt="Bar"></li>
</ul>

Testing the same call to webpack_asset directly in a primary template and not an included partial, it works (showing this is not a case of an erroneous path). Incidentally, if you previously include the same image and it is already in the manifest, loading the image in an included twig template works fine, but that is not practical.

I have not tested loading other asset types besides images, but I'd imagine using images inside an included partial twig template is a common use case for the sake of re-use, organization, and keeping templates slim.

mariusbalcytis commented 7 years ago

Actually, include tags are not parsed. Bundle just scans configured directories for *.twig files, parses them and processes webpack_asset function calls.

So, probably your included twig file is in some different directory, which is not scanned for twig templates. Could that be the case?

Do you have enabled_bundles or twig.additional_directories configured in config.yml?

lf-jeremy commented 7 years ago

The partial template I experienced this with was directly in my AppBundle's Resources/views/ directory and not in a subdirectory or another directory.

I do not have enabled_bundles or twig.additional_directories configured, but I can verify in my var/cache/dev/appDevDebugProjectContainer.xml file that my actual collection of enabled_bundles includes my AppBundle. Other twig files (including sibling twig files in the filepath) in my AppBundle have assets added to the manifest when webpack_asset is called.

lf-jeremy commented 7 years ago

I figured out my issue - the bundle was globbing for twig templates expecting the .twig extension, but some of my partials omitted the extension (e.g. partial.html instead of partial.html.twig). I tried checking that before, but I must have not cleared the app cache fully before troubleshooting other possibilities.

Once I changed my partial to {name}.html.twig, it worked perfectly. Thanks!