-
Notifications
You must be signed in to change notification settings - Fork 74
/
OpenBrowserPlugin.js
49 lines (47 loc) · 1.14 KB
/
OpenBrowserPlugin.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
const open = require('open');
module.exports = class OpenBrowser {
constructor(options) {
if (typeof options === 'string') {
this.options = Object.assign(
{
hasOpen: false
},
{
url: options
}
);
} else {
this.options = Object.assign(
{
port: 8080,
host: 'localhost',
protocol: 'http:',
hasOpen: false
},
options
);
}
}
apply(compiler) {
const options = this.options;
let url;
let hasOpen = options.hasOpen;
if (options.protocol && !options.protocol.endsWith(':')) options.protocol += ':';
if (options.url) url = options.url;
else url = `${options.protocol}//${options.host}:${options.port}`;
if (compiler.hooks) {
compiler.hooks.afterEmit.tap('openBrowser', () => {
if (!hasOpen) open(url);
hasOpen = true;
this.options.hasOpen = true;
});
} else {
compiler.plugin('after-emit', (c, cb) => {
if (!hasOpen) open(url);
hasOpen = true;
this.options.hasOpen = true;
return cb();
});
}
}
};