我已经用给定的命令symfony new app --webapp创建了一个Symfony完整的web应用程序。它附带了配置为Webpack的webpack-encore。我可以用npm run watch编译我的资产。但是浏览器不会在我的I进行更改时自动重新加载。我尝试过使用webpack开发服务器来跟踪Symfony的官方文档这里,但是没有起作用。
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"
}
}发布于 2022-03-06 01:10:12
您可以在Symfony项目中使用webpack-encore在Browsersync的帮助下设置实时重新加载。仔细遵循下面的步骤,你就会走得很好。
npm i browser-sync-webpack-plugin browser-sync dotenv --save-devwebpack.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();.env中(当您生成symfony serve时,url应该是您得到的):# the url should be the one you get when you make symfony serve
PROXY=http://127.0.0.1:8000/symfony servenpm run watchhttps://stackoverflow.com/questions/71364357
复制相似问题