如何在 Symfony 项目中使用 webpack-encore 设置实时重新加载?
我使用给定的命令 symfony new app --webapp 创建了一个 Symfony 完整 Web 应用程序。它附带了配置了 webpack-encore
的 Webpack。我可以使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以借助 Browsersync 在 Symfony 项目中使用
webpack-encore
设置实时重新加载。仔细按照以下步骤操作,您就可以开始了。webpack.config.js
(有要添加的行的注释):.env
中添加下面的行(网址应该是您要添加的行)运行 symfonyserve 时获取):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.webpack.config.js
(there are comments for the lines to add):.env
(the url should be the one you get when you runsymfony serve
):我建议使用 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 文件中,你可以
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 ofnpm 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