Compiler.hooks.normalModuleFactory.tap无法读取未定义的属性“ tap”

时间:2018-12-24 19:08:06

标签: javascript webpack

我正在尝试使用here中的该插件,这给了我一个错误。为什么!

               compiler.hooks.normalModuleFactory.tap(
                                                   ^

TypeError: Cannot read property 'tap' of undefined

这是插件

/*
    MIT License http://www.opensource.org/licenses/mit-license.php
    Author Tobias Koppers @sokra
*/
"use strict";

const path = require("path");

class NormalModuleReplacementPlugin {
    constructor(resourceRegExp, newResource) {
        this.resourceRegExp = resourceRegExp;
        this.newResource = newResource;
    }

    apply(compiler) {
        const resourceRegExp = this.resourceRegExp;
        const newResource = this.newResource;
        compiler.hooks.normalModuleFactory.tap(
            "NormalModuleReplacementPlugin",
            nmf => {
                nmf.hooks.beforeResolve.tap("NormalModuleReplacementPlugin", result => {
                    if (!result) return;
                    if (resourceRegExp.test(result.request)) {
                        if (typeof newResource === "function") {
                            newResource(result);
                        } else {
                            result.request = newResource;
                        }
                    }
                    return result;
                });
                nmf.hooks.afterResolve.tap("NormalModuleReplacementPlugin", result => {
                    if (!result) return;
                    if (resourceRegExp.test(result.resource)) {
                        if (typeof newResource === "function") {
                            newResource(result);
                        } else {
                            result.resource = path.resolve(
                                path.dirname(result.resource),
                                newResource
                            );
                        }
                    }
                    return result;
                });
            }
        );
    }
}

module.exports = NormalModuleReplacementPlugin;

0 个答案:

没有答案