【问题标题】:Using a loader inside a webpack plugin在 webpack 插件中使用加载器
【发布时间】:2015-03-11 13:15:54
【问题描述】:

为了澄清 - 这是一个关于写webpack plugin的问题

如何在 webpack 插件中使用 webpack require

MyPlugin.prototype.apply = function(compiler) {
  var self = this;
  compiler.plugin('emit', function(compilation, callback) {
     var file = 'example.css';
     compilation.fileDependencies.push(file);
     var loaderResult = require('style!css!' + file); // <-- is there any way to make this possible?
  });
};

【问题讨论】:

    标签: javascript webpack


    【解决方案1】:

    经过一番搜索,我看到 text-extract 插件使用子编译来使用插件内部的编译器:

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

    在以下示例中,我使用my-loader 来加载和编译input.js 文件:

    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);
      });
    };
    

    【讨论】:

    • 这实际上并没有将 input.js 的内容推送到最终包中。我错过了什么吗?
    • 而你实际上并没有得到加载器返回的数据:\
    猜你喜欢
    • 2017-06-26
    • 2023-04-09
    • 2019-03-31
    • 2017-10-02
    • 2017-06-02
    • 2018-12-26
    • 2019-02-21
    • 2019-01-02
    • 2016-09-23
    相关资源
    最近更新 更多