forked from joaner/html-webpack-preconnect-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
49 lines (44 loc) · 1.46 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'use strict'
var assert = require('assert')
/**
* html webpack preconnect plugin
* @class
*/
function HtmlWebpackPreconnectPlugin(options) {
assert.equal(options, undefined, 'The ResourceHintWebpackPlugin does not accept any options')
}
const addPreconnectLinks = function (htmlPluginData, callback) {
var origins = htmlPluginData.plugin.options.preconnect;
assert.equal(origins instanceof Array, true, new TypeError('origins need array'));
origins.forEach(function (origin) {
// webpack config may contain quotos, remove that
var href = origin.replace(/['"]+/g, '');
var tag = {
tagName: 'link',
selfClosingTag: false,
attributes: {
rel: 'preconnect',
href: href,
crossorigin: ''
}
};
htmlPluginData.head.push(tag);
});
callback(null, htmlPluginData);
}
HtmlWebpackPreconnectPlugin.prototype.apply = function (compiler) {
// Webpack 4
if (compiler.hooks) {
compiler.hooks.compilation.tap('htmlWebpackPreconnectPlugin', function(compilation) {
// Hook into the html-webpack-plugin processing
compilation.hooks.htmlWebpackPluginAlterAssetTags.tapAsync('htmlWebpackPreconnectPlugin', addPreconnectLinks)
})
// Webpack 3
} else {
compiler.plugin('compilation', function (compilation) {
// Hook into the html-webpack-plugin processing
compilation.plugin('html-webpack-plugin-alter-asset-tags', addPreconnectLinks)
})
}
}
module.exports = HtmlWebpackPreconnectPlugin