Webpack 网页包插件,何时构建虚拟模块?

Webpack 网页包插件,何时构建虚拟模块?,webpack,Webpack,我正在制作一个webpack插件,它从javascript文件中提取内容,然后注入一个由该内容生成的新模块 我通过以下方式提取内容: var content = []; compiler.parser.plugin("call " + this.functionName, function (expr) { //example of extraction: content.push(this.evaluateExpression(expr.arguments[0]).string

我正在制作一个webpack插件,它从javascript文件中提取内容,然后注入一个由该内容生成的新模块

我通过以下方式提取内容:

var content = [];
compiler.parser.plugin("call " + this.functionName, function (expr) {
    //example of extraction:
    content.push(this.evaluateExpression(expr.arguments[0]).string);
});
var needRebuild = true, source = '';
var buildPromise = Promise.resolve();
var listModule = new Module(); //TODO refactor and extend Module
listModule.identifier = listModule.readableIdentifier = function () {
    return listModuleName;
};
listModule.needRebuild = function () {
    return needRebuild;
};
listModule.build = function (options, compilation, resolver, fs, callback) {
    var that = this;
    //Here is my problem. For now I try to delay the build with a promise.
    buildPromise.then(function () {
        //example of source generation
        source = "module.exports=" + JSON.stringify(content, null, '  ') + ';';
        needRebuild = false;
        callback();
    });
};
listModule.source = function () {
    if (this.useSourceMap) {
        return new OriginalSource(source, this.identifier());
    } else {
        return new RawSource(source);
    }
};
listModule.size = function () {
    return source.length || -1;
};


compiler.plugin("normal-module-factory", function (normalModuleFactory) {
    normalModuleFactory.plugin("resolver", function (next) {
        return function (data, callback) {
            if (data.request === listModuleName) {
                return callback(null, listModule);
            }
            return next(data, callback);
        }
    });
});
并以这种方式注入新模块:

var content = [];
compiler.parser.plugin("call " + this.functionName, function (expr) {
    //example of extraction:
    content.push(this.evaluateExpression(expr.arguments[0]).string);
});
var needRebuild = true, source = '';
var buildPromise = Promise.resolve();
var listModule = new Module(); //TODO refactor and extend Module
listModule.identifier = listModule.readableIdentifier = function () {
    return listModuleName;
};
listModule.needRebuild = function () {
    return needRebuild;
};
listModule.build = function (options, compilation, resolver, fs, callback) {
    var that = this;
    //Here is my problem. For now I try to delay the build with a promise.
    buildPromise.then(function () {
        //example of source generation
        source = "module.exports=" + JSON.stringify(content, null, '  ') + ';';
        needRebuild = false;
        callback();
    });
};
listModule.source = function () {
    if (this.useSourceMap) {
        return new OriginalSource(source, this.identifier());
    } else {
        return new RawSource(source);
    }
};
listModule.size = function () {
    return source.length || -1;
};


compiler.plugin("normal-module-factory", function (normalModuleFactory) {
    normalModuleFactory.plugin("resolver", function (next) {
        return function (data, callback) {
            if (data.request === listModuleName) {
                return callback(null, listModule);
            }
            return next(data, callback);
        }
    });
});
所以我的问题是,在我解析所有内容之前,虚拟插件构建得太快了


我试图用一个承诺来延迟构建,并在例如,
compiler.plugin(“compilation”)中解决这个承诺,
。但这似乎不起作用。我找不到合适的地方来解决承诺/进行构建。

我不确定它是否能解决您的问题-您可能在发布此问题后继续前进-但我构建了一个类似的插件。