【问题标题】:How to make webpack-dev-server pick up latest changes如何让 webpack-dev-server 获取最新的更改
【发布时间】:2021-12-16 01:08:49
【问题描述】:

我正在使用 Webpack 5,无法将 dev-server 设置为立即获取最新更改。这就是为什么当我的项目有任何更改时,我应该先做yarn build,然后每次都做yarn dev。我已经在这里看到了同样的问题Webpack-dev-server doesn't pick up latest changes,但是writeToDisk: true 在控制台中显示了一个错误。我已经附上了我的 webpack.config.js 文件。

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const miniCss = require('mini-css-extract-plugin');

module.exports = {
    entry: { main: './src/index.js' },
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'main.js',
        publicPath: ''
    },
    mode: 'development',
    devServer: {
        static: {
            directory: path.join(__dirname, "dist")
        },
        compress: true,
        port: 8080,
        devMiddleware: {
            publicPath: "//localhost:8080",
        },
        hot: "only",
        open: true
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                use: 'babel-loader',
                exclude: '/node_modules/'
            },
            {
                test: /\.pug$/,
                loader: 'pug-loader',
                options: {
                    pretty: true
                }
            },
            {
                test: /\.css$/,
                use: [MiniCssExtractPlugin.loader, {
                    loader: 'css-loader'
                }]
            },
            {
                test: /\.(s*)css$/,
                use: [
                    miniCss.loader,
                    'css-loader',
                    'sass-loader',
                ]
            },
            {
                test: /\.(png|svg|jpg|gif|woff(2)?|eot|ttf|otf)$/,
                type: 'asset/resource'
            },
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: './src/pages/page/page.pug',
            filename: 'index.html'
        }),
        new MiniCssExtractPlugin(),
        new miniCss(),
    ]
}

【问题讨论】:

    标签: javascript webpack webpack-dev-server


    【解决方案1】:

    您的问题很可能与hot: "only" 有关。根据https://webpack.js.org/configuration/dev-server/,如果不是hot: true,您将不会看到构建失败的页面刷新。

    除此之外,我看不出您的代码有任何明显错误。我怀疑如果您将其更改为hot: true,它应该会刷新页面并且您还有其他一些构建失败。

    【讨论】:

    • 不幸的是,它没有帮助,我将 hot: "only" 更改为 hot: true 并且它的行为方式相同
    • 尝试将contentBase 添加到您的静态选项中,也许它没有观看,因为它不知道要观看的内容库。这只是一个猜测。
    • 我刚试过,控制台返回错误'options object that does not match the API schema'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 1970-01-01
    • 2017-08-07
    相关资源
    最近更新 更多