我正在使用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-dev-server配置:
http://localhost:{yourport}/webpack-dev-server
。 然后,您可以单击文件之一(链接),它将向您显示该文件的发送路径以及文件的内容。 webpack.config.js
文件向您显示该服务器在其中提供内容。 (有关详细说明,请参见下文) 的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行,请注意,由于配置中的contentBase
, http://localhost:8080/scripts/
实际上指向磁盘上的C:\\Workspace\\WebSite\\public\\scripts
。 (这是webpack还将放置其文件的地方:) !!!)
在publicPath
配置上,尾随反斜杠很重要。