Skip to content

Best practice for extended twig template which requires js and css? #698

Open
@wumke

Description

@wumke

(Sorry if the info is already somewhere in the docs, I searched but did not find...)
When we have a main template (which is never used on its own but only extended) that requires JS and CSS, how should we handle that correctly when using webpack to require JS and CSS?

A simple example is jQuery: the main template uses it and I also want to use it in the template that extends the main template.

  1. We can create a new entry for the main template in webpack.config.js, but when we require the same JS or CSS in the template that extends the main template, does webpack optimize chunks such that it includes 'double requirements by 2 webpack entries in the same page' only once? (I guess not?)
    The pro of this method is that you immediately see which CSS and JS is required for a template and simply cannot forget to add the requirements since they are defined in the template file you are extending.
    The cons are that you need to check all extended twig files when checking which additional stuff to require... and probably also less optimized chunks?

  2. Another option is to require a JS and CSS file in the extending template that requires everything needed for the main template. (Note that these files are not referenced in the main template.)
    The pro of this method is that you can track JS and CSS dependencies down to the last template and then require only additional items that are not yet defined.
    The con is that you cannot immediate see what is required by a template since it is not written anywhere in the template.

  3. Another option is to check what all templates you extend need and require that in one file for the page, but if the main template would be updated and require additional stuff you must add it as a require in all files that extend the main template (which is a very bad thing)

Maybe... there is another option (did not test yet), sort of combining 1 and 2, by defining a new webpack entry for the main template and include it with encore_entry_link_tags() and encore_entry_script_tags() (method 1) BUT overwriting those blocks when the extending template needs additional stuff (like method 2). That would also allow the main template to be rendered standalone. Also, when no additional requires are needed you do not need to do anything else than extending the template.

What do you guys think about this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions