如何在 Symfony 项目中使用 webpack-encore 设置实时重新加载?

发布于 2025-01-12 01:40:46 字数 2015 浏览 4 评论 0原文

我使用给定的命令 symfony new app --webapp 创建了一个 Symfony 完整 Web 应用程序。它附带了配置了 webpack-encoreWebpack。我可以使用 npm run watch 编译我的资产。但是当我进行更改时,浏览器不会自动重新加载。我已经按照Symfony的官方文档webpack-dev-server尝试了此处,但不起作用。

webpack.config.js(我刚刚删除了注释):

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();

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"
    }
}

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 I make changes. I have tried webpack-dev-server following Symfony's official documentation here, but didn't work.

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();

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"
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我家小可爱 2025-01-19 01:40:46

您可以借助 Browsersync 在 Symfony 项目中使用 webpack-encore 设置实时重新加载。仔细按照以下步骤操作,您就可以开始了。

  1. 第一步:
npm i browser-sync-webpack-plugin browser-sync dotenv --save-dev
  1. 编辑 webpack.config.js (有要添加的行的注释):
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/*.css"],
                },
            ],
            notify: false,
        },

        {

            reload: true,
        }
    ))

;

module.exports = Encore.getWebpackConfig();
  1. .env 中添加下面的行(网址应该是您要添加的行)运行 symfonyserve 时获取):
# The url should be the one you get when you run symfony serve
PROXY=http://127.0.0.1:8000/
  1. 在项目根文件夹中打开一个终端并运行:
symfony serve
  1. 在项目根文件夹中打开第二个终端并运行:
npm run watch

You can set up live reloading with webpack-encore in a Symfony project with the help of Browsersync. Follow the below steps carefully and you will be good to go.

  1. Step one:
npm i browser-sync-webpack-plugin browser-sync 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/*.css"],
                },
            ],
            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 run symfony serve):
# The url should be the one you get when you run symfony serve
PROXY=http://127.0.0.1:8000/
  1. Open a terminal in your project root folder and run:
symfony serve
  1. Open a second terminal in your project root folder and run:
npm run watch
我最亲爱的 2025-01-19 01:40:46

我建议使用 webpack 内置的 liveReload。

您可以使用 --live-reload 标志在 encore 中启用它。
例如 encore dev-server --live-reload

要运行 dev-server,您需要运行 npm run dev-server 而不是 npm run watch

实时重新加载 twig / php 文件更改。

如果您想在树枝更改时重新加载,可以使用 webpack.devServer.watchFiles.paths
请参阅 https://webpack.js.org/configuration/dev-server/#devserverwatchfiles

我没有确切的语法,但在 yourwebpack.config.js 文件中,你可以

// webpack.config.js
// ...

Encore
    // ...

    .configureDevServerOptions(options => {
        options.watchFiles = {
            paths: ['src/**/*.php', 'templates/**/*'],
        }
    })
;

Id recommend using webpack's built-in liveReload.

You can enable it in encore with the --live-reload flag.
e.g. encore dev-server --live-reload

To run the dev-server, you would run npm run dev-server instead of npm run watch

Live reload on twig / php file changes.

If you want to reload on twig changes, you can use webpack.devServer.watchFiles.paths
See https://webpack.js.org/configuration/dev-server/#devserverwatchfiles

I dont have the exact syntax, but in yourwebpack.config.js file, you can

// webpack.config.js
// ...

Encore
    // ...

    .configureDevServerOptions(options => {
        options.watchFiles = {
            paths: ['src/**/*.php', 'templates/**/*'],
        }
    })
;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文