使用 EJS 重建 webpack-dev-server

webpack-dev-server rebuild with ejs

提问人:Jarosław Wlazło 提问时间:4/12/2017 更新时间:11/13/2019 访问量:2532

问:

我正在使用带有此配置的 webpack-dev-server:

import webpack from 'webpack';
import autoprefixer from 'autoprefixer';
import ExtractTextPlugin from 'extract-text-webpack-plugin';
import CaseSensitivePathsPlugin from 'case-sensitive-paths-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';

const exports = {
    devtool: 'cheap-module-source-map',
    entry: {
        bundle: `${__dirname}/src/main.ejs`,
        commons: [
            'lodash',
            'webpack-dev-server/client?http://localhost:8090',
            'webpack/hot/only-dev-server'
        ]
    },
    module: {
        rules: [
            {
                test: /\.(js)?$/,
                include: `${__dirname}/src`,
                loader: 'babel-loader'
            }, {
                test: /\.(scss|css)$/,
                include: [
                    `${__dirname}/src`
                ],
                use: ExtractTextPlugin.extract({
                    fallback: 'style-loader',
                    use: ['css-loader', 'postcss-loader', 'sass-loader']
                })
            },  {
                test: /\.(ejs)$/,
                include: `${__dirname}/src`,
                use: 'ejs-render-loader'
            }, {
                test: /\.(png|jpg|gif)$/,
                include: [
                    `${__dirname}/src`
                ],
                loader: 'file-loader'
            }, {
                test: /\.svg/,
                include: [
                    `${__dirname}/src`
                ],
                loader: 'file-loader?mimetype=image/svg+xml'
            }, {
                test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                include: [
                    `${__dirname}/src`
                ],
                loader: 'file-loader?mimetype=application/font-woff'
            }, {
                test: /\.(ttf|eot)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                include: [
                    `${__dirname}/src`
                ],
                loader: 'file-loader'
            }
        ]
    },
    resolve: {
        extensions: ['.js', '.ejs', '.scss']
    },
    output: {
        path: `${__dirname}/dist`,
        filename: 'bundle.js'
    },
    devServer: {
        contentBase: `${__dirname}/dist`,
        publicPath: 'http://localhost:8090',
        hot: true,
        historyApiFallback: true,
        host: 'localhost',
        port: 8090,
        inline: true
    },
    plugins: [
        new ExtractTextPlugin('styles.css'),
        new webpack.optimize.CommonsChunkPlugin({
            name: 'commons',
            filename: 'commons.js',
            minChunks: Infinity

        }),
        new CaseSensitivePathsPlugin(),
        new webpack.LoaderOptionsPlugin({
            options: {
                postcss: [autoprefixer({
                    browsers: [
                        'last 2 Chrome versions',
                        'last 2 Firefox versions',
                        'last 2 edge versions',
                        'IE >= 9',
                        'Safari >= 7',
                        'iOS >= 7'
                    ]
                })]
            }
        }),
        new HtmlWebpackPlugin({
            filename: 'index.html',
            hash: true,
            template: 'src/main.ejs'
        })
    ]
};

module.exports = exports;

我的文件看起来像这样:main.ejs

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="styles.css" />
    <title>Webpack App</title>
</head>
<body>
    <div id="app">
        <% include components/navigation/navigation.ejs %>
    </div>
</body>
</html>

情况是,当我的任何其他文件发生更改时,webpack-dev-server 不会重建(例如。 我已包含在 ),它仅在我对文件应用任何更改时才会重建。我已经在网上搜索了这个问题的解决方案,但没有任何成功。.ejscomponents/navigation/navigation.ejsmain.ejsmain.ejs

模板 webpack ejs webpack-dev-server 重建

评论

0赞 Larry Lane 11/29/2017
我遇到了类似的问题,我找到了解决方案,我会告诉你的。
0赞 Bynho 2/2/2018
你找到解决方案了吗?
0赞 Jarosław Wlazło 8/14/2018
前段时间我在搜索,我只找到了信息,这只是 .ejs 扩展名的问题,这是一个错误。还没解决...
1赞 N.J.Dawson 9/23/2018
现在使用 .html 扩展名时遇到此问题。很困惑

答:

0赞 Nima Hejazi 11/13/2019 #1

在观看模式下运行它:

$ webpack-dev-server --watch

或在 :webpack.config.js

devServer: {
    watchContentBase: true,
}