Using a loader inside a webpack plugin

后端 未结 1 1825
予麋鹿
予麋鹿 2020-12-16 01:19

For clarification - this is a question about writing a webpack plugin

How do you use the webpack require inside a webpack plugin?

MyPlug         


        
相关标签:
1条回答
  • 2020-12-16 01:22

    After some searching I saw that the text-extract plugin uses a child compilation to use a compiler inside the plugin:

    https://github.com/SanderSpies/extract-text-webpack-plugin/blob/be6484f00c46799280b9ec28946faf935cb9ae63/loader.js#L65

    In the following example I am using the my-loader to load and compile the input.js file:

    MyPlugin.prototype.apply = function(compiler) {
      compiler.plugin('make', function(compilation, callback) {
        var outputOptions = {
          filename: 'output.js',
          publicPath: compilation.outputOptions.publicPath
        };
        var childCompiler = compilation.createChildCompiler('MyPluginCompilation', outputOptions);
        childCompiler.apply(new NodeTemplatePlugin(outputOptions));
        childCompiler.apply(new LibraryTemplatePlugin('result', 'var'));
        childCompiler.apply(new NodeTargetPlugin());
        childCompiler.apply(new SingleEntryPlugin(this.context, 'my-loader!input.js'));
        childCompiler.runAsChild(callback);
      });
    };
    
    0 讨论(0)
提交回复
热议问题