Home > other >  Symfony, how to set up hot reloading using webpack-encore?
Symfony, how to set up hot reloading using webpack-encore?

Time:03-08

I have created a Symfony full web app with the given command:

symfony new app --webapp

It came with webpack configured with webpack-encore. I can have my assets compiled with:

npm run watch

But the browser don't reload automatically when my css changes for example. I have tried webpack-dev-server following Symfony's official documentation here.

My webpack.config.js (I just removed the comments):

const Encore = require('@symfony/webpack-encore');

if (!Encore.isRuntimeEnvironmentConfigured()) {
    Encore.configureRuntimeEnvironment(process.env.NODE_ENV || 'dev');
}
Encore
    .setOutputPath('public/build/')
    .setPublicPath('/build')
    .addEntry('app', './assets/app.js')
    .enableStimulusBridge('./assets/controllers.json')
    .splitEntryChunks()
    .enableSingleRuntimeChunk()
    .cleanupOutputBeforeBuild()
    .enableBuildNotifications()
    .enableSourceMaps(!Encore.isProduction())
    .enableVersioning(Encore.isProduction())
    .configureBabel((config) => {
        config.plugins.push('@babel/plugin-proposal-class-properties');
    })
    .configureBabelPresetEnv((config) => {
        config.useBuiltIns = 'usage';
        config.corejs = 3;
    })
;

module.exports = Encore.getWebpackConfig();

My package.json:

{
    "devDependencies": {
        "@hotwired/stimulus": "^3.0.0",
        "@symfony/stimulus-bridge": "^3.0.0",
        "@symfony/webpack-encore": "^1.7.0",
        "core-js": "^3.0.0",
        "regenerator-runtime": "^0.13.2",
        "webpack-notifier": "^1.6.0"
    },
    "license": "UNLICENSED",
    "private": true,
    "scripts": {
        "dev-server": "encore dev-server",
        "dev": "encore dev",
        "watch": "encore dev --watch",
        "build": "encore production --progress"
    }
}

CodePudding user response:

1. Solution

Here is how you could set up hot reloading with webpack-encore and Symfony.

  1. Step one:
npm i browser-sync-webpack-plugin dotenv --save-dev
  1. Edit webpack.config.js (there are comments for the lines to add):
const Encore = require('@symfony/webpack-encore');


require("dotenv").config(); // line to add
const BrowserSyncPlugin = require("browser-sync-webpack-plugin"); // line to add


if (!Encore.isRuntimeEnvironmentConfigured()) {
    Encore.configureRuntimeEnvironment(process.env.NODE_ENV || 'dev');
}

Encore

    .setOutputPath('public/build/')
    .setPublicPath('/build')
    .addEntry('app', './assets/app.js')
    .enableStimulusBridge('./assets/controllers.json')
    .splitEntryChunks()
    .enableSingleRuntimeChunk()
    .cleanupOutputBeforeBuild()
    .enableBuildNotifications()
    .enableSourceMaps(!Encore.isProduction())
    .enableVersioning(Encore.isProduction())
    .configureBabel((config) => {
        config.plugins.push('@babel/plugin-proposal-class-properties');
    })
    .configureBabelPresetEnv((config) => {
        config.useBuiltIns = 'usage';
        config.corejs = 3;
    })

    // entry to add 
    .addPlugin(new BrowserSyncPlugin(
        {
            host: "localhost",
            port: 3000,
            proxy: process.env.PROXY,
            files: [
                {
                    match: ["src/*.php"],
                },
                {
                    match: ["templates/*.twig"],
                },
                {
                    match: ["assets/*.js"],
                },
                {
                    match: ["assets/*.scss"],
                },
            ],
            notify: false,
        },

        {

            reload: true,
        }
    ))

;

module.exports = Encore.getWebpackConfig();
  1. Add the line below inside your .env (the url should be the one you get when you make symfony serve):
PROXY=http://127.0.0.1:8000/
  1. Open a terminal in your project folder and type:
symfony serve
  1. Open a second terminal in your project folder and type:
npm run watch

2. Troubleshot

If it is not working, here what you should do :

  • make sure the port used by BrowserSyncPlugin witch is currently 3000 is not used elsewhere, otherwise feel free to change it.
  • make sure the url put inside PROXY in .env is the same as the one you get when you do symfony serve.
  • Related