Webpack开发服务器监视Twig

eng*_*nws 5 symfony webpack webpack-dev-server

我将Symfony 4和Symfony Encore一起使用来处理资产和一些有用的功能,例如HMR。

目前,我可以处理Sass文件,CSS文件,JS等,并且可以与HMR正常工作。

现在,我希望能够使Weback开发服务器监视* .twig文件进行更改并触发实时重新加载(因为热重新加载对于服务器端呈现的模板而言不是一种选择)。

我已经看到了有关--watchContentBasecontentBase选项的信息,但就我而言,它什么也没做:

WDS CLI:

./node_modules/.bin/encore dev-server --hot --disable-host-check --watchContentBase --contentBase ./templates/ --reload
Run Code Online (Sandbox Code Playgroud)

webpack.config.js:

const Encore = require('@symfony/webpack-encore');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

Encore
    .setOutputPath('public/build/')
    .setPublicPath('/build')
    .cleanupOutputBeforeBuild()
    .autoProvidejQuery()  
    .addPlugin(new MiniCssExtractPlugin('[name].css'))
    .enableSourceMaps(!Encore.isProduction())
    .addLoader({
        test: /\.(sc|sa|c)ss$/,
        use: ['css-hot-loader'].concat(
            MiniCssExtractPlugin.loader,
            {
                loader: 'css-loader'
            },
            {
                loader: 'postcss-loader'
            },
            // {
            //     loader: 'postcss-loader'
            // },
            {
                loader: 'sass-loader'
            }            
        ),
      },)
      .addLoader({
        test: /\.twig$/,
        loader: 'raw-loader'
      },)
    .enableVersioning(Encore.isProduction())
    .addEntry('autocall-main', './assets/js/index.js')
    // .addStyleEntry('autocall-main', ['./assets/scss/index.scss'])
    .splitEntryChunks()
    .enableSingleRuntimeChunk()
;
const config = Encore.getWebpackConfig();

module.exports = config;
Run Code Online (Sandbox Code Playgroud)

我的项目文件/文件夹遵循经典的Symfony 4结构:https//github.com/symfony/demo

我在那里想念什么?

Nga*_*ine 5

2020年的今天,我有两个解决方案:

Webpack配置解决方案

正如你所说:I've seen things about --watchContentBase and contentBase options...,这与安可无关。它是默认的 webpack 配置,您可以在此处从 webpack 文档了解更多信息

根据此处的高级 Webpack 配置文档,您可以通过调用来扩展 webpack 配置var config = Encore.getWebpackConfig();

我已经实现如下面的代码所示。对于我的情况,它工作正常。

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

// Manually configure the runtime environment if not already configured yet by the "encore" command.
// It's useful when you use tools that rely on webpack.config.js file.
if (!Encore.isRuntimeEnvironmentConfigured()) {
    Encore.configureRuntimeEnvironment(process.env.NODE_ENV || 'dev');
}

Encore
    // directory where compiled assets will be stored
    .setOutputPath('public/build/')
    .setPublicPath('/build')
    .addEntry('global', './assets/app.js')

    // ... Your other encore code


    // EXTEND/OVERRIDE THE WEBPACK CONFIG

    const fullConfig = Encore.getWebpackConfig();
    fullConfig.name = 'full';

    // watch options poll is used to reload the site after specific set time
    // polling is useful when running Encore inside a Virtual Machine
    // more: https://webpack.js.org/configuration/watch/
    fullConfig.watchOptions = {
        poll: true,
        ignored: /node_modules/
    };

    fullConfig.devServer = {
        public: 'http://localhost:3000',
        allowedHosts: ['0.0.0.0'],
        // extend folder to watch in a symfony project
        // use of content base
        // customize the paths below as per your needs, for this simple 
        //example i will leave them as they are for now.
        contentBase: [
            path.join(__dirname, 'templates/'), // watch twig templates folder
            path.join(__dirname, 'src/') // watch the src php folder
        ],
        // enable watching them
        watchContentBase: true,
        compress: true,
        open: true,
        disableHostCheck: true,
        progress: true,
        watchOptions: {
            watch: true,
            poll: true
        }
    };


// export it
module.exports = fullConfig;
Run Code Online (Sandbox Code Playgroud)

另一种解决方案

如果您需要一个简单的实现,可以使用:webpack-watch-files-plugin。我更喜欢这个,当您阅读这个答案时,它可能会被放弃,但还有许多其他人具有相同的功能。在Symfony 文档中,您可以实现自定义加载器和插件,如下所示。使用上面提到的插件,我们可以按如下方式实现它:

// webpack.config.js
const WatchExternalFilesPlugin = require('webpack-watch-files-plugin').default;

Encore
    // ...your code

     .addPlugin(new WatchExternalFilesPlugin({
            files: [
                '/templates', // watch files in templates folder
                '/src', // watch files in src folder
                '!../var', // don't watch files in var folder (exclude)
            ],
            verbose: true
        }))

    //...your code
;
Run Code Online (Sandbox Code Playgroud)

干杯。快乐编码!


Sia*_*vas 0

加载器还需要知道文件的位置.twig,这些文件在 Symfony 4 中位于/templates目录中。考虑到默认结构,这应该适合您:

  ...
  .addLoader({
    test: /\.twig$/,
    loader: 'raw-loader',
    include: [
      path.resolve(__dirname, "templates")
    ],
  },)
  ...
Run Code Online (Sandbox Code Playgroud)