Please wait...

Symfony and Webpack


Before the Symfony 3 framework release, we used Assetics to manage the assets and dependencies of our projects.

We are now using a more optimized tool, named Webpack Encore, based on Webpack and Node.

According to the docs:

Webpack Encore is a simpler way to integrate Webpack into your application. It wraps Webpack, giving you a clean & powerful API for bundling JavaScript modules, pre-processing CSS & JS and compiling and minifying assets. Encore gives you professional asset system that's a delight to use.

There is an Encore minimal configuration:

// webpack.config.js
var Encore = require('@symfony/webpack-encore');

    // directory where compiled assets will be stored
    // public path used by the web server to access the output path
    // only needed for CDN's or sub-directory deploy

     * Add 1 entry for each "page" of your app
     * (including one that's included on every page - e.g. "app")
     * Each entry will result in one JavaScript file (e.g. app.js)
     * and one CSS file (e.g. app.css) if your JavaScript imports CSS.
    .addEntry('app', './assets/js/app.js')
    //.addEntry('page1', './assets/js/page1.js')
    //.addEntry('page2', './assets/js/page2.js')

    // will require an extra script tag for runtime.js
    // but, you probably want this, unless you're building a single-page app

    // enables hashed filenames (e.g. app.abc123.css)

    // uncomment if you use TypeScript

    // uncomment if you use Sass/SCSS files

    // uncomment if you're having problems with a jQuery plugin

module.exports = Encore.getWebpackConfig();

And there are some commands:

yarn encore dev # Pour compiler les assets une fois
yarn encore dev --watch
yarn encore production

If you want to use npm instead of yarn, you should add encore to your package.json scripts section.

Like this:

// package.json 
"scripts": {
    "encore": "encore"

Then you could run encore with this commands:

npm run encore -- dev
npm run encore -- dev --watch

See you soon,