Skip to content

Commit 1250492

Browse files
authored
Make plugin compatible with the HTML Webpack Plugin version 4
1 parent 77c0ac5 commit 1250492

File tree

1 file changed

+18
-36
lines changed

1 file changed

+18
-36
lines changed

index.js

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,19 @@ var path = require('path');
55
var slash = require('slash');
66
var sourceMapUrl = require('source-map-url');
77

8-
function HtmlWebpackInlineSourcePlugin (options) {
8+
function HtmlWebpackInlineSourcePlugin (htmlWebpackPlugin, options) {
9+
this.htmlWebpackPlugin = htmlWebpackPlugin;
910
assert.equal(options, undefined, 'The HtmlWebpackInlineSourcePlugin does not accept any options');
1011
}
1112

1213
HtmlWebpackInlineSourcePlugin.prototype.apply = function (compiler) {
1314
var self = this;
1415

1516
// Hook into the html-webpack-plugin processing
16-
17-
(compiler.hooks
18-
? compiler.hooks.compilation.tap.bind(compiler.hooks.compilation, 'html-webpack-inline-source-plugin')
19-
: compiler.plugin.bind(compiler, 'compilation'))(function (compilation) {
20-
(compilation.hooks
21-
? compilation.hooks.htmlWebpackPluginAlterAssetTags.tapAsync.bind(compilation.hooks.htmlWebpackPluginAlterAssetTags, 'html-webpack-inline-source-plugin')
22-
: compilation.plugin.bind(compilation, 'html-webpack-plugin-alter-asset-tags'))(function (htmlPluginData, callback) {
17+
compiler.hooks.compilation.tap('html-webpack-inline-source-plugin', compilation => {
18+
self.htmlWebpackPlugin
19+
.getHooks(compilation)
20+
.alterAssetTagGroups.tapAsync('html-webpack-inline-source-plugin', (htmlPluginData, callback) => {
2321
if (!htmlPluginData.plugin.options.inlineSource) {
2422
return callback(null, htmlPluginData);
2523
}
@@ -29,28 +27,27 @@ HtmlWebpackInlineSourcePlugin.prototype.apply = function (compiler) {
2927
var result = self.processTags(compilation, regexStr, htmlPluginData);
3028

3129
callback(null, result);
32-
});
33-
});
30+
})
31+
})
3432
};
3533

3634
HtmlWebpackInlineSourcePlugin.prototype.processTags = function (compilation, regexStr, pluginData) {
3735
var self = this;
3836

39-
var body = [];
40-
var head = [];
37+
var bodyTags = [];
38+
var headTags = [];
4139

4240
var regex = new RegExp(regexStr);
43-
var filename = pluginData.plugin.options.filename;
4441

45-
pluginData.head.forEach(function (tag) {
46-
head.push(self.processTag(compilation, regex, tag, filename));
42+
pluginData.headTags.forEach(function (tag) {
43+
headTags.push(self.processTag(compilation, regex, tag));
4744
});
4845

49-
pluginData.body.forEach(function (tag) {
50-
body.push(self.processTag(compilation, regex, tag, filename));
46+
pluginData.bodyTags.forEach(function (tag) {
47+
bodyTags.push(self.processTag(compilation, regex, tag));
5148
});
5249

53-
return { head: head, body: body, plugin: pluginData.plugin, chunks: pluginData.chunks, outputName: pluginData.outputName };
50+
return { headTags: headTags, bodyTags: bodyTags, plugin: pluginData.plugin, outputName: pluginData.outputName };
5451
};
5552

5653
HtmlWebpackInlineSourcePlugin.prototype.resolveSourceMaps = function (compilation, assetName, asset) {
@@ -84,11 +81,11 @@ HtmlWebpackInlineSourcePlugin.prototype.resolveSourceMaps = function (compilatio
8481
});
8582
};
8683

87-
HtmlWebpackInlineSourcePlugin.prototype.processTag = function (compilation, regex, tag, filename) {
84+
HtmlWebpackInlineSourcePlugin.prototype.processTag = function (compilation, regex, tag) {
8885
var assetUrl;
8986

9087
// inline js
91-
if (tag.tagName === 'script' && tag.attributes && regex.test(tag.attributes.src)) {
88+
if (tag.tagName === 'script' && regex.test(tag.attributes.src)) {
9289
assetUrl = tag.attributes.src;
9390
tag = {
9491
tagName: 'script',
@@ -113,28 +110,13 @@ HtmlWebpackInlineSourcePlugin.prototype.processTag = function (compilation, rege
113110
if (assetUrl) {
114111
// Strip public URL prefix from asset URL to get Webpack asset name
115112
var publicUrlPrefix = compilation.outputOptions.publicPath || '';
116-
// if filename is in subfolder, assetUrl should be prepended folder path
117-
if (path.basename(filename) !== filename) {
118-
assetUrl = path.dirname(filename) + '/' + assetUrl;
119-
}
120113
var assetName = path.posix.relative(publicUrlPrefix, assetUrl);
121-
var asset = getAssetByName(compilation.assets, assetName);
114+
var asset = compilation.assets[assetName];
122115
var updatedSource = this.resolveSourceMaps(compilation, assetName, asset);
123116
tag.innerHTML = (tag.tagName === 'script') ? updatedSource.replace(/(<)(\/script>)/g, '\\x3C$2') : updatedSource;
124117
}
125118

126119
return tag;
127120
};
128121

129-
function getAssetByName (assests, assetName) {
130-
for (var key in assests) {
131-
if (assests.hasOwnProperty(key)) {
132-
var processedKey = path.posix.relative('', key);
133-
if (processedKey === assetName) {
134-
return assests[key];
135-
}
136-
}
137-
}
138-
}
139-
140122
module.exports = HtmlWebpackInlineSourcePlugin;

0 commit comments

Comments
 (0)