|
1 |
| -var fs = require('fs') |
2 |
| - |
3 |
| -var inlineReg = /<!-- inline-plugin[\w\W\r\n]*?-->/g |
4 |
| -var pathReg = /path="(.*?)"/ |
5 |
| - |
6 |
| -function wrapScript(script) { |
7 |
| - return '<script>' + script + '</script>\n' |
8 |
| -} |
| 1 | +var inline = require('inline-source') |
9 | 2 |
|
10 | 3 | function HtmlWebpackInlinePlugin(options) {
|
11 | 4 | // Setup the plugin instance with options...
|
12 | 5 | this.options = options
|
13 | 6 | }
|
14 | 7 |
|
15 | 8 | HtmlWebpackInlinePlugin.prototype.apply = function(compiler) {
|
16 |
| - compiler.plugin('compilation', function(compilation, options) { |
17 |
| - compilation.plugin('html-webpack-plugin-before-html-processing', function(htmlPluginData, callback) { |
18 |
| - var html = htmlPluginData.html |
19 |
| - var tags = html.match(inlineReg) |
20 |
| - if (tags) { |
21 |
| - tags.forEach(function(tag) { |
22 |
| - var ret = tag.match(pathReg) |
23 |
| - if (ret && ret[1]) { |
24 |
| - var scriptContent = fs.readFileSync(ret[1], 'utf8') |
25 |
| - html = html.replace(tag, wrapScript(scriptContent)) |
26 |
| - } |
27 |
| - }) |
28 |
| - htmlPluginData.html = html |
29 |
| - } |
30 |
| - callback(null, htmlPluginData) |
31 |
| - }); |
| 9 | + var self = this |
| 10 | + compiler.plugin('compilation', function(compilation, options) { |
| 11 | + compilation.plugin('html-webpack-plugin-before-html-processing', function(htmlPluginData, callback) { |
| 12 | + var html = htmlPluginData.html |
| 13 | + inline(html, self.options, function(err, html) { |
| 14 | + if (!err) { |
| 15 | + htmlPluginData.html = html |
| 16 | + } |
| 17 | + callback(null, htmlPluginData) |
| 18 | + }) |
32 | 19 | });
|
| 20 | + }); |
33 | 21 | };
|
34 | 22 |
|
35 | 23 | module.exports = HtmlWebpackInlinePlugin
|
0 commit comments