Skip to content
Advertisement

Using webpack on a common Symfony Bundle

I have a “common” bundle that is used on several Symfony projects. This bundle contains the assets we use on our different projects (js and css). Until now this bundle was mainly adding assets with macros that allowed to load the libraries cdn.

JavaScript

We use macros to load only the scripts we want for each page. We manage the dependencies ourselves.

But we want to start using npm + webpack to manage the js library updates (jquery, bootstrap,etc…). So I installed webpack on the “common” bundle (and not on our different projects). I also created an entry for each library (see code below). And to avoid re-importing all dependencies in each entry, I activated the splitEntryChunks option.

Now my webpack.config.js look like this:

JavaScript

And my twig macros look like this :

JavaScript

I can’t use {{ encore_entry_script_tags() }} because webpack is not installed on our mains applications so y tried to load the chunks myself.

It’s really ugly and it doesn’t even work.

Bootstrap-select initialization is called 4 times and I end up with duplicate selection fields.

also, I have this error message from datepicker :

Uncaught TypeError: Cannot read property ‘regional’ of undefined

How can i improve the webpack configuration ? If possible, I would like to continue to use exactly the same macros, i can’t refactor all our other project to stop using them.

Advertisement

Answer

So I managed to change my chunks configuration and now it’s easier to maintain inside my macros webpack.config.js :

JavaScript

it creates one chunk per library, but then it’s easy to divide them into my macros.

JavaScript

The vendors/chunks are duplicated but the browser don’t load them each time because they are the same file, so it’s ok.

It’s probably not the cleanest solution but it works in my specific use case.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement