Webpack 5 devServer未加载公共静态文件

yhived7q  于 2022-11-30  发布在  Webpack
关注(0)|答案(1)|浏览(168)

我的文件结构如下:

-node_modules
-public
--scripts
-src

还有我的webpack.config.js:

const path = require('path');

module.exports = () => {
    return {
        entry: './src/index.js',
        output: {
            path: path.resolve(__dirname, 'public/scripts'),
            filename: 'bundle.js'
        },
        module: {
            rules: [
                {
                    test: /\.js$/,
                    exclude: /nodule_modules/,
                    use: {
                        loader: 'babel-loader',
                        options: {
                            presets: ['@babel/preset-env'],
                        },
                    },
                },
            ],
        },
        mode: process.env.WEBPACK_MODE || 'development',
        devServer: { 
            static: {
                directory: path.join(__dirname, 'public'),
                publicPath: '/scripts'
            },

            hot: true,
            compress: true,
            port: 3000,
        },
    };
};

我的问题是浏览器不加载任何内容报告:在192.168.0.11/:1Failed to load resource: the server responded with a status of 404 (Not Found)
当我删除publicPath属性时,HTML被呈现,但是找不到bundle.js。

oug3syen

oug3syen1#

我希望这对那些正在寻找答案的人有帮助,在webpack 5上,我们可以将多个路径设置为公共的,只需用列表替换对象即可。

{
      ...
      devServer: {
        static: [
          path.join(__dirname, "build"),
          path.join(__dirname, "scripts"),
        ],
}

相关问题