forked from easy-team/egg-webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
57 lines (51 loc) · 1.88 KB
/
server.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
'use strict';
const WebpackTool = require('webpack-tool');
const Utils = require('./utils');
const Constant = require('./constant');
const build = require('./build');
class WebpackServer extends WebpackTool {
constructor(agent, config) {
super(config);
this.agent = agent;
this.port = Utils.getPort(config.port);
const webpackConfigList = Array.isArray(this.config.webpackConfigList) ? this.config.webpackConfigList : [this.config.webpackConfigList];
if (webpackConfigList.length) {
this.webpackConfigList = webpackConfigList;
} else {
this.webpackConfigList = Utils.getWebpackConfig(config, { baseDir: this.agent.baseDir || process.cwd() });
}
}
start() {
build.dll(this.config, { baseDir: this.agent.baseDir }, () => {
const compilers = this.dev(this.webpackConfigList, {}, () => {
this.finish();
});
this.listen(compilers);
});
}
finish() {
this.agent.messenger.sendToApp(Constant.EVENT_WEBPACK_BUILD_STATE, { state: true, port: this.port });
this.endTime = Date.now();
console.log(`webpack build cost:${this.endTime - this.startTime}ms`);
this.openBrowser();
}
listen(compilers) {
this.agent.messenger.on(Constant.EVENT_WEBPACK_BUILD_STATE, () => {
this.agent.messenger.sendToApp(Constant.EVENT_WEBPACK_BUILD_STATE, { state: this.ready, port: this.port });
});
this.agent.messenger.on(Constant.EVENT_WEBPACK_READ_FILE_MEMORY, data => {
const filePath = data.filePath;
const fileContent = Utils.readWebpackMemoryFile(compilers, filePath, data.fileName);
this.agent.messenger.sendToApp(Constant.EVENT_WEBPACK_READ_FILE_MEMORY_CONTENT, {
fileContent, filePath
});
});
}
openBrowser() {
if (!this.opened) {
this.opened = true;
this.agent.messenger.sendToApp(Constant.EVENT_WEBPACK_OPEN_BROWSER);
}
}
}
module.exports = WebpackServer;