Skip to content

Commit b00720d

Browse files
committed
use inline-source
1 parent 23d5def commit b00720d

File tree

2 files changed

+16
-25
lines changed

2 files changed

+16
-25
lines changed

index.js

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,23 @@
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')
92

103
function HtmlWebpackInlinePlugin(options) {
114
// Setup the plugin instance with options...
125
this.options = options
136
}
147

158
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+
})
3219
});
20+
});
3321
};
3422

3523
module.exports = HtmlWebpackInlinePlugin

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@
77
"test": "echo \"Error: no test specified\" && exit 1"
88
},
99
"author": "storkyeh",
10-
"license": "MIT"
10+
"license": "MIT",
11+
"dependencies": {
12+
"inline-source": "^5.2.4"
13+
}
1114
}

0 commit comments

Comments
 (0)