【问题标题】:How to run clean-webpack-plugin only when Flow compilation success?仅当 Flow 编译成功时如何运行 clean-webpack-plugin?
【发布时间】:2017-05-11 21:21:09
【问题描述】:

我在构建之前使用clean-webpack-plugin 删除/dist 文件夹,并且仅在Flow 成功的编译阶段

我遇到的问题是即使 Flow 抛出错误,clean 插件也会运行。

是否可以解决这个问题(我的意思是当出现编译错误时,不要删除“dist”文件夹)?

./src/app.js

function init(a: number) {
    //...
}
init("2");

./webpack.config.json

const path = require("path");
const webpack = require("webpack");
const flowTypeLoaderPlugin = require("flowtype-loader/plugin");
const CleanWebpackPlugin = require("clean-webpack-plugin");

module.exports = [{
    entry : "./src/app.js",
    output : {
        filename : "build.js",
        path : path.resolve(__dirname, "dist")
    },
    module : {
        loaders : [
            /*{
                test: /\.js$/,
                enforce: "pre",
                use: CleanWebpackPlugin(["dist"], {
                    root: "/dist",
                    verbose: true,
                    dry: true,
                    "watch": true
                    })
            },*/
            {
                test : /\.js$/,
                loader : "flowtype-loader",
                enforce : "pre",
                exclude : /node_modules/
            },
            {
                test : /\.jsx?$/,
                enforce : "pre",
                loader : "remove-flow-types-loader",
                include : path.join(__dirname, "src")
            }
        ]
    },
    plugins : [
        new flowTypeLoaderPlugin({cwd : path.resolve(__dirname, "src"), failOnError : true}),

        //Empty "dist" folder
        new CleanWebpackPlugin(["dist"], {
            root: "/",
            verbose: true,
            dry: true,
            "watch": true
        })
    ]
}];

【问题讨论】:

    标签: webpack webpack-2


    【解决方案1】:

    解决此问题的一种方法是通过添加“should-emit”条件来修改 CleanWebpackPlugin 的代码。它对我有用,但我不确定它是否适用于所有可能的情况:

    CleanWebpackPlugin.prototype.apply = function(compiler) {
      var _this = this;
      if (compiler === undefined) {
        return clean.call(_this);
      } else {
        compiler.plugin("should-emit", compilation => {
          if (_this.options.watch) {
            compiler.plugin("compile", function(params) {
              clean.call(_this);
            });
          } else {
            return clean.call(_this);
          }
        });
      }
    };
    

    这也需要你在你的 webpack 配置文件中使用另一个插件:NoEmitOnErrorsPlugin

    【讨论】:

    • 谢谢,我会检查一下,但我可以从哪里安装NoEmitOnErrorsPlugin?我在 npmjs 上找不到它npmjs.com/search?q=NoEmitOnErrorsPlugin
    • 我认为它与 webpack 捆绑在一起。这是配置文件中的一个示例:plugins: [ new webpack.NoEmitOnErrorsPlugin(), ...
    猜你喜欢
    • 2019-11-10
    • 1970-01-01
    • 2019-01-29
    • 2019-06-17
    • 2012-03-11
    • 2012-06-02
    • 2016-12-10
    • 2018-04-17
    • 2020-08-11
    相关资源
    最近更新 更多