在 webpack 插件中使用加载器
Using a loader inside a webpack plugin
为了澄清 - 这是一个关于编写 webpack 插件的问题
如何在 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?
});
};
经过一番搜索,我看到文本提取插件使用子编译来使用插件内部的编译器:
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);
});
};
相关文章:
- 如何使用url加载程序在webpack中导入多个图像
- Webpack/Rect:遵循egghead.io教程,但出现错误:您可能需要一个合适的加载程序来处理此文件类型
- index.html使用webpack开发服务器未重新加载
- 如何在不使用require语句的情况下使用webpack加载目录中的所有文件
- Webpack-无法使用$.getScript加载bundle.js
- Webpack:如何将原始HTML加载为React JSX
- 避免将lib依赖项与webpack+handlebas加载程序捆绑在一起
- 无法使用Webpack加载ES6模块
- webpack样式加载器-在react组件上导入不同的样式,而无需重写
- Angular 1.X,webpack和ngtemplate加载程序问题
- 在 webpack 插件中使用加载器
- 在ui路由器中以Webpack块的形式懒惰地加载Angular指令
- 使用react webpack文件加载器提供静态pdf
- webpack:在MacOS上很好,在linux上加载程序错误
- Webpack,较少加载程序-意外的令牌-为什么
- 具有动态查询的Webpack加载器
- 有没有任何方法可以自动加载webpack外部文件
- 当我在package.json中更改了我所需的模块之一时,我如何重新加载webpack ?
- 在基于require.js的项目中加载webpack模块返回null
- Karma无法加载webpack