forked from easy-team/egg-webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
54 lines (51 loc) · 1.77 KB
/
app.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
'use strict';
const path = require('path');
const fs = require('fs');
const WebpackTool = require('webpack-tool');
const convert = require('koa-convert');
const proxy = require('./lib/proxy');
const Constant = require('./lib/constant');
module.exports = app => {
app.use(function* (next) {
if (app.WEBPACK_BUILD_READY) {
yield* next;
} else {
if (app.WEBPACK_LOADING_TEXT) {
this.body = app.WEBPACK_LOADING_TEXT;
} else {
const filePath = path.resolve(__dirname, './lib/template/loading.html');
this.body = app.WEBPACK_LOADING_TEXT = fs.readFileSync(filePath, 'utf8');
}
}
});
app.messenger.setMaxListeners(app.config.webpack.maxListeners || 10000);
app.messenger.on(Constant.EVENT_WEBPACK_BUILD_STATE, data => {
app.WEBPACK_BUILD_READY = data.state;
const config = app.config.webpack;
const port = data.port;
if (!app.WEBPACK_BUILD_PROXY && config.proxy) {
app.WEBPACK_BUILD_PROXY = true;
if (typeof config.proxy === 'boolean') {
config.proxy = {
host: `http://127.0.0.1:${port}`,
match: /^\/public\//,
};
} else if (config.proxy.host) {
config.proxy.host = config.proxy.host.replace(config.port, port);
}
app.middleware.splice(app.middleware.length - 2, 0, convert(proxy(config.proxy)));
}
});
app.messenger.on(Constant.EVENT_WEBPACK_OPEN_BROWSER, () => {
const appPort = app.options.port;
const browser = app.config.webpack.browser;
if (/^https?/.test(browser)) {
WebpackTool.utils.openBrowser(appPort, browser);
} else if (browser === undefined || browser === true) {
WebpackTool.utils.openBrowser(appPort);
}
});
app.ready(() => {
app.messenger.sendToAgent(Constant.EVENT_WEBPACK_BUILD_STATE);
});
};