-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
executable file
·87 lines (75 loc) · 2.93 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
'use strict';
var assert = require('assert');
function HtmlWebpackExcludeAssetsPlugin (options) {
assert.equal(options, undefined, 'The HtmlWebpackExcludeAssetsPlugin does not accept any options');
this.PluginName = 'HtmlWebpackExcludeAssetsPlugin';
}
HtmlWebpackExcludeAssetsPlugin.prototype.apply = function (compiler) {
if ('hooks' in compiler) {
// v4 approach:
compiler.hooks.compilation.tap(this.PluginName, this.applyCompilation.bind(this));
} else {
// legacy approach:
// Hook into the html-webpack-plugin processing
compiler.plugin('compilation', this.applyCompilation.bind(this));
}
};
HtmlWebpackExcludeAssetsPlugin.prototype.applyCompilation = function applyCompilation (compilation) {
var self = this;
if ('hooks' in compilation) {
// If our target hook is not present, throw a descriptive error
if (!compilation.hooks.htmlWebpackPluginAlterAssetTags) {
throw new Error('The expected HtmlWebpackPlugin hook was not found! Ensure HtmlWebpackPlugin is installed and' +
' was initialized before this plugin.');
}
compilation.hooks.htmlWebpackPluginAlterAssetTags.tapAsync(this.PluginName, registerCb);
} else {
compilation.plugin('html-webpack-plugin-alter-asset-tags', registerCb);
}
function registerCb (htmlPluginData, callback) {
var excludeAssets = htmlPluginData.plugin.options.excludeAssets;
// Skip if the plugin configuration didn't set `excludeAssets`
if (!excludeAssets) {
if (callback) {
return callback(null, htmlPluginData);
} else {
return Promise.resolve(htmlPluginData);
}
}
if (excludeAssets.constructor !== Array) {
excludeAssets = [excludeAssets];
}
// Skip invalid RegExp patterns
var excludePatterns = excludeAssets.filter(function (excludePattern) {
return excludePattern.constructor === RegExp;
});
var result = self.processAssets(excludePatterns, htmlPluginData);
if (callback) {
callback(null, result);
} else {
return Promise.resolve(result);
}
}
};
HtmlWebpackExcludeAssetsPlugin.prototype.isExcluded = function (excludePatterns, assetPath) {
return excludePatterns.filter(function (excludePattern) {
return excludePattern.test(assetPath);
}).length > 0;
};
HtmlWebpackExcludeAssetsPlugin.prototype.processAssets = function (excludePatterns, pluginData) {
var self = this;
var body = [];
var head = [];
pluginData.head.forEach(function (tag) {
if (!tag.attributes || !self.isExcluded(excludePatterns, tag.attributes.src || tag.attributes.href)) {
head.push(tag);
}
});
pluginData.body.forEach(function (tag) {
if (!tag.attributes || !self.isExcluded(excludePatterns, tag.attributes.src || tag.attributes.href)) {
body.push(tag);
}
});
return { head: head, body: body, plugin: pluginData.plugin, chunks: pluginData.chunks, outputName: pluginData.outputName };
};
module.exports = HtmlWebpackExcludeAssetsPlugin;