【问题标题】:Webpack/Babel/Sass compiling into foldersWebpack/Babel/Sass 编译成文件夹
【发布时间】:2018-12-29 15:16:01
【问题描述】:

想知道你是否可以帮忙 -

Webpack 让我抓狂。

我有一个带有 js、img 和 css 子文件夹的 dist 文件夹。 我希望 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"
  }
}

【问题讨论】:

    标签: webpack sass webpack-dev-server


    【解决方案1】:

    为了热重载工作

    在你的 webpack 文件中添加这个

    const webpack = require('webpack');

    更新

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

     devServer: {
            contentBase: './dist'
            hot: true,   //this is important
        },
    

    然后

    new webpack.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官网Webpack HMR有详细说明

    【讨论】:

      猜你喜欢
      • 2016-07-26
      • 1970-01-01
      • 2018-12-27
      • 2017-10-27
      • 2020-04-06
      • 1970-01-01
      • 1970-01-01
      • 2019-04-17
      • 1970-01-01
      相关资源
      最近更新 更多