-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (50 loc) · 2.17 KB
/
index.js
File metadata and controls
62 lines (50 loc) · 2.17 KB
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
var path = require('path');
var fs = require('fs');
var _ = require('lodash');
var RSVP = require('rsvp');
function InjectPreloader(options) {
this.options = _.extend({
browserIndicatorPlaceholder: '<!-- build:injectBrowserIndicator -->',
loadIndicatorPlaceholder: '<!-- build:injectLoadIndicator -->',
browserUpdateScriptPlaceholder: '<!-- build:injectBrowserUpdateScript -->',
browserIndicatorTemplate: path.join(__dirname, 'lib/browserIndicator.html'),
loadIndicatorTemplate: path.join(__dirname, 'lib/loadIndicator.html'),
browserCheckScriptTemplate: path.join(__dirname, 'lib/browserCheckScript.html')
}, options);
}
InjectPreloader.prototype.apply = function (compiler) {
var self = this;
compiler.hooks.compilation.tap('InjectPreloader', (compilation) => {
compilation.hooks.htmlWebpackPluginBeforeHtmlProcessing.tapAsync(
'InjectPreloader',
(htmlPluginData, callback) => {
var loadFile = function (path) {
return new RSVP.Promise(function (resolve, reject) {
fs.readFile (path, 'utf8', function (error, data) {
if (error) {
reject(error);
}
resolve(data);
});
});
};
var promises = {
browserIndicator: loadFile(self.options.browserIndicatorTemplate),
loadIndicator: loadFile(self.options.loadIndicatorTemplate),
browserScript: loadFile(self.options.browserCheckScriptTemplate)
};
RSVP.hash(promises).then(function(files) {
htmlPluginData.html = htmlPluginData.html.replace(self.options.browserIndicatorPlaceholder, files.browserIndicator);
htmlPluginData.html = htmlPluginData.html.replace(self.options.loadIndicatorPlaceholder, files.loadIndicator);
htmlPluginData.html = htmlPluginData.html.replace(self.options.browserUpdateScriptPlaceholder, files.browserScript);
callback(null, htmlPluginData);
}).catch(function(reason) {
reason.plugin = 'preloader-html-webpack-plugin';
console.error(reason);
callback(reason, htmlPluginData);
});
}
);
});
};
module.exports = InjectPreloader;