如何使用开发服务器监视修复错误



有以下项目结构项目结构

我希望服务器从 dist 运行项目,但是当更改为 src 时,它被重新编译并且所有内容都复制到 dist。

My webpack.config.js.

const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
module.exports = {
  mode: 'development',
  entry: './src/scripts/script.js',
  devtool: 'inline-source-map',
  devServer: {
    contentBase: './dist'
  },
  plugins: [
      new CopyWebpackPlugin([
          {
              from: './src/index.html',
              to: '../index.html'
          }
      ]),
      new MiniCssExtractPlugin({
            filename: '../styles/style.css'
      })
  ],
  module: {
      rules: [
          {
              test: /.scss$/,
              use: [
                MiniCssExtractPlugin.loader,    
                'css-loader',
                'sass-loader'
              ]
          }
      ]
  },
  output: {
    filename: 'script.js',
    path: path.resolve(__dirname, 'dist/scripts')
  }
};

内容脚本.js

import './../styles/style.scss';

包.json

{
  "name": "landing",
  "version": "1.0.0",
  "description": "",
  "private": true,
  "scripts": {
  "start": "webpack-dev-server --open",
  "build": "webpack"
},
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "copy-webpack-plugin": "^4.6.0",
    "css-loader": "^2.1.0",
    "import-glob": "^1.5.0",
    "mini-css-extract-plugin": "^0.5.0",
    "node-sass": "^4.11.0",
    "sass-loader": "^7.1.0",
    "webpack": "^4.28.1",
    "webpack-cli": "^3.2.1",
    "webpack-dev-server": "^3.1.14"
  }
}

目前,当更改为 src 时,会发生编译,但这些更改不会落入 dist 文件夹。

如何解决?

你有没有试过watchContentBase选项:

devServer: {
       contentBase: './dist',
       watchContentBase: true,
  },

使用开发服务器时,更改不会转到 dist 文件夹。它存储在缓冲区中。有关更多详细信息,请参阅以下很好的解释: 为什么 webpack-dev-server 实时重新加载不起作用

相关内容

最新更新