gpt4 book ai didi

webpack 开发服务器不构建和重新加载 index.html

转载 作者:行者123 更新时间:2023-12-05 07:32:50 25 4
gpt4 key购买 nike

我现在正在使用 webpack v4,试图将 webpack-dev-server 添加到我的项目中。我在这里面临的问题是,当我运行 webpack-dev-server 命令时,它会在我的默认浏览器中打开一个本地服务器,但不会提供已编译的 Assets ,也不会监视文件的变化。

我还没有实现模块热替换。

这是项目结构

enter image description here

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"
}),
]
};

请帮我解决这个问题。

最佳答案

output.path用于设置应用程序的输出目录。

您尝试输出"app/dist/js"中的所有内容,您应该将其设置为"app/dist",然后您可以选择特定的输出您 Assets 的路径。

要在名为 js 的文件夹中输出 javascript 包,您可以设置 output.filename: "js/bundle.js"

对于 css,您可以在 MiniCssExtractPlugin 选项中设置 filename: "css/style.css"

无需设置publicPath在此示例中( Assets 将从本地主机服务器的根目录提供)。

重要提示:如果您运行生产包,然后将您的应用程序上传到与服务器根目录不同的文件夹中,您需要相应地设置 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!");

样式.css

body {
background-color: red;
}

文件夹结构

folder-structure

关于webpack 开发服务器不构建和重新加载 index.html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50998195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com