UNPKG

4.18 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5const http_1 = __importDefault(require("http"));
6const https_1 = __importDefault(require("https"));
7const compiler_1 = __importDefault(require("./compiler"));
8const log_1 = __importDefault(require("./log"));
9const mockServer_1 = __importDefault(require("./mockServer"));
10const mockServerHelper_1 = __importDefault(require("./mockServerHelper"));
11const mockServerMiddleware_1 = __importDefault(require("./mockServerMiddleware"));
12const mockServerOptions_1 = require("./mockServerOptions");
13const webpackMockServer = {
14 /**
15 * Applies webpackMiddleware on existed express-application
16 * @param app expressjs application that is used for mapping-routes
17 * @param extendOptions MockServerOptions that overrides default options
18 */
19 use(app, extendOptions = undefined) {
20 const opt = Object.assign(Object.assign({}, mockServerOptions_1.defOptions), extendOptions);
21 opt.compilerOptions = Object.assign(Object.assign(Object.assign({}, mockServerOptions_1.defOptions.compilerOptions), extendOptions === null || extendOptions === void 0 ? void 0 : extendOptions.compilerOptions), mockServerOptions_1.defOptions.strictCompilerOptions);
22 log_1.default.verbose = opt.verbose;
23 let isDefined = false;
24 // important to apply middleware before we made hook otherwise post/put request will be rejected
25 (0, mockServerMiddleware_1.default)(app, opt.port);
26 let disposeAll = [];
27 function setupHook(httpOrHttps) {
28 const prev = httpOrHttps.createServer;
29 disposeAll.push(() => {
30 // eslint-disable-next-line no-param-reassign
31 httpOrHttps.createServer = prev;
32 });
33 // eslint-disable-next-line no-param-reassign
34 httpOrHttps.createServer = function hook() {
35 // @ts-ignore
36 const server = prev.apply(this, arguments);
37 function parentServerCallback(err) {
38 if (isDefined) {
39 return;
40 }
41 isDefined = true;
42 disposeAll.forEach((d) => d());
43 disposeAll = [];
44 const address = server.address();
45 const parentPort = (address === null || address === void 0 ? void 0 : address.port) || (err === null || err === void 0 ? void 0 : err.port) || 8080;
46 log_1.default.debug("Parent server is started. Starting mock-server...");
47 // set another port because of httpServer in the same process overrides previous listenere
48 if (opt.port === parentPort) {
49 ++opt.port;
50 }
51 try {
52 (0, compiler_1.default)(opt.entry, opt.tsConfigFileName, opt.compilerOptions, (outFileNames) => {
53 (0, mockServer_1.default)(outFileNames, opt, (port) => {
54 (0, mockServerMiddleware_1.default)(app, port);
55 });
56 });
57 }
58 catch (ex) {
59 log_1.default.error("Unable to start server\n", ex);
60 }
61 }
62 server.once("listening", parentServerCallback);
63 server.once("error", parentServerCallback);
64 return server;
65 };
66 }
67 setupHook.call(this, http_1.default);
68 setupHook.call(this, https_1.default);
69 },
70 /**
71 * Add mock functions into webackMockServer
72 * @param mockFunction
73 */
74 add(mockFunction) {
75 return (app) => {
76 mockFunction(app, mockServerHelper_1.default);
77 };
78 },
79 /** Default MockServer options (readonly) */
80 get defaultOptions() {
81 return JSON.parse(JSON.stringify(mockServerOptions_1.defOptions));
82 },
83};
84module.exports = webpackMockServer;