Webpack/Babel/Sass编译到文件夹中

Webpack/Babel/Sass编译到文件夹中,webpack,sass,webpack-dev-server,Webpack,Sass,Webpack Dev Server,不知你是否能帮忙- 网页使我毛骨悚然 我有一个dist文件夹,里面有js、img和css子文件夹。 我希望webpack编译我的: src/js/index.js到dist/js 并将src/main.scss转换为dist/css。那我就要 webpack dev server监视dist文件,因此如果我在src中进行更改,它将在我工作时立即显示 这听起来很简单,但我花了一天中最好的时间来实现它 这是我的配置文件的外观: var path = require('path'); const Mi

不知你是否能帮忙-

网页使我毛骨悚然

我有一个dist文件夹,里面有js、img和css子文件夹。 我希望webpack编译我的: src/js/index.js到dist/js 并将src/main.scss转换为dist/css。那我就要 webpack dev server监视dist文件,因此如果我在src中进行更改,它将在我工作时立即显示

这听起来很简单,但我花了一天中最好的时间来实现它

这是我的配置文件的外观:

var path = require('path');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    entry: ['babel-polyfill', './src/js/index.js'], //this is the correct entry point, and I have imported my main.scss into this file
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'js/main.js'
// it's already going to the dist folder, but is this path correct? I've tried /js/main.js - doesn't work
    },
    devServer: {
        contentBase: './dist'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                use: ['babel-loader']
            },
            {
                test: /\.scss$/,
                use:  [ MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader']
            }
        ]
    },
    plugins: [
        new MiniCssExtractPlugin({
            filename: '/css/styles.css',
          }),
        new HtmlWebpackPlugin({
            inject: false,
            hash: true,
            template: './src/index.html',
            filename: 'index.html'
          })
    ]

}
package.json:

{
  "name": "webpack-sass",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "webpack --mode development",
    "build": "webpack --mode production",
    "start": "webpack-dev-server --mode development --open"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",
    "babel-preset-env": "^1.7.0",
    "css-loader": "^1.0.0",
    "html-webpack-plugin": "^3.2.0",
    "mini-css-extract-plugin": "^0.4.1",
    "node-sass": "^4.9.2",
    "sass-loader": "^7.0.3",
    "style-loader": "^0.21.0",
    "webpack": "^4.16.1",
    "webpack-cli": "^3.1.0",
    "webpack-dev-server": "^3.1.4"
  },
  "dependencies": {
    "babel-polyfill": "^6.26.0"
  }
}

用于热重新加载工作

将此添加到您的网页包文件中

const webpack=require('webpack')

更新

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

然后

新网页包.HotModuleReplacementPlugin()
添加到插件部分

plugins: [
        new MiniCssExtractPlugin({
            filename: '/css/styles.css',
          }),
        new HtmlWebpackPlugin({
            inject: false,
            hash: true,
            template: './src/index.html',
            filename: 'index.html'
          }),
        new webpack.HotModuleReplacementPlugin()
    ]
然后在
index.js中

if (module.hot) {
   module.hot.accept('./yourImportedComponent.js', function() {

   })
 }
以上步骤将在webpack网站上详细说明

if (module.hot) {
   module.hot.accept('./yourImportedComponent.js', function() {

   })
 }