Webpack:如何设置webpack-dev-server和Hot Reload

时间:2017-05-17 19:11:29

标签: webpack webpack-dev-server webpack-2 webpack-style-loader webpack-hmr

运行npm run watch命令时,我希望热重新加载。 (我希望它在使用 vue-cli 时工作 - 当您运行npm run dev命令时:https://vue-loader.vuejs.org/en/features/hot-reload.html)。

所以,我已经安装了 webpack-dev-server ,这就是我尝试过的:

的package.json

{
  "name": "webpack-learning",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "dev": "cross-env NODE_ENV=development webpack",
    "watch": "cross-env NODE_ENV=development webpack-dev-server --open --hot",
    "build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.24.1",
    "babel-loader": "^7.0.0",
    "babel-preset-es2015": "^6.24.1",
    "cross-env": "^5.0.0",
    "css-loader": "^0.28.1",
    "extract-text-webpack-plugin": "^2.1.0",
    "file-loader": "^0.11.1",
    "node-sass": "^4.5.2",
    "purifycss-webpack": "^0.6.2",
    "sass-loader": "^6.0.5",
    "style-loader": "^0.17.0",
    "webpack": "^2.4.1",
    "webpack-dev-server": "^2.4.5"
  }
}

如果你查看"watch"脚本 - 它使用 webpack-dev-server ,它也有--open --hot - 这与 vue-相同cli的package.json

另外,这是我的 webpack.config.js

var webpack = require('webpack');
var path = require('path');
const glob = require('glob');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const PurifyCSSPlugin = require('purifycss-webpack');
var inProduction = (process.env.NODE_ENV === 'production');

module.exports = {
    entry: {
        app: [
            './src/js/main.js',
            './src/sass/app.scss'
        ]
    },
    output: {
        path: path.resolve(__dirname, './dist'),
        filename: '[name].js'
    },

    module: {
        rules: [
            {
                test: /\.s[ac]ss$/,
                use: ExtractTextPlugin.extract({
                    use: ['css-loader', 'sass-loader'],
                    fallback: 'style-loader'
                })
            },
            {
                test:  /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2)$/,
                loader: 'file-loader',
                options: {
                    name: '[name].[ext]?[hash]'
                }
            },
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader']
            },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: "babel-loader"
            }
        ]
    },

    plugins: [
        new ExtractTextPlugin('[name].css'),

        new PurifyCSSPlugin({
            paths: glob.sync(path.join(__dirname, 'index.html')),
            minimize: inProduction
        }),

        new webpack.LoaderOptionsPlugin({
            minimize: inProduction
        })
    ],

    devtool: '#eval-source-map'
};

if (inProduction) {
    module.exports.devtool = '#source-map';

    module.exports.plugins.push(
        new webpack.optimize.UglifyJsPlugin({
            sourceMap: true,
            compress: {
                warnings: false
            }
        })
    );
}

请注意,我使用的是 ExtractTextPlugin (我有一个单独的CSS文件),文件加载器插件(用于网址/图片)等。

当我运行npm run watch命令时 - 没有Hot Relaoding,没有任何反应。没有错误,但它没有做任何事情,它只在浏览器中打开 index.html http://localhost:8080)。

未检测到任何更改。例如,如果我编辑:

  • app.scss
  • 的index.html

...未加载这些更改。

它甚至没有编译任何东西(如果我记得很清楚,如果你删除了 dist 目录,那么在 vue-cli 中 - 当你运行npm run dev时 - 它将再次重新创建 dist 目录,它将把编译的资产放在那里......但是在这里,如果你删除了已编译的资产 - 它将不会编译/创建新的资产。) / p>

1 个答案:

答案 0 :(得分:1)

我有一个模板项目,我曾经用它来引导我自己的项目,它有热重装工作。它使用gulp来运行webpack-dev-server,但你可以查看并将相关的位添加回你自己的webpack.conf.js:

https://github.com/vhogemann/angular2-starter/blob/master/config/gulp.dev-server.js