2015-03-11 13 views
7

Để làm rõ - đây là một câu hỏi về cách viết một webpack pluginSử dụng một bộ nạp bên trong một webpack cắm

Làm thế nào để bạn sử dụng webpack require bên trong một plugin webpack?

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

Trả lời

8

Sau khi một số tìm kiếm tôi thấy rằng các plugin text-chiết xuất sử dụng một bộ sưu tập trẻ sử dụng một trình biên dịch bên trong plugin:

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

Trong ví dụ sau Tôi đang sử dụng my-loader để tải và biên dịch tệp 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); 
    }); 
}; 
+1

Điều này dường như không thực sự đẩy nội dung của input.js vào gói cuối cùng. Tui bỏ lỡ điều gì vậy? –

Các vấn đề liên quan