-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (47 loc) · 1.4 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
/**
* A gulp task to inject live reload into JS files
*
* @author Matheus Giovani
* @since 06/04/2021
*/
const through = require("through2");
/**
* @param {{
* port?: number,
* host?: string,
* protocol?: "http" | "https",
* inject?: boolean
* }} config The configuration data
* @returns
*/
module.exports = (config) => {
// Setup the defaults
config = Object.assign({
port: 35729,
host: "localhost",
protocol: "http",
inject: true
}, config);
return through.obj(async (file, enc, cb) => {
// Check if it's not injecting
if (!config.inject) {
// Ignore it
return cb(null, file);
}
// Convert the file contents to a string
let content = file.contents.toString("utf8");
const livereload = /*javascript*/`
// Injected by @ofertorio/modules/gulp/inject-livereload
var script = document.createElement("script");
script.src = "${config.protocol}://${config.host}:${config.port}/livereload.js?port=${config.port}";
script.type = "text/javascript";
script.async = "true";
script.defer = "true";
document.head.appendChild(script);
`.replace(/\t/g, "");
content += livereload;
// Save the JS file contents
file.contents = Buffer.from(content);
cb(null, file);
});
};