@modern-js/server 1.18.0 → 1.18.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +37 -0
- package/dist/js/modern/dev-tools/{dev-server-plugin.js → dev-middleware/dev-server-plugin.js} +4 -4
- package/dist/js/modern/{hmr-client → dev-tools/dev-middleware/hmr-client}/createSocketUrl.js +0 -0
- package/dist/js/modern/{hmr-client → dev-tools/dev-middleware/hmr-client}/index.js +0 -0
- package/dist/js/modern/dev-tools/dev-middleware/index.js +138 -0
- package/dist/js/modern/dev-tools/{socket-server.js → dev-middleware/socket-server.js} +8 -4
- package/dist/js/modern/dev-tools/register/index.js +5 -1
- package/dist/js/modern/server/dev-server.js +44 -141
- package/dist/js/modern/server/index.js +1 -8
- package/dist/js/node/dev-tools/{dev-server-plugin.js → dev-middleware/dev-server-plugin.js} +4 -4
- package/dist/js/node/{hmr-client → dev-tools/dev-middleware/hmr-client}/createSocketUrl.js +0 -0
- package/dist/js/node/{hmr-client → dev-tools/dev-middleware/hmr-client}/index.js +0 -0
- package/dist/js/node/dev-tools/dev-middleware/index.js +152 -0
- package/dist/js/node/dev-tools/{socket-server.js → dev-middleware/socket-server.js} +8 -4
- package/dist/js/node/dev-tools/register/index.js +5 -1
- package/dist/js/node/server/dev-server.js +45 -144
- package/dist/js/node/server/index.js +1 -9
- package/dist/types/dev-tools/{dev-server-plugin.d.ts → dev-middleware/dev-server-plugin.d.ts} +1 -1
- package/dist/types/{hmr-client → dev-tools/dev-middleware/hmr-client}/createSocketUrl.d.ts +0 -0
- package/dist/types/{hmr-client → dev-tools/dev-middleware/hmr-client}/index.d.ts +0 -0
- package/dist/types/dev-tools/dev-middleware/index.d.ts +31 -0
- package/dist/types/dev-tools/{socket-server.d.ts → dev-middleware/socket-server.d.ts} +1 -1
- package/dist/types/dev-tools/mock/getMockData.d.ts +1 -1
- package/dist/types/dev-tools/mock/index.d.ts +1 -1
- package/dist/types/index.d.ts +2 -2
- package/dist/types/server/dev-server.d.ts +6 -14
- package/dist/types/server/index.d.ts +1 -1
- package/dist/types/types.d.ts +4 -24
- package/package.json +9 -9
- package/dist/js/modern/server/dev-server-split.js +0 -43
- package/dist/js/node/server/dev-server-split.js +0 -57
- package/dist/types/server/dev-server-split.d.ts +0 -16
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _events = require("events");
|
|
9
|
+
|
|
10
|
+
var _webpackDevMiddleware = _interopRequireDefault(require("@modern-js/utils/webpack-dev-middleware"));
|
|
11
|
+
|
|
12
|
+
var _devServerPlugin = _interopRequireDefault(require("./dev-server-plugin"));
|
|
13
|
+
|
|
14
|
+
var _socketServer = _interopRequireDefault(require("./socket-server"));
|
|
15
|
+
|
|
16
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
|
+
|
|
18
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
19
|
+
|
|
20
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
21
|
+
|
|
22
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
23
|
+
|
|
24
|
+
const noop = () => {// noop
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
class DevMiddleware extends _events.EventEmitter {
|
|
28
|
+
constructor({
|
|
29
|
+
compiler,
|
|
30
|
+
dev,
|
|
31
|
+
config
|
|
32
|
+
}) {
|
|
33
|
+
super();
|
|
34
|
+
|
|
35
|
+
_defineProperty(this, "middleware", void 0);
|
|
36
|
+
|
|
37
|
+
_defineProperty(this, "compiler", void 0);
|
|
38
|
+
|
|
39
|
+
_defineProperty(this, "devOptions", void 0);
|
|
40
|
+
|
|
41
|
+
_defineProperty(this, "socketServer", void 0);
|
|
42
|
+
|
|
43
|
+
_defineProperty(this, "config", void 0);
|
|
44
|
+
|
|
45
|
+
this.compiler = compiler;
|
|
46
|
+
this.devOptions = dev;
|
|
47
|
+
this.config = config; // init socket server
|
|
48
|
+
|
|
49
|
+
this.socketServer = new _socketServer.default(dev); // Todo: should remove after abstract dev middleware
|
|
50
|
+
|
|
51
|
+
if (this.compiler) {
|
|
52
|
+
// setup compiler in server, also add dev-middleware to handler static file in memory
|
|
53
|
+
// set up plugin to each compiler
|
|
54
|
+
this.setupDevServerPlugin(); // register hooks for each compilation, update socket stats if recompiled
|
|
55
|
+
|
|
56
|
+
this.setupHooks(); // start dev middleware
|
|
57
|
+
|
|
58
|
+
this.middleware = this.setupDevMiddleware();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
init(app) {
|
|
63
|
+
app.on('listening', () => {
|
|
64
|
+
this.socketServer.prepare(app);
|
|
65
|
+
});
|
|
66
|
+
app.on('close', async () => {
|
|
67
|
+
var _this$middleware;
|
|
68
|
+
|
|
69
|
+
(_this$middleware = this.middleware) === null || _this$middleware === void 0 ? void 0 : _this$middleware.close(noop);
|
|
70
|
+
this.socketServer.close();
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
setupDevServerPlugin() {
|
|
75
|
+
const {
|
|
76
|
+
devOptions
|
|
77
|
+
} = this; // apply dev server to client compiler, add hmr client to entry.
|
|
78
|
+
|
|
79
|
+
if (this.compiler.compilers) {
|
|
80
|
+
this.compiler.compilers.forEach(target => {
|
|
81
|
+
if (this.isClientCompiler(target)) {
|
|
82
|
+
new _devServerPlugin.default(devOptions).apply(target);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
} else {
|
|
86
|
+
new _devServerPlugin.default(devOptions).apply(this.compiler);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
setupHooks() {
|
|
91
|
+
const invalidPlugin = () => {
|
|
92
|
+
this.socketServer.sockWrite('invalid');
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const addHooks = compiler => {
|
|
96
|
+
if (compiler.name === 'server') {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const {
|
|
101
|
+
compile,
|
|
102
|
+
invalid,
|
|
103
|
+
done
|
|
104
|
+
} = compiler.hooks;
|
|
105
|
+
compile.tap('modern-dev-server', invalidPlugin);
|
|
106
|
+
invalid.tap('modern-dev-server', invalidPlugin);
|
|
107
|
+
done.tap('modern-dev-server', stats => {
|
|
108
|
+
this.socketServer.updateStats(stats);
|
|
109
|
+
this.emit('change', stats);
|
|
110
|
+
});
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
if (this.compiler.compilers) {
|
|
114
|
+
this.compiler.compilers.forEach(addHooks);
|
|
115
|
+
} else {
|
|
116
|
+
addHooks(this.compiler);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
setupDevMiddleware() {
|
|
121
|
+
var _config$tools, _config$tools$devServ;
|
|
122
|
+
|
|
123
|
+
const {
|
|
124
|
+
config,
|
|
125
|
+
devOptions
|
|
126
|
+
} = this;
|
|
127
|
+
const middleware = (0, _webpackDevMiddleware.default)(this.compiler, _objectSpread({
|
|
128
|
+
headers: (_config$tools = config.tools) === null || _config$tools === void 0 ? void 0 : (_config$tools$devServ = _config$tools.devServer) === null || _config$tools$devServ === void 0 ? void 0 : _config$tools$devServ.headers,
|
|
129
|
+
stats: false
|
|
130
|
+
}, devOptions.devMiddleware));
|
|
131
|
+
return middleware;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
isClientCompiler(compiler) {
|
|
135
|
+
const {
|
|
136
|
+
target
|
|
137
|
+
} = compiler.options; // if target not contains `node`, it's a client compiler
|
|
138
|
+
|
|
139
|
+
if (target) {
|
|
140
|
+
if (Array.isArray(target)) {
|
|
141
|
+
return !target.includes('node');
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return target !== 'node';
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return compiler.name === 'client';
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
exports.default = DevMiddleware;
|
|
@@ -32,10 +32,12 @@ class SocketServer {
|
|
|
32
32
|
|
|
33
33
|
|
|
34
34
|
prepare(app) {
|
|
35
|
+
var _this$options$client;
|
|
36
|
+
|
|
35
37
|
this.app = app;
|
|
36
38
|
this.wsServer = new _ws.default.Server({
|
|
37
39
|
noServer: true,
|
|
38
|
-
path: this.options.client.path
|
|
40
|
+
path: (_this$options$client = this.options.client) === null || _this$options$client === void 0 ? void 0 : _this$options$client.path
|
|
39
41
|
}); // listen upgrade event to handle socket
|
|
40
42
|
|
|
41
43
|
this.app.on('upgrade', (req, sock, head) => {
|
|
@@ -103,6 +105,8 @@ class SocketServer {
|
|
|
103
105
|
}
|
|
104
106
|
|
|
105
107
|
onConnect(socket) {
|
|
108
|
+
var _this$options$client2, _this$options$client3, _this$options$client4;
|
|
109
|
+
|
|
106
110
|
const connection = socket;
|
|
107
111
|
connection.isAlive = true;
|
|
108
112
|
connection.on('pong', () => {
|
|
@@ -122,7 +126,7 @@ class SocketServer {
|
|
|
122
126
|
}
|
|
123
127
|
});
|
|
124
128
|
|
|
125
|
-
if (this.options.client.logging) {
|
|
129
|
+
if ((_this$options$client2 = this.options.client) !== null && _this$options$client2 !== void 0 && _this$options$client2.logging) {
|
|
126
130
|
this.singleWrite(connection, 'logging', this.options.client.logging);
|
|
127
131
|
}
|
|
128
132
|
|
|
@@ -134,11 +138,11 @@ class SocketServer {
|
|
|
134
138
|
this.singleWrite(connection, 'liveReload');
|
|
135
139
|
}
|
|
136
140
|
|
|
137
|
-
if (this.options.client.progress) {
|
|
141
|
+
if ((_this$options$client3 = this.options.client) !== null && _this$options$client3 !== void 0 && _this$options$client3.progress) {
|
|
138
142
|
this.singleWrite(connection, 'progress', this.options.client.progress);
|
|
139
143
|
}
|
|
140
144
|
|
|
141
|
-
if (this.options.client.overlay) {
|
|
145
|
+
if ((_this$options$client4 = this.options.client) !== null && _this$options$client4 !== void 0 && _this$options$client4.overlay) {
|
|
142
146
|
this.singleWrite(connection, 'overlay', this.options.client.overlay);
|
|
143
147
|
} // send first stats to active client sock if stats exist
|
|
144
148
|
|
|
@@ -46,7 +46,10 @@ const enableRegister = (projectRoot, config // eslint-disable-next-line consiste
|
|
|
46
46
|
const existTsConfigPaths = checkDep('tsconfig-paths', [projectRoot]);
|
|
47
47
|
|
|
48
48
|
if (isTsProject && existTsNode && existTsConfigPaths) {
|
|
49
|
+
var _config$output;
|
|
50
|
+
|
|
49
51
|
debug('use ts-node');
|
|
52
|
+
const distPath = (config === null || config === void 0 ? void 0 : (_config$output = config.output) === null || _config$output === void 0 ? void 0 : _config$output.path) || 'dist';
|
|
50
53
|
|
|
51
54
|
const tsNode = require('ts-node');
|
|
52
55
|
|
|
@@ -86,7 +89,8 @@ const enableRegister = (projectRoot, config // eslint-disable-next-line consiste
|
|
|
86
89
|
project: tsconfigPath,
|
|
87
90
|
// for env.d.ts, https://www.npmjs.com/package/ts-node#missing-types
|
|
88
91
|
files: true,
|
|
89
|
-
transpileOnly: true
|
|
92
|
+
transpileOnly: true,
|
|
93
|
+
ignore: ['(?:^|/)node_modules/', `(?:^|/)${distPath}/bundles/`]
|
|
90
94
|
});
|
|
91
95
|
} else {
|
|
92
96
|
debug('use @babel/register');
|
|
@@ -13,22 +13,18 @@ var _https = require("https");
|
|
|
13
13
|
|
|
14
14
|
var _utils = require("@modern-js/utils");
|
|
15
15
|
|
|
16
|
-
var _webpackDevMiddleware = _interopRequireDefault(require("@modern-js/utils/webpack-dev-middleware"));
|
|
17
|
-
|
|
18
16
|
var _prodServer = require("@modern-js/prod-server");
|
|
19
17
|
|
|
20
18
|
var _constants = require("../constants");
|
|
21
19
|
|
|
22
20
|
var _mock = require("../dev-tools/mock");
|
|
23
21
|
|
|
24
|
-
var _socketServer = _interopRequireDefault(require("../dev-tools/socket-server"));
|
|
25
|
-
|
|
26
|
-
var _devServerPlugin = _interopRequireDefault(require("../dev-tools/dev-server-plugin"));
|
|
27
|
-
|
|
28
22
|
var _register = require("../dev-tools/register");
|
|
29
23
|
|
|
30
24
|
var _watcher = _interopRequireWildcard(require("../dev-tools/watcher"));
|
|
31
25
|
|
|
26
|
+
var _devMiddleware = _interopRequireDefault(require("../dev-tools/dev-middleware"));
|
|
27
|
+
|
|
32
28
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
33
29
|
|
|
34
30
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
@@ -49,19 +45,19 @@ class ModernDevServer extends _prodServer.ModernServer {
|
|
|
49
45
|
|
|
50
46
|
_defineProperty(this, "dev", void 0);
|
|
51
47
|
|
|
52
|
-
_defineProperty(this, "
|
|
53
|
-
|
|
54
|
-
_defineProperty(this, "socketServer", void 0);
|
|
48
|
+
_defineProperty(this, "devMiddleware", void 0);
|
|
55
49
|
|
|
56
50
|
_defineProperty(this, "watcher", void 0);
|
|
57
51
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
this.workDir = this.pwd; // set webpack compiler
|
|
52
|
+
this.workDir = this.pwd; // set dev server options, like webpack-dev-server
|
|
61
53
|
|
|
62
|
-
this.
|
|
54
|
+
this.dev = this.getDevOptions(options); // create dev middleware instance
|
|
63
55
|
|
|
64
|
-
this.
|
|
56
|
+
this.devMiddleware = new _devMiddleware.default({
|
|
57
|
+
dev: this.dev,
|
|
58
|
+
compiler: options.compiler,
|
|
59
|
+
config: this.conf
|
|
60
|
+
});
|
|
65
61
|
(0, _register.enableRegister)(this.pwd, this.conf);
|
|
66
62
|
}
|
|
67
63
|
|
|
@@ -74,15 +70,15 @@ class ModernDevServer extends _prodServer.ModernServer {
|
|
|
74
70
|
} // Complete the preparation of services
|
|
75
71
|
|
|
76
72
|
|
|
77
|
-
async onInit(runner) {
|
|
73
|
+
async onInit(runner, app) {
|
|
78
74
|
var _conf$tools, _conf$tools$devServer;
|
|
79
75
|
|
|
80
76
|
this.runner = runner;
|
|
81
77
|
const {
|
|
82
78
|
conf,
|
|
83
79
|
pwd,
|
|
84
|
-
|
|
85
|
-
|
|
80
|
+
dev,
|
|
81
|
+
devMiddleware
|
|
86
82
|
} = this; // before dev handler
|
|
87
83
|
|
|
88
84
|
const beforeHandlers = await this.setupBeforeDevMiddleware();
|
|
@@ -136,14 +132,29 @@ class ModernDevServer extends _prodServer.ModernServer {
|
|
|
136
132
|
} // do webpack build / plugin apply / socket server when pass compiler instance
|
|
137
133
|
|
|
138
134
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
135
|
+
devMiddleware.init(app);
|
|
136
|
+
devMiddleware.on('change', stats => {
|
|
137
|
+
// Reset only when client compile done
|
|
138
|
+
if (stats.toJson({
|
|
139
|
+
all: false
|
|
140
|
+
}).name === 'client') {
|
|
141
|
+
this.onRepack({
|
|
142
|
+
routes: this.getRoutes()
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
this.addHandler((ctx, next) => {
|
|
147
|
+
const {
|
|
148
|
+
req,
|
|
149
|
+
res
|
|
150
|
+
} = ctx;
|
|
146
151
|
|
|
152
|
+
if (devMiddleware.middleware) {
|
|
153
|
+
devMiddleware.middleware(req, res, next);
|
|
154
|
+
} else {
|
|
155
|
+
next();
|
|
156
|
+
}
|
|
157
|
+
}); // after dev handler
|
|
147
158
|
|
|
148
159
|
const afterHandlers = await this.setupAfterDevMiddleware();
|
|
149
160
|
afterHandlers.forEach(handler => {
|
|
@@ -155,28 +166,16 @@ class ModernDevServer extends _prodServer.ModernServer {
|
|
|
155
166
|
return handler(req, res, next);
|
|
156
167
|
});
|
|
157
168
|
});
|
|
158
|
-
await super.onInit(runner); // watch mock/ server/ api/ dir file change
|
|
169
|
+
await super.onInit(runner, app); // watch mock/ server/ api/ dir file change
|
|
159
170
|
|
|
160
171
|
if (dev.watch) {
|
|
161
172
|
this.startWatcher();
|
|
162
|
-
|
|
163
|
-
|
|
173
|
+
app.on('close', async () => {
|
|
174
|
+
var _this$watcher;
|
|
164
175
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
await super.onClose();
|
|
169
|
-
await ((_this$watcher = this.watcher) === null || _this$watcher === void 0 ? void 0 : _this$watcher.close());
|
|
170
|
-
await new Promise(resolve => {
|
|
171
|
-
if (this.devMiddleware) {
|
|
172
|
-
this.devMiddleware.close(() => {
|
|
173
|
-
resolve();
|
|
174
|
-
});
|
|
175
|
-
} else {
|
|
176
|
-
resolve();
|
|
177
|
-
}
|
|
178
|
-
});
|
|
179
|
-
(_this$socketServer = this.socketServer) === null || _this$socketServer === void 0 ? void 0 : _this$socketServer.close();
|
|
176
|
+
await ((_this$watcher = this.watcher) === null || _this$watcher === void 0 ? void 0 : _this$watcher.close());
|
|
177
|
+
});
|
|
178
|
+
}
|
|
180
179
|
}
|
|
181
180
|
|
|
182
181
|
onRepack(options = {}) {
|
|
@@ -192,12 +191,6 @@ class ModernDevServer extends _prodServer.ModernServer {
|
|
|
192
191
|
super.onRepack(options);
|
|
193
192
|
}
|
|
194
193
|
|
|
195
|
-
onListening(app) {
|
|
196
|
-
var _this$socketServer2;
|
|
197
|
-
|
|
198
|
-
(_this$socketServer2 = this.socketServer) === null || _this$socketServer2 === void 0 ? void 0 : _this$socketServer2.prepare(app);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
194
|
async createHTTPServer(handler) {
|
|
202
195
|
const {
|
|
203
196
|
dev
|
|
@@ -257,104 +250,12 @@ class ModernDevServer extends _prodServer.ModernServer {
|
|
|
257
250
|
this.logger.error(e);
|
|
258
251
|
}
|
|
259
252
|
}
|
|
260
|
-
} // set up plugin to each compiler
|
|
261
|
-
// register hooks for each compilation, update socket stats if recompiled
|
|
262
|
-
// start dev middleware
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
setupCompiler(compiler) {
|
|
266
|
-
this.setupDevServerPlugin(compiler);
|
|
267
|
-
this.setupHooks();
|
|
268
|
-
return this.setupDevMiddleware(compiler);
|
|
269
253
|
}
|
|
270
254
|
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
if (target) {
|
|
277
|
-
if (Array.isArray(target)) {
|
|
278
|
-
return !target.includes('node');
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
return target !== 'node';
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
return compiler.name === 'client';
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
setupDevServerPlugin(compiler) {
|
|
288
|
-
const {
|
|
289
|
-
dev: devConf
|
|
290
|
-
} = this; // apply dev server to client compiler, add hmr client to entry.
|
|
291
|
-
|
|
292
|
-
if (compiler.compilers) {
|
|
293
|
-
compiler.compilers.forEach(target => {
|
|
294
|
-
if (this.isClientCompiler(target)) {
|
|
295
|
-
new _devServerPlugin.default(devConf).apply(target);
|
|
296
|
-
}
|
|
297
|
-
});
|
|
298
|
-
} else {
|
|
299
|
-
new _devServerPlugin.default(devConf).apply(compiler);
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
setupHooks() {
|
|
304
|
-
const invalidPlugin = () => {
|
|
305
|
-
this.socketServer.sockWrite('invalid');
|
|
306
|
-
};
|
|
307
|
-
|
|
308
|
-
const addHooks = compiler => {
|
|
309
|
-
if (compiler.name === 'server') {
|
|
310
|
-
return;
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
const {
|
|
314
|
-
compile,
|
|
315
|
-
invalid,
|
|
316
|
-
done
|
|
317
|
-
} = compiler.hooks;
|
|
318
|
-
compile.tap('modern-dev-server', invalidPlugin);
|
|
319
|
-
invalid.tap('modern-dev-server', invalidPlugin);
|
|
320
|
-
done.tap('modern-dev-server', stats => {
|
|
321
|
-
this.socketServer.updateStats(stats); // Reset only when client compile done
|
|
322
|
-
|
|
323
|
-
if (stats.toJson({
|
|
324
|
-
all: false
|
|
325
|
-
}).name === 'client') {
|
|
326
|
-
this.onRepack({
|
|
327
|
-
routes: this.getRoutes()
|
|
328
|
-
});
|
|
329
|
-
}
|
|
330
|
-
});
|
|
331
|
-
};
|
|
332
|
-
|
|
333
|
-
if (this.compiler.compilers) {
|
|
334
|
-
this.compiler.compilers.forEach(addHooks);
|
|
335
|
-
} else {
|
|
336
|
-
addHooks(this.compiler);
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
setupDevMiddleware(compiler) {
|
|
341
|
-
var _conf$tools2, _conf$tools2$devServe;
|
|
342
|
-
|
|
343
|
-
const {
|
|
344
|
-
conf
|
|
345
|
-
} = this;
|
|
346
|
-
this.devMiddleware = (0, _webpackDevMiddleware.default)(compiler, _objectSpread({
|
|
347
|
-
headers: (_conf$tools2 = conf.tools) === null || _conf$tools2 === void 0 ? void 0 : (_conf$tools2$devServe = _conf$tools2.devServer) === null || _conf$tools2$devServe === void 0 ? void 0 : _conf$tools2$devServe.headers,
|
|
348
|
-
publicPath: '/',
|
|
349
|
-
stats: false
|
|
350
|
-
}, this.dev.devMiddleware));
|
|
351
|
-
return (ctx, next) => {
|
|
352
|
-
const {
|
|
353
|
-
req,
|
|
354
|
-
res
|
|
355
|
-
} = ctx;
|
|
356
|
-
this.devMiddleware(req, res, next);
|
|
357
|
-
};
|
|
255
|
+
createContext(req, res) {
|
|
256
|
+
return super.createContext(req, res, {
|
|
257
|
+
etag: true
|
|
258
|
+
});
|
|
358
259
|
}
|
|
359
260
|
|
|
360
261
|
async setupBeforeDevMiddleware() {
|
|
@@ -9,16 +9,8 @@ var _prodServer = require("@modern-js/prod-server");
|
|
|
9
9
|
|
|
10
10
|
var _devServer = require("./dev-server");
|
|
11
11
|
|
|
12
|
-
var _devServerSplit = require("./dev-server-split");
|
|
13
|
-
|
|
14
12
|
const createDevServer = options => {
|
|
15
|
-
|
|
16
|
-
return new _devServerSplit.ModernAPIDevServer(options);
|
|
17
|
-
} else if (options.ssrOnly) {
|
|
18
|
-
return new _devServerSplit.ModernSSRDevServer(options);
|
|
19
|
-
} else {
|
|
20
|
-
return new _devServer.ModernDevServer(options);
|
|
21
|
-
}
|
|
13
|
+
return new _devServer.ModernDevServer(options);
|
|
22
14
|
};
|
|
23
15
|
|
|
24
16
|
class DevServer extends _prodServer.Server {
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import { Server, IncomingMessage, ServerResponse } from 'http';
|
|
4
|
+
import { EventEmitter } from 'events';
|
|
5
|
+
import { Compiler, MultiCompiler } from 'webpack';
|
|
6
|
+
import webpackDevMiddleware from '@modern-js/utils/webpack-dev-middleware';
|
|
7
|
+
import type { NormalizedConfig } from '@modern-js/core';
|
|
8
|
+
import { DevServerOptions } from '../../types';
|
|
9
|
+
declare type Options = {
|
|
10
|
+
compiler: MultiCompiler | Compiler | null;
|
|
11
|
+
dev: DevServerOptions;
|
|
12
|
+
config: NormalizedConfig;
|
|
13
|
+
};
|
|
14
|
+
export default class DevMiddleware extends EventEmitter {
|
|
15
|
+
middleware?: webpackDevMiddleware.API<IncomingMessage, ServerResponse>;
|
|
16
|
+
private compiler;
|
|
17
|
+
private devOptions;
|
|
18
|
+
private socketServer;
|
|
19
|
+
private config;
|
|
20
|
+
constructor({
|
|
21
|
+
compiler,
|
|
22
|
+
dev,
|
|
23
|
+
config
|
|
24
|
+
}: Options);
|
|
25
|
+
init(app: Server): void;
|
|
26
|
+
private setupDevServerPlugin;
|
|
27
|
+
private setupHooks;
|
|
28
|
+
private setupDevMiddleware;
|
|
29
|
+
private isClientCompiler;
|
|
30
|
+
}
|
|
31
|
+
export {};
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { Server } from 'http';
|
|
3
3
|
import ws from 'ws';
|
|
4
4
|
import type webpack from 'webpack';
|
|
5
|
-
import type { DevServerOptions } from '
|
|
5
|
+
import type { DevServerOptions } from '../../types';
|
|
6
6
|
export default class SocketServer {
|
|
7
7
|
private wsServer;
|
|
8
8
|
private readonly sockets;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { IncomingMessage, ServerResponse } from 'http';
|
|
3
|
-
import { ModernServerContext, NextFunction } from '@modern-js/types';
|
|
3
|
+
import type { ModernServerContext, NextFunction } from '@modern-js/types';
|
|
4
4
|
export declare type MockConfig = Record<string, {
|
|
5
5
|
data: any;
|
|
6
6
|
} | ((req: IncomingMessage, res: ServerResponse, next: NextFunction) => Promise<void>)>;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { DevServer as Server } from './server';
|
|
2
|
-
import type {
|
|
2
|
+
import type { ModernDevServerOptions } from './types';
|
|
3
3
|
export { Server };
|
|
4
|
-
export type {
|
|
4
|
+
export type { ModernDevServerOptions };
|
|
5
5
|
|
|
6
6
|
declare const _default: (options: ModernDevServerOptions) => Promise<Server>;
|
|
7
7
|
|
|
@@ -1,23 +1,19 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="node" />
|
|
3
|
-
import
|
|
3
|
+
import { IncomingMessage, Server, ServerResponse } from 'http';
|
|
4
4
|
import { ServerHookRunner, ModernServer, BuildOptions } from '@modern-js/prod-server';
|
|
5
5
|
import { WatchEvent } from '../dev-tools/watcher';
|
|
6
|
-
import { ModernDevServerOptions } from '../types';
|
|
6
|
+
import type { ModernDevServerOptions } from '../types';
|
|
7
7
|
export declare class ModernDevServer extends ModernServer {
|
|
8
8
|
private mockHandler;
|
|
9
9
|
private readonly dev;
|
|
10
|
-
private readonly
|
|
11
|
-
private socketServer;
|
|
10
|
+
private readonly devMiddleware;
|
|
12
11
|
private watcher?;
|
|
13
|
-
private devMiddleware;
|
|
14
12
|
constructor(options: ModernDevServerOptions);
|
|
15
13
|
private getDevOptions;
|
|
16
|
-
onInit(runner: ServerHookRunner): Promise<void>;
|
|
17
|
-
onClose(): Promise<void>;
|
|
14
|
+
onInit(runner: ServerHookRunner, app: Server): Promise<void>;
|
|
18
15
|
onRepack(options?: BuildOptions): void;
|
|
19
|
-
|
|
20
|
-
createHTTPServer(handler: (req: IncomingMessage, res: ServerResponse, next?: () => void) => void): Promise<http.Server | import("https").Server>;
|
|
16
|
+
createHTTPServer(handler: (req: IncomingMessage, res: ServerResponse, next?: () => void) => void): Promise<Server | import("https").Server>;
|
|
21
17
|
protected warmupSSRBundle(): void;
|
|
22
18
|
protected onServerChange({
|
|
23
19
|
filepath,
|
|
@@ -26,11 +22,7 @@ export declare class ModernDevServer extends ModernServer {
|
|
|
26
22
|
filepath: string;
|
|
27
23
|
event: WatchEvent;
|
|
28
24
|
}): void;
|
|
29
|
-
|
|
30
|
-
private isClientCompiler;
|
|
31
|
-
private setupDevServerPlugin;
|
|
32
|
-
private setupHooks;
|
|
33
|
-
private setupDevMiddleware;
|
|
25
|
+
protected createContext(req: IncomingMessage, res: ServerResponse): import("@modern-js/prod-server/src/libs/context").ModernServerContext;
|
|
34
26
|
private setupBeforeDevMiddleware;
|
|
35
27
|
private setupAfterDevMiddleware;
|
|
36
28
|
private cleanSSRCache;
|
package/dist/types/types.d.ts
CHANGED
|
@@ -1,29 +1,9 @@
|
|
|
1
1
|
import type webpack from 'webpack';
|
|
2
|
+
import type { DevServerOptions, DevServerHttpsOptions } from '@modern-js/types';
|
|
2
3
|
import type { ModernServerOptions } from '@modern-js/prod-server';
|
|
3
|
-
export
|
|
4
|
-
key: string;
|
|
5
|
-
cert: string;
|
|
6
|
-
};
|
|
7
|
-
export declare type DevServerOptions = {
|
|
8
|
-
client: {
|
|
9
|
-
path?: string;
|
|
10
|
-
port?: string;
|
|
11
|
-
host?: string;
|
|
12
|
-
logging?: string;
|
|
13
|
-
overlay?: boolean;
|
|
14
|
-
progress?: boolean;
|
|
15
|
-
};
|
|
16
|
-
devMiddleware: {
|
|
17
|
-
writeToDisk: boolean | ((filename: string) => boolean);
|
|
18
|
-
};
|
|
19
|
-
watch: boolean;
|
|
20
|
-
hot: boolean | string;
|
|
21
|
-
liveReload: boolean;
|
|
22
|
-
https?: DevServerHttpsOptions;
|
|
23
|
-
[propName: string]: any;
|
|
24
|
-
};
|
|
4
|
+
export type { DevServerOptions, DevServerHttpsOptions };
|
|
25
5
|
export declare type ExtraOptions = {
|
|
26
|
-
dev
|
|
27
|
-
compiler
|
|
6
|
+
dev: boolean | Partial<DevServerOptions>;
|
|
7
|
+
compiler: webpack.MultiCompiler | webpack.Compiler | null;
|
|
28
8
|
};
|
|
29
9
|
export declare type ModernDevServerOptions = ModernServerOptions & ExtraOptions;
|