Webpack开发服务器无法获取

问题描述 投票:0回答:1

我正在使用Webpack-4。 当前行为是,在运行webpack-dev-server时,/ build下的文件完全不会更新,并且显示文件目录。

如果我删除/ build下的文件,则webpack-dev-server给出的无法获取/。 我认为,它应该从内存中加载它们。

const HtmlWebPackPlugin = require("html-webpack-plugin");
const htmlPlugin = new HtmlWebPackPlugin({
   template: "./src/index.html",
   filename: "./index.html"
});

const path = require('path');

module.exports = {
output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'build/'),
},

module: {
    rules: [{
            test: /\.(js|jsx)$/,
            exclude: /node_modules/,
            use: {
                loader: "babel-loader",
                options: {
                    presets: ["env","react"]
                }
            }
        },
        { 
            test: /\.html$/,
            use: [{
                loader: "html-loader",
                options: {
                    minimize: true
                }
            }]
        }
    ]
},
plugins: [
    htmlPlugin
],
devServer: {       
    contentBase: "./build/",
    port: 5555
}
}
webpack webpack-dev-server
1个回答
4
投票

一些提示可以帮助我理解和调试本地webpack-dev-server配置:

  1. 要查看webpack-dev-server在内存中提供文件的位置,请在浏览器中键入http://localhost:{yourport}/webpack-dev-server 然后,您可以单击文件之一(链接),它将向您显示该文件的发送路径以及文件的内容。
  2. 当启动webpack-dev-server时(即使用npm start),它会在控制台中根据webpack.config.js文件向您显示该服务器在其中提供内容。 (有关详细说明,请参见下文)
  3. 如果要进行热重载(我不明白为什么不这样做),则需要在命令行中(在package.json启动脚本中)而不是在配置文件中进行指定。 由于某种原因,将其放入配置文件中无法正常工作。 因此,使用类似:

的package.json

"scripts": {
    "start": "webpack-dev-server --config webpack.config.js --hot --inline"
},

webpack.config.js配置

...
output: {
    filename: '[name].bundle.js',
    path: path.join(__dirname, 'public', 'scripts'),
},
...
devServer: {
    contentBase: path.join(__dirname, "public"),
    publicPath: 'http://localhost:8080/scripts/',
    port: 8080
},
...

产量

 i 「wds」: Project is running at http://localhost:8080/    
 i 「wds」: webpack output is served from http://localhost:8080/scripts/
 i 「wds」: Content not from webpack is served from C:\Workspace\WebSite\public

在输出的第2行,请注意,由于配置中的contentBasehttp://localhost:8080/scripts/实际上指向磁盘上的C:\\Workspace\\WebSite\\public\\scripts 。 (这是webpack还将放置其文件的地方:) !!!)

publicPath配置上,尾随反斜杠很重要。

© www.soinside.com 2019 - 2024. All rights reserved.