3

Webpack won't recompile any partial file when saving them unless I hit "save" on the main file ( index.ejs ). Everything else is working as entended but I can't figure out how to make my partials recompile the whole app. I am using webpack-dev-server for development and ejs-webpack-loader for the ejs compilation.

package.json:

"scripts": {
    "watch": "webpack --env.NODE_ENV=development --watch --progress --hide-modules",
    "start": "webpack-dev-server --env.NODE_ENV=development --open",
    "build": "webpack --env.NODE_ENV=production --env.production --progress"
  }
"devDependencies": {
    ...
    "ejs-webpack-loader": "^2.2.2",
    "webpack": "^4.41.2",
    "webpack-dev-server": "^3.9.0",
    ...
  }

webpack.config.js:

const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = env => {
    return {
        entry: './src/app.js',
        output: {
            path: path.resolve(__dirname, 'dist'),
            filename: 'app.min.js'
        },
        mode: env.NODE_ENV,
        devServer: {
            contentBase: './dist',
        },
        module: {
            rules: [
                {
                    test: /\.ejs$/,
                    use: [
                        {
                            loader: "ejs-webpack-loader",
                            options: {
                                data: { },
                                htmlmin: env.production
                            }
                        }
                    ]
                },
                {
                    test: /\.js$/,
                    exclude: '/node_modules/',
                    use: {
                        loader: 'babel-loader',
                        options: {
                            presets: ['@babel/preset-env']
                        }
                    }
                },
                {
                    test: /\.s[ac]ss$/i,
                    use: [
                        'style-loader',
                        'css-loader',
                        'sass-loader'
                    ]
                }
            ]
        },
        plugins: [
            new webpack.DefinePlugin({
                'process.env': {
                    'NODE_ENV': JSON.stringify(process.env.NODE_ENV)
                },

            }),
            new HtmlWebpackPlugin({
                template: '!!ejs-webpack-loader!src/index.ejs'
            })
        ]
    }
};

index.ejs:

<!doctype html>
<html lang="en">
    <head>
        <!-- Required meta tags -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <title>Used listing</title>
    </head>

    <body>
        <div class="main container-fluid">
            <% include ./toolbar %>

            <div class="main-content">
                <div class="row">
                    <div class="col col-2">
                        <% include ./sidebar %>
                    </div>

                    <div class="col col-10">
                        <% include ./listing %>
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>

And the partials are just ejs files with plain html.

EDIT: I tried this solution without success: webpack-dev-server rebuild with ejs

Berthy
  • 358
  • 2
  • 8

0 Answers0