我现在正在使用webpack v4,试图将webpack-dev-server添加到我的项目中。我在这里面临的问题是,当我运行webpack-dev-server命令时,它在默认浏览器中打开了本地服务器,但不提供已编译的资产,也没有监视文件中的更改。
我还没有实现热模块更换。
这是项目结构
package.json
{
"name": "webpack-starterkit",
"version": "1.0.0",
"description": "webpack starter kit",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack --mode production",
"dev": "webpack --mode development",
"start": "webpack-dev-server --mode development --open"
},
"author": "",
"license": "ISC",
"devDependencies": {
"autoprefixer": "^8.6.3",
"browser-sync": "^2.24.4",
"browser-sync-webpack-plugin": "^2.2.2",
"css-loader": "^0.28.11",
"mini-css-extract-plugin": "^0.4.0",
"node-sass": "^4.9.0",
"postcss-loader": "^2.1.5",
"sass-loader": "^7.0.3",
"style-loader": "^0.21.0",
"webpack": "^4.12.0",
"webpack-cli": "^3.0.8",
"webpack-dev-server": "^3.1.4"
}
}
webpack.config.js
const path = require("path");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const webpack = require("webpack");
module.exports = {
entry: "./app/src/js/index.js",
output: {
filename: "bundle.js",
path: path.resolve(__dirname, "app/dist/js"),
publicPath: "dist/js"
},
devServer: {
contentBase: path.resolve(__dirname, "app/dist"),
port: 3000,
hot: true
},
module: {
rules: [
{
test: /\.(sa|sc|c)ss$/,
use: [
{
loader: "style-loader"
},
{
loader: MiniCssExtractPlugin.loader
},
{
loader: "css-loader"
},
{
loader: "postcss-loader"
},
{
loader: "sass-loader"
}
]
}
]
},
plugins: [
new MiniCssExtractPlugin({
filename: "../css/style.css"
}),
]
};
请帮助我解决问题。
答案 0 :(得分:0)
output.path用于设置应用程序的输出目录。
您尝试在"app/dist/js"
中输出所有内容,而应将其设置为"app/dist"
,然后可以为资产选择特定的输出路径。
要在名为 js 的文件夹中输出javascript捆绑包,可以设置output.filename: "js/bundle.js"
。
对于CSS,您可以在 MiniCssExtractPlugin 选项中设置filename: "css/style.css"
。
在此示例中,无需设置publicPath(资产将从本地服务器的根目录提供)。
重要:如果您运行生产包,然后将应用程序上传到与服务器根目录不同的文件夹中,则需要相应设置publicPath
。
您也不需要任何devServer
选项(默认情况下是http://localhost:8080/
的用户)。
这是解决方案(我将代码简化了一点),devServer
甚至可以处理html文件。
package.json
{
"name": "webpack-test",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"dev": "webpack --mode=development --devtool=false",
"start": "webpack-dev-server --mode=development"
},
"license": "MIT",
"devDependencies": {
"css-loader": "^0.28.11",
"html-webpack-plugin": "^3.2.0",
"mini-css-extract-plugin": "^0.4.0",
"webpack": "^4.12.0",
"webpack-cli": "^3.0.8",
"webpack-dev-server": "^3.1.4"
}
}
webpack.config.js
const path = require("path");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: "./app/src/js/index.js",
output: {
path: path.resolve(__dirname, "app/dist"),
filename: "js/bundle.js"
},
module: {
rules: [
{
test: /\.css$/,
use: [
{ loader: MiniCssExtractPlugin.loader },
{ loader: "css-loader" }
]
}
]
},
plugins: [
new MiniCssExtractPlugin({
filename: "css/style.css"
}),
new HtmlWebpackPlugin({
template: "./app/src/index.html",
title: "Test"
})
]
};
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><%= htmlWebpackPlugin.options.title %></title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
index.js
import "../css/style.css";
console.log("test!");
style.css
body {
background-color: red;
}
文件夹结构