如何使用开发服务器监视程序解决错误?

时间:2019-01-10 06:58:39

标签: webpack webpack-dev-server

有以下项目结构 project structure

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

我的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')
  }
};

内容script.js

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

package.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文件夹中。

如何修复?

1 个答案:

答案 0 :(得分:0)

您是否尝试过watchContentBase选项:

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

使用dev-server时,所做的更改不会进入dist文件夹。它存储在缓冲区中。有关更多详细信息,请参见以下详细说明:Why webpack-dev-server Live-Reload Is Not Working