forked from apollographql/apollo-client-devtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebExtPlugin.js
87 lines (75 loc) · 2.03 KB
/
WebExtPlugin.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
// Original source: https://github.com/hiikezoe/web-ext-webpack-plugin/blob/master/web-ext-webpack-plugin.js
const webExt = require("web-ext");
const pluginName = "WebExtPlugin";
class WebExtPlugin {
constructor({
sourceDir = "build",
browserConsole = true,
startUrl = "localhost:3000",
target,
lintOnBuild = false,
} = {}) {
this.runner = null;
this.watchMode = false;
(this.browserConsole = browserConsole),
(this.sourceDir = `${process.cwd()}/${sourceDir}`);
this.startUrl = startUrl;
this.target = target;
this.lintOnBuild = lintOnBuild;
}
apply(compiler) {
const watchRun = async (compiler) => {
this.watchMode = true;
};
const afterEmit = async (compilation) => {
try {
if (this.lintOnBuild) {
await webExt.cmd.lint(
{
boring: false,
metadata: false,
output: "text",
pretty: false,
sourceDir: this.sourceDir,
verbose: false,
warningsAsErrors: true,
},
{
shouldExitProgram: false,
}
);
}
if (!this.watchMode) {
return;
}
if (this.runner) {
this.runner.reloadAllExtensions();
return;
}
await webExt.cmd
.run(
{
browserConsole: this.browserConsole,
sourceDir: this.sourceDir,
startUrl: this.startUrl,
noReload: true,
target: this.target,
},
{}
)
.then((runner) => (this.runner = runner));
if (!this.runner) {
return;
}
this.runner.registerCleanup(() => {
this.runner = null;
});
} catch (err) {
console.log(err);
}
};
compiler.hooks.afterEmit.tapPromise({ name: pluginName }, afterEmit);
compiler.hooks.watchRun.tapPromise({ name: pluginName }, watchRun);
}
}
module.exports = WebExtPlugin;