【问题标题】:webpack dev server not building and reloading index.htmlwebpack 开发服务器未构建和重新加载 index.html
【发布时间】:2018-12-02 12:53:22
【问题描述】:

我现在正在使用 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"
    }),
  ]
};

请帮我解决问题。

【问题讨论】:

  • @ippi,我想用 webpack-dev-server 实现开发环境。我使用生产模式构建它正在编译并生成工作资产到“ dist ”文件夹。我读过的大多数关于 webpack-devserver 的教程都是自动编译资产并监视更改,但在我的情况下并没有发生这种情况。有什么想法吗??
  • 我只是猜测,尝试更改为publicPath: "/js/"
  • @VitaliySmolyakov,不工作

标签: webpack webpack-dev-server webpack-2 webpack-hmr webpack-4


【解决方案1】:

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;
}

文件夹结构

【讨论】:

    猜你喜欢
    • 2019-05-22
    • 2017-05-03
    • 2021-12-15
    • 1970-01-01
    • 2020-12-25
    • 1970-01-01
    • 2017-04-15
    • 2017-06-07
    • 1970-01-01
    相关资源
    最近更新 更多