@modern-js/server 2.0.0-beta.6 → 2.0.0-beta.7

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 CHANGED
@@ -1,5 +1,24 @@
1
1
  # @modern-js/server
2
2
 
3
+ ## 2.0.0-beta.7
4
+
5
+ ### Major Changes
6
+
7
+ - dda38c9c3e: chore: v2
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [c9e800d39a]
12
+ - Updated dependencies [edd1cfb1af]
13
+ - Updated dependencies [6bda14ed71]
14
+ - Updated dependencies [dda38c9c3e]
15
+ - Updated dependencies [8b8e1bb571]
16
+ - Updated dependencies [bbe4c4ab64]
17
+ - @modern-js/prod-server@2.0.0-beta.7
18
+ - @modern-js/utils@2.0.0-beta.7
19
+ - @modern-js/types@2.0.0-beta.7
20
+ - @modern-js/server-utils@2.0.0-beta.7
21
+
3
22
  ## 2.0.0-beta.6
4
23
 
5
24
  ### Major Changes
@@ -35,20 +35,22 @@ var __async = (__this, __arguments, generator) => {
35
35
  });
36
36
  };
37
37
  import { EventEmitter } from "events";
38
- import webpackDevMiddleware from "@modern-js/utils/webpack-dev-middleware";
39
- import DevServerPlugin from "./dev-server-plugin";
40
38
  import SocketServer from "./socket-server";
41
39
  const noop = () => {
42
40
  };
41
+ function getHMRClientPath(client) {
42
+ const host = (client == null ? void 0 : client.host) ? `&host=${client.host}` : "";
43
+ const path = (client == null ? void 0 : client.path) ? `&path=${client.path}` : "";
44
+ const port = (client == null ? void 0 : client.port) ? `&port=${client.port}` : "";
45
+ const clientEntry = `${require.resolve("./hmr-client")}?${host}${path}${port}`;
46
+ return clientEntry;
47
+ }
43
48
  class DevMiddleware extends EventEmitter {
44
- constructor({ compiler, dev, devMiddleware }) {
49
+ constructor({ dev, devMiddleware }) {
45
50
  super();
46
- this.compiler = compiler;
47
51
  this.devOptions = dev;
48
52
  this.socketServer = new SocketServer(dev);
49
- if (this.compiler) {
50
- this.setupDevServerPlugin();
51
- this.setupHooks();
53
+ if (devMiddleware) {
52
54
  this.middleware = this.setupDevMiddleware(devMiddleware);
53
55
  }
54
56
  }
@@ -62,61 +64,29 @@ class DevMiddleware extends EventEmitter {
62
64
  this.socketServer.close();
63
65
  }));
64
66
  }
65
- setupDevServerPlugin() {
66
- const { devOptions } = this;
67
- if (this.compiler.compilers) {
68
- this.compiler.compilers.forEach((target) => {
69
- if (this.isClientCompiler(target)) {
70
- new DevServerPlugin(devOptions).apply(target);
71
- }
72
- });
73
- } else {
74
- new DevServerPlugin(devOptions).apply(this.compiler);
75
- }
76
- }
77
67
  sockWrite(type, data) {
78
68
  this.socketServer.sockWrite(type, data);
79
69
  }
80
- setupHooks() {
81
- const invalidPlugin = () => {
82
- this.socketServer.sockWrite("invalid");
83
- };
84
- const addHooks = (compiler) => {
85
- if (compiler.name === "server") {
86
- return;
87
- }
88
- const { compile, invalid, done } = compiler.hooks;
89
- compile.tap("modern-dev-server", invalidPlugin);
90
- invalid.tap("modern-dev-server", invalidPlugin);
91
- done.tap("modern-dev-server", (stats) => {
70
+ setupDevMiddleware(devMiddleware) {
71
+ const { devOptions } = this;
72
+ const callbacks = {
73
+ onInvalid: () => {
74
+ this.socketServer.sockWrite("invalid");
75
+ },
76
+ onDone: (stats) => {
92
77
  this.socketServer.updateStats(stats);
93
78
  this.emit("change", stats);
94
- });
79
+ }
95
80
  };
96
- if (this.compiler.compilers) {
97
- this.compiler.compilers.forEach(addHooks);
98
- } else {
99
- addHooks(this.compiler);
100
- }
101
- }
102
- setupDevMiddleware(devMiddleware = webpackDevMiddleware) {
103
- const { devOptions } = this;
104
- const middleware = devMiddleware(this.compiler, __spreadValues({
81
+ const enableHMR = this.devOptions.hot || this.devOptions.liveReload;
82
+ const middleware = devMiddleware(__spreadValues({
105
83
  headers: devOptions.headers,
106
- stats: false
84
+ stats: false,
85
+ callbacks,
86
+ hmrClientPath: enableHMR ? getHMRClientPath(devOptions.client) : void 0
107
87
  }, devOptions.devMiddleware));
108
88
  return middleware;
109
89
  }
110
- isClientCompiler(compiler) {
111
- const { target } = compiler.options;
112
- if (target) {
113
- if (Array.isArray(target)) {
114
- return !target.includes("node");
115
- }
116
- return target !== "node";
117
- }
118
- return compiler.name === "client";
119
- }
120
90
  }
121
91
  export {
122
92
  DevMiddleware as default
@@ -18,10 +18,27 @@ var __async = (__this, __arguments, generator) => {
18
18
  step((generator = generator.apply(__this, __arguments)).next());
19
19
  });
20
20
  };
21
- import * as devcert from "devcert";
22
- const genHttpsOptions = (userOptions) => __async(void 0, null, function* () {
21
+ import { chalk, getPackageManager, logger } from "@modern-js/utils";
22
+ const genHttpsOptions = (userOptions, pwd) => __async(void 0, null, function* () {
23
23
  const httpsOptions = typeof userOptions === "boolean" ? {} : userOptions;
24
24
  if (!httpsOptions.key || !httpsOptions.cert) {
25
+ let devcertPath;
26
+ try {
27
+ devcertPath = require.resolve("devcert", { paths: [pwd, __dirname] });
28
+ } catch (err) {
29
+ const packageManager = yield getPackageManager(pwd);
30
+ const command = chalk.yellow.bold(
31
+ `${packageManager} install devcert@1.2.2 -D`
32
+ );
33
+ logger.error(
34
+ `You have enabled "dev.https" option, but the "devcert" package is not installed.`
35
+ );
36
+ logger.error(
37
+ `Please run ${command} to install manually, otherwise the https can not work.`
38
+ );
39
+ throw new Error('[https] "devcert" is not found.');
40
+ }
41
+ const devcert = require(devcertPath);
25
42
  const selfsign = yield devcert.certificateFor(["localhost"]);
26
43
  return selfsign;
27
44
  }
@@ -1,7 +1,7 @@
1
1
  import { DevServer as Server } from "./server";
2
2
  var src_default = (options) => {
3
3
  if (options == null) {
4
- throw new Error("can not start mserver without options");
4
+ throw new Error("can not start server without options");
5
5
  }
6
6
  const server = new Server(options);
7
7
  return server.init();
@@ -68,7 +68,6 @@ class ModernDevServer extends ModernServer {
68
68
  this.dev = this.getDevOptions(options);
69
69
  this.devMiddleware = new DevMiddleware({
70
70
  dev: this.dev,
71
- compiler: options.compiler,
72
71
  devMiddleware: options.devMiddleware
73
72
  });
74
73
  enableRegister(this.pwd, this.conf);
@@ -195,7 +194,7 @@ class ModernDevServer extends ModernServer {
195
194
  const devHttpsOption = typeof dev === "object" && dev.https;
196
195
  if (devHttpsOption) {
197
196
  const { genHttpsOptions } = require("../dev-tools/https");
198
- const httpsOptions = yield genHttpsOptions(devHttpsOption);
197
+ const httpsOptions = yield genHttpsOptions(devHttpsOption, this.pwd);
199
198
  return createHttpsServer(httpsOptions, handler);
200
199
  } else {
201
200
  return createServer(handler);
@@ -61,20 +61,22 @@ __export(dev_middleware_exports, {
61
61
  });
62
62
  module.exports = __toCommonJS(dev_middleware_exports);
63
63
  var import_events = require("events");
64
- var import_webpack_dev_middleware = __toESM(require("@modern-js/utils/webpack-dev-middleware"));
65
- var import_dev_server_plugin = __toESM(require("./dev-server-plugin"));
66
64
  var import_socket_server = __toESM(require("./socket-server"));
67
65
  const noop = () => {
68
66
  };
67
+ function getHMRClientPath(client) {
68
+ const host = (client == null ? void 0 : client.host) ? `&host=${client.host}` : "";
69
+ const path = (client == null ? void 0 : client.path) ? `&path=${client.path}` : "";
70
+ const port = (client == null ? void 0 : client.port) ? `&port=${client.port}` : "";
71
+ const clientEntry = `${require.resolve("./hmr-client")}?${host}${path}${port}`;
72
+ return clientEntry;
73
+ }
69
74
  class DevMiddleware extends import_events.EventEmitter {
70
- constructor({ compiler, dev, devMiddleware }) {
75
+ constructor({ dev, devMiddleware }) {
71
76
  super();
72
- this.compiler = compiler;
73
77
  this.devOptions = dev;
74
78
  this.socketServer = new import_socket_server.default(dev);
75
- if (this.compiler) {
76
- this.setupDevServerPlugin();
77
- this.setupHooks();
79
+ if (devMiddleware) {
78
80
  this.middleware = this.setupDevMiddleware(devMiddleware);
79
81
  }
80
82
  }
@@ -88,61 +90,29 @@ class DevMiddleware extends import_events.EventEmitter {
88
90
  this.socketServer.close();
89
91
  }));
90
92
  }
91
- setupDevServerPlugin() {
92
- const { devOptions } = this;
93
- if (this.compiler.compilers) {
94
- this.compiler.compilers.forEach((target) => {
95
- if (this.isClientCompiler(target)) {
96
- new import_dev_server_plugin.default(devOptions).apply(target);
97
- }
98
- });
99
- } else {
100
- new import_dev_server_plugin.default(devOptions).apply(this.compiler);
101
- }
102
- }
103
93
  sockWrite(type, data) {
104
94
  this.socketServer.sockWrite(type, data);
105
95
  }
106
- setupHooks() {
107
- const invalidPlugin = () => {
108
- this.socketServer.sockWrite("invalid");
109
- };
110
- const addHooks = (compiler) => {
111
- if (compiler.name === "server") {
112
- return;
113
- }
114
- const { compile, invalid, done } = compiler.hooks;
115
- compile.tap("modern-dev-server", invalidPlugin);
116
- invalid.tap("modern-dev-server", invalidPlugin);
117
- done.tap("modern-dev-server", (stats) => {
96
+ setupDevMiddleware(devMiddleware) {
97
+ const { devOptions } = this;
98
+ const callbacks = {
99
+ onInvalid: () => {
100
+ this.socketServer.sockWrite("invalid");
101
+ },
102
+ onDone: (stats) => {
118
103
  this.socketServer.updateStats(stats);
119
104
  this.emit("change", stats);
120
- });
105
+ }
121
106
  };
122
- if (this.compiler.compilers) {
123
- this.compiler.compilers.forEach(addHooks);
124
- } else {
125
- addHooks(this.compiler);
126
- }
127
- }
128
- setupDevMiddleware(devMiddleware = import_webpack_dev_middleware.default) {
129
- const { devOptions } = this;
130
- const middleware = devMiddleware(this.compiler, __spreadValues({
107
+ const enableHMR = this.devOptions.hot || this.devOptions.liveReload;
108
+ const middleware = devMiddleware(__spreadValues({
131
109
  headers: devOptions.headers,
132
- stats: false
110
+ stats: false,
111
+ callbacks,
112
+ hmrClientPath: enableHMR ? getHMRClientPath(devOptions.client) : void 0
133
113
  }, devOptions.devMiddleware));
134
114
  return middleware;
135
115
  }
136
- isClientCompiler(compiler) {
137
- const { target } = compiler.options;
138
- if (target) {
139
- if (Array.isArray(target)) {
140
- return !target.includes("node");
141
- }
142
- return target !== "node";
143
- }
144
- return compiler.name === "client";
145
- }
146
116
  }
147
117
  // Annotate the CommonJS export names for ESM import in node:
148
118
  0 && (module.exports = {});
@@ -1,8 +1,6 @@
1
- var __create = Object.create;
2
1
  var __defProp = Object.defineProperty;
3
2
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
3
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf;
6
4
  var __hasOwnProp = Object.prototype.hasOwnProperty;
7
5
  var __export = (target, all) => {
8
6
  for (var name in all)
@@ -16,10 +14,6 @@ var __copyProps = (to, from, except, desc) => {
16
14
  }
17
15
  return to;
18
16
  };
19
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
- mod
22
- ));
23
17
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
24
18
  var __async = (__this, __arguments, generator) => {
25
19
  return new Promise((resolve, reject) => {
@@ -46,10 +40,27 @@ __export(https_exports, {
46
40
  genHttpsOptions: () => genHttpsOptions
47
41
  });
48
42
  module.exports = __toCommonJS(https_exports);
49
- var devcert = __toESM(require("devcert"));
50
- const genHttpsOptions = (userOptions) => __async(void 0, null, function* () {
43
+ var import_utils = require("@modern-js/utils");
44
+ const genHttpsOptions = (userOptions, pwd) => __async(void 0, null, function* () {
51
45
  const httpsOptions = typeof userOptions === "boolean" ? {} : userOptions;
52
46
  if (!httpsOptions.key || !httpsOptions.cert) {
47
+ let devcertPath;
48
+ try {
49
+ devcertPath = require.resolve("devcert", { paths: [pwd, __dirname] });
50
+ } catch (err) {
51
+ const packageManager = yield (0, import_utils.getPackageManager)(pwd);
52
+ const command = import_utils.chalk.yellow.bold(
53
+ `${packageManager} install devcert@1.2.2 -D`
54
+ );
55
+ import_utils.logger.error(
56
+ `You have enabled "dev.https" option, but the "devcert" package is not installed.`
57
+ );
58
+ import_utils.logger.error(
59
+ `Please run ${command} to install manually, otherwise the https can not work.`
60
+ );
61
+ throw new Error('[https] "devcert" is not found.');
62
+ }
63
+ const devcert = require(devcertPath);
53
64
  const selfsign = yield devcert.certificateFor(["localhost"]);
54
65
  return selfsign;
55
66
  }
@@ -24,7 +24,7 @@ module.exports = __toCommonJS(src_exports);
24
24
  var import_server = require("./server");
25
25
  var src_default = (options) => {
26
26
  if (options == null) {
27
- throw new Error("can not start mserver without options");
27
+ throw new Error("can not start server without options");
28
28
  }
29
29
  const server = new import_server.DevServer(options);
30
30
  return server.init();
@@ -84,7 +84,6 @@ class ModernDevServer extends import_prod_server.ModernServer {
84
84
  this.dev = this.getDevOptions(options);
85
85
  this.devMiddleware = new import_dev_middleware.default({
86
86
  dev: this.dev,
87
- compiler: options.compiler,
88
87
  devMiddleware: options.devMiddleware
89
88
  });
90
89
  (0, import_register.enableRegister)(this.pwd, this.conf);
@@ -211,7 +210,7 @@ class ModernDevServer extends import_prod_server.ModernServer {
211
210
  const devHttpsOption = typeof dev === "object" && dev.https;
212
211
  if (devHttpsOption) {
213
212
  const { genHttpsOptions } = require("../dev-tools/https");
214
- const httpsOptions = yield genHttpsOptions(devHttpsOption);
213
+ const httpsOptions = yield genHttpsOptions(devHttpsOption, this.pwd);
215
214
  return (0, import_https.createServer)(httpsOptions, handler);
216
215
  } else {
217
216
  return (0, import_http.createServer)(handler);
@@ -236,29 +236,30 @@ var __generator = this && this.__generator || function(thisArg, body) {
236
236
  }
237
237
  };
238
238
  import { EventEmitter } from "events";
239
- import webpackDevMiddleware from "@modern-js/utils/webpack-dev-middleware";
240
- import DevServerPlugin from "./dev-server-plugin";
241
239
  import SocketServer from "./socket-server";
242
240
  var noop = function() {};
241
+ function getHMRClientPath(client) {
242
+ var host = (client === null || client === void 0 ? void 0 : client.host) ? "&host=".concat(client.host) : "";
243
+ var path = (client === null || client === void 0 ? void 0 : client.path) ? "&path=".concat(client.path) : "";
244
+ var port = (client === null || client === void 0 ? void 0 : client.port) ? "&port=".concat(client.port) : "";
245
+ var clientEntry = "".concat(require.resolve("./hmr-client"), "?").concat(host).concat(path).concat(port);
246
+ return clientEntry;
247
+ }
243
248
  var DevMiddleware = /*#__PURE__*/ function(EventEmitter) {
244
249
  "use strict";
245
250
  _inherits(DevMiddleware, EventEmitter);
246
251
  var _super = _createSuper(DevMiddleware);
247
252
  function DevMiddleware(param) {
248
- var compiler = param.compiler, dev = param.dev, devMiddleware = param.devMiddleware;
253
+ var dev = param.dev, devMiddleware = param.devMiddleware;
249
254
  _classCallCheck(this, DevMiddleware);
250
255
  var _this;
251
256
  _this = _super.call(this);
252
257
  _defineProperty(_assertThisInitialized(_this), "middleware", void 0);
253
- _defineProperty(_assertThisInitialized(_this), "compiler", void 0);
254
258
  _defineProperty(_assertThisInitialized(_this), "devOptions", void 0);
255
259
  _defineProperty(_assertThisInitialized(_this), "socketServer", void 0);
256
- _this.compiler = compiler;
257
260
  _this.devOptions = dev;
258
261
  _this.socketServer = new SocketServer(dev);
259
- if (_this.compiler) {
260
- _this.setupDevServerPlugin();
261
- _this.setupHooks();
262
+ if (devMiddleware) {
262
263
  _this.middleware = _this.setupDevMiddleware(devMiddleware);
263
264
  }
264
265
  return _this;
@@ -284,22 +285,6 @@ var DevMiddleware = /*#__PURE__*/ function(EventEmitter) {
284
285
  }));
285
286
  }
286
287
  },
287
- {
288
- key: "setupDevServerPlugin",
289
- value: function setupDevServerPlugin() {
290
- var _this = this;
291
- var devOptions = this.devOptions;
292
- if (this.compiler.compilers) {
293
- this.compiler.compilers.forEach(function(target) {
294
- if (_this.isClientCompiler(target)) {
295
- new DevServerPlugin(devOptions).apply(target);
296
- }
297
- });
298
- } else {
299
- new DevServerPlugin(devOptions).apply(this.compiler);
300
- }
301
- }
302
- },
303
288
  {
304
289
  key: "sockWrite",
305
290
  value: function sockWrite(type, data) {
@@ -307,55 +292,28 @@ var DevMiddleware = /*#__PURE__*/ function(EventEmitter) {
307
292
  }
308
293
  },
309
294
  {
310
- key: "setupHooks",
311
- value: function setupHooks() {
295
+ key: "setupDevMiddleware",
296
+ value: function setupDevMiddleware(devMiddleware) {
312
297
  var _this = this;
313
- var invalidPlugin = function() {
314
- _this.socketServer.sockWrite("invalid");
315
- };
316
- var addHooks = function(compiler) {
317
- if (compiler.name === "server") {
318
- return;
319
- }
320
- var _hooks = compiler.hooks, compile = _hooks.compile, invalid = _hooks.invalid, done = _hooks.done;
321
- compile.tap("modern-dev-server", invalidPlugin);
322
- invalid.tap("modern-dev-server", invalidPlugin);
323
- done.tap("modern-dev-server", function(stats) {
298
+ var devOptions = this.devOptions;
299
+ var callbacks = {
300
+ onInvalid: function() {
301
+ _this.socketServer.sockWrite("invalid");
302
+ },
303
+ onDone: function(stats) {
324
304
  _this.socketServer.updateStats(stats);
325
305
  _this.emit("change", stats);
326
- });
306
+ }
327
307
  };
328
- if (this.compiler.compilers) {
329
- this.compiler.compilers.forEach(addHooks);
330
- } else {
331
- addHooks(this.compiler);
332
- }
333
- }
334
- },
335
- {
336
- key: "setupDevMiddleware",
337
- value: function setupDevMiddleware() {
338
- var devMiddleware = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : webpackDevMiddleware;
339
- var devOptions = this.devOptions;
340
- var middleware = devMiddleware(this.compiler, _objectSpread({
308
+ var enableHMR = this.devOptions.hot || this.devOptions.liveReload;
309
+ var middleware = devMiddleware(_objectSpread({
341
310
  headers: devOptions.headers,
342
- stats: false
311
+ stats: false,
312
+ callbacks: callbacks,
313
+ hmrClientPath: enableHMR ? getHMRClientPath(devOptions.client) : void 0
343
314
  }, devOptions.devMiddleware));
344
315
  return middleware;
345
316
  }
346
- },
347
- {
348
- key: "isClientCompiler",
349
- value: function isClientCompiler(compiler) {
350
- var target = compiler.options.target;
351
- if (target) {
352
- if (Array.isArray(target)) {
353
- return !target.includes("node");
354
- }
355
- return target !== "node";
356
- }
357
- return compiler.name === "client";
358
- }
359
317
  }
360
318
  ]);
361
319
  return DevMiddleware;
@@ -122,31 +122,63 @@ var __generator = this && this.__generator || function(thisArg, body) {
122
122
  };
123
123
  }
124
124
  };
125
- import * as devcert from "devcert";
125
+ import { chalk, getPackageManager, logger } from "@modern-js/utils";
126
126
  var genHttpsOptions = function() {
127
- var _ref = _asyncToGenerator(function(userOptions) {
128
- var httpsOptions, selfsign;
127
+ var _ref = _asyncToGenerator(function(userOptions, pwd) {
128
+ var httpsOptions, devcertPath, err, packageManager, command, devcert, selfsign;
129
129
  return __generator(this, function(_state) {
130
130
  switch(_state.label){
131
131
  case 0:
132
132
  httpsOptions = typeof userOptions === "boolean" ? {} : userOptions;
133
133
  if (!(!httpsOptions.key || !httpsOptions.cert)) return [
134
134
  3,
135
- 2
135
+ 6
136
136
  ];
137
+ _state.label = 1;
138
+ case 1:
139
+ _state.trys.push([
140
+ 1,
141
+ 2,
142
+ ,
143
+ 4
144
+ ]);
145
+ devcertPath = require.resolve("devcert", {
146
+ paths: [
147
+ pwd,
148
+ __dirname
149
+ ]
150
+ });
151
+ return [
152
+ 3,
153
+ 4
154
+ ];
155
+ case 2:
156
+ err = _state.sent();
157
+ return [
158
+ 4,
159
+ getPackageManager(pwd)
160
+ ];
161
+ case 3:
162
+ packageManager = _state.sent();
163
+ command = chalk.yellow.bold("".concat(packageManager, " install devcert@1.2.2 -D"));
164
+ logger.error('You have enabled "dev.https" option, but the "devcert" package is not installed.');
165
+ logger.error("Please run ".concat(command, " to install manually, otherwise the https can not work."));
166
+ throw new Error('[https] "devcert" is not found.');
167
+ case 4:
168
+ devcert = require(devcertPath);
137
169
  return [
138
170
  4,
139
171
  devcert.certificateFor([
140
172
  "localhost"
141
173
  ])
142
174
  ];
143
- case 1:
175
+ case 5:
144
176
  selfsign = _state.sent();
145
177
  return [
146
178
  2,
147
179
  selfsign
148
180
  ];
149
- case 2:
181
+ case 6:
150
182
  return [
151
183
  2,
152
184
  httpsOptions
@@ -154,7 +186,7 @@ var genHttpsOptions = function() {
154
186
  }
155
187
  });
156
188
  });
157
- return function genHttpsOptions(userOptions) {
189
+ return function genHttpsOptions(userOptions, pwd) {
158
190
  return _ref.apply(this, arguments);
159
191
  };
160
192
  }();
@@ -1,7 +1,7 @@
1
1
  import { DevServer as Server } from "./server";
2
2
  var src_default = function(options) {
3
3
  if (options == null) {
4
- throw new Error("can not start mserver without options");
4
+ throw new Error("can not start server without options");
5
5
  }
6
6
  var server = new Server(options);
7
7
  return server.init();
@@ -367,7 +367,6 @@ var ModernDevServer = /*#__PURE__*/ function(ModernServer) {
367
367
  _this.dev = _this.getDevOptions(options);
368
368
  _this.devMiddleware = new DevMiddleware({
369
369
  dev: _this.dev,
370
- compiler: options.compiler,
371
370
  devMiddleware: options.devMiddleware
372
371
  });
373
372
  enableRegister(_this.pwd, _this.conf);
@@ -628,7 +627,7 @@ var ModernDevServer = /*#__PURE__*/ function(ModernServer) {
628
627
  genHttpsOptions = require("../dev-tools/https").genHttpsOptions;
629
628
  return [
630
629
  4,
631
- genHttpsOptions(devHttpsOption)
630
+ genHttpsOptions(devHttpsOption, _this.pwd)
632
631
  ];
633
632
  case 1:
634
633
  httpsOptions = _state.sent();
@@ -2,28 +2,21 @@
2
2
  /// <reference types="node" />
3
3
  import { Server } from 'http';
4
4
  import { EventEmitter } from 'events';
5
- import { Compiler, MultiCompiler } from 'webpack';
6
- import { DevServerOptions, DevMiddlewareAPI, CustomDevMiddleware } from '../../types';
5
+ import { DevServerOptions, DevMiddlewareAPI, DevMiddleware as CustomDevMiddleware } from '../../types';
7
6
  declare type Options = {
8
- compiler: MultiCompiler | Compiler | null;
9
7
  dev: DevServerOptions;
10
8
  devMiddleware?: CustomDevMiddleware;
11
9
  };
12
10
  export default class DevMiddleware extends EventEmitter {
13
11
  middleware?: DevMiddlewareAPI;
14
- private compiler;
15
12
  private devOptions;
16
13
  private socketServer;
17
14
  constructor({
18
- compiler,
19
15
  dev,
20
16
  devMiddleware
21
17
  }: Options);
22
18
  init(app: Server): void;
23
- private setupDevServerPlugin;
24
19
  sockWrite(type: string, data?: Record<string, any> | string | boolean): void;
25
- private setupHooks;
26
20
  private setupDevMiddleware;
27
- private isClientCompiler;
28
21
  }
29
22
  export {};
@@ -1,6 +1,6 @@
1
1
  import type Buffer from 'buffer';
2
2
  import type { DevServerHttpsOptions } from '../../types';
3
- export declare const genHttpsOptions: (userOptions: DevServerHttpsOptions) => Promise<{
3
+ export declare const genHttpsOptions: (userOptions: DevServerHttpsOptions, pwd: string) => Promise<{
4
4
  key?: Buffer | string;
5
5
  cert?: Buffer | string;
6
6
  }>;
@@ -1,6 +1,5 @@
1
1
  /// <reference types="node" />
2
2
  import type { IncomingMessage, ServerResponse } from 'http';
3
- import type webpack from 'webpack';
4
3
  import type { DevServerOptions, DevServerHttpsOptions, NextFunction } from '@modern-js/types';
5
4
  import type { ModernServerOptions } from '@modern-js/prod-server';
6
5
  export type { DevServerOptions, DevServerHttpsOptions };
@@ -8,14 +7,32 @@ declare type Middleware = (req: IncomingMessage, res: ServerResponse, next: Next
8
7
  export declare type DevMiddlewareAPI = Middleware & {
9
8
  close: (callback: (err: Error | null | undefined) => void) => any;
10
9
  };
11
- export declare type CustomDevMiddleware = (compiler: webpack.MultiCompiler | webpack.Compiler, options: {
10
+ export declare type MiddlewareCallbacks = {
11
+ onInvalid: () => void;
12
+ onDone: (stats: any) => void;
13
+ };
14
+ export declare type DevMiddlewareOptions = {
15
+ /** To ensure HMR works, the devMiddleware need inject the hmr client path into page when HMR enable. */
16
+ hmrClientPath?: string;
17
+ /** The options need by compiler middleware (like webpackMiddleware) */
18
+
12
19
  headers?: Record<string, string>;
13
20
  writeToDisk?: boolean | ((filename: string) => boolean);
14
21
  stats?: boolean;
15
- }) => DevMiddlewareAPI;
22
+ /** should trigger when compiler hook called */
23
+
24
+ callbacks: MiddlewareCallbacks;
25
+ };
26
+ /**
27
+ * The modern/server do nothing about compiler, the devMiddleware need do such things to ensure dev works well:
28
+ * - Call compiler.watch (normally did by webpack-dev-middleware).
29
+ * - Inject the hmr client path into page (the hmr client modern/server already provide).
30
+ * - Notify server when compiler hooks are triggered.
31
+ */
32
+
33
+ export declare type DevMiddleware = (options: DevMiddlewareOptions) => DevMiddlewareAPI;
16
34
  export declare type ExtraOptions = {
17
35
  dev: boolean | Partial<DevServerOptions>;
18
- compiler: webpack.MultiCompiler | webpack.Compiler | null;
19
- devMiddleware?: CustomDevMiddleware;
36
+ devMiddleware?: DevMiddleware;
20
37
  };
21
38
  export declare type ModernDevServerOptions = ModernServerOptions & ExtraOptions;
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "2.0.0-beta.6",
14
+ "version": "2.0.0-beta.7",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -31,14 +31,13 @@
31
31
  "@babel/core": "^7.18.0",
32
32
  "@babel/register": "^7.17.7",
33
33
  "connect-history-api-fallback": "^2.0.0",
34
- "devcert": "^1.2.2",
35
34
  "minimatch": "^3.0.4",
36
35
  "path-to-regexp": "^6.2.0",
37
36
  "ws": "^8.2.0",
38
- "@modern-js/prod-server": "2.0.0-beta.6",
39
- "@modern-js/server-utils": "2.0.0-beta.6",
40
- "@modern-js/types": "2.0.0-beta.6",
41
- "@modern-js/utils": "2.0.0-beta.6"
37
+ "@modern-js/prod-server": "2.0.0-beta.7",
38
+ "@modern-js/server-utils": "2.0.0-beta.7",
39
+ "@modern-js/types": "2.0.0-beta.7",
40
+ "@modern-js/utils": "2.0.0-beta.7"
42
41
  },
43
42
  "devDependencies": {
44
43
  "@types/connect-history-api-fallback": "^1.3.5",
@@ -53,15 +52,19 @@
53
52
  "typescript": "^4",
54
53
  "webpack": "^5.75.0",
55
54
  "websocket": "^1",
56
- "@modern-js/server-core": "2.0.0-beta.6",
57
- "@scripts/jest-config": "2.0.0-beta.6",
58
- "@scripts/build": "2.0.0-beta.6"
55
+ "@modern-js/server-core": "2.0.0-beta.7",
56
+ "@scripts/build": "2.0.0-beta.7",
57
+ "@scripts/jest-config": "2.0.0-beta.7"
59
58
  },
60
59
  "peerDependencies": {
60
+ "devcert": "^1.0.0",
61
61
  "ts-node": "^10.1.0",
62
62
  "tsconfig-paths": ">= 3.0.0 || >= 4.0.0"
63
63
  },
64
64
  "peerDependenciesMeta": {
65
+ "devcert": {
66
+ "optional": true
67
+ },
65
68
  "ts-node": {
66
69
  "optional": true
67
70
  },
@@ -1,32 +0,0 @@
1
- class DevServerPlugin {
2
- constructor(options) {
3
- this.options = options;
4
- }
5
- injectHMRClient(compiler) {
6
- const { client } = this.options;
7
- const host = (client == null ? void 0 : client.host) ? `&host=${client.host}` : "";
8
- const path = (client == null ? void 0 : client.path) ? `&path=${client.path}` : "";
9
- const port = (client == null ? void 0 : client.port) ? `&port=${client.port}` : "";
10
- const clientEntry = `${require.resolve("./hmr-client")}?${host}${path}${port}`;
11
- new compiler.webpack.EntryPlugin(compiler.context, clientEntry, {
12
- name: void 0
13
- }).apply(compiler);
14
- }
15
- apply(compiler) {
16
- if (this.options.hot || this.options.liveReload) {
17
- this.injectHMRClient(compiler);
18
- }
19
- const compilerOptions = compiler.options;
20
- const { HotModuleReplacementPlugin } = compiler.webpack;
21
- compilerOptions.plugins = compilerOptions.plugins || [];
22
- if (!compilerOptions.plugins.find(
23
- (p) => p.constructor === HotModuleReplacementPlugin
24
- )) {
25
- const plugin = new HotModuleReplacementPlugin();
26
- plugin.apply(compiler);
27
- }
28
- }
29
- }
30
- export {
31
- DevServerPlugin as default
32
- };
@@ -1,59 +0,0 @@
1
- var __create = Object.create;
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __export = (target, all) => {
8
- for (var name in all)
9
- __defProp(target, name, { get: all[name], enumerable: true });
10
- };
11
- var __copyProps = (to, from, except, desc) => {
12
- if (from && typeof from === "object" || typeof from === "function") {
13
- for (let key of __getOwnPropNames(from))
14
- if (!__hasOwnProp.call(to, key) && key !== except)
15
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
- }
17
- return to;
18
- };
19
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
- mod
22
- ));
23
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
24
- var dev_server_plugin_exports = {};
25
- __export(dev_server_plugin_exports, {
26
- default: () => DevServerPlugin
27
- });
28
- module.exports = __toCommonJS(dev_server_plugin_exports);
29
- class DevServerPlugin {
30
- constructor(options) {
31
- this.options = options;
32
- }
33
- injectHMRClient(compiler) {
34
- const { client } = this.options;
35
- const host = (client == null ? void 0 : client.host) ? `&host=${client.host}` : "";
36
- const path = (client == null ? void 0 : client.path) ? `&path=${client.path}` : "";
37
- const port = (client == null ? void 0 : client.port) ? `&port=${client.port}` : "";
38
- const clientEntry = `${require.resolve("./hmr-client")}?${host}${path}${port}`;
39
- new compiler.webpack.EntryPlugin(compiler.context, clientEntry, {
40
- name: void 0
41
- }).apply(compiler);
42
- }
43
- apply(compiler) {
44
- if (this.options.hot || this.options.liveReload) {
45
- this.injectHMRClient(compiler);
46
- }
47
- const compilerOptions = compiler.options;
48
- const { HotModuleReplacementPlugin } = compiler.webpack;
49
- compilerOptions.plugins = compilerOptions.plugins || [];
50
- if (!compilerOptions.plugins.find(
51
- (p) => p.constructor === HotModuleReplacementPlugin
52
- )) {
53
- const plugin = new HotModuleReplacementPlugin();
54
- plugin.apply(compiler);
55
- }
56
- }
57
- }
58
- // Annotate the CommonJS export names for ESM import in node:
59
- 0 && (module.exports = {});
@@ -1,74 +0,0 @@
1
- function _classCallCheck(instance, Constructor) {
2
- if (!(instance instanceof Constructor)) {
3
- throw new TypeError("Cannot call a class as a function");
4
- }
5
- }
6
- function _defineProperties(target, props) {
7
- for(var i = 0; i < props.length; i++){
8
- var descriptor = props[i];
9
- descriptor.enumerable = descriptor.enumerable || false;
10
- descriptor.configurable = true;
11
- if ("value" in descriptor) descriptor.writable = true;
12
- Object.defineProperty(target, descriptor.key, descriptor);
13
- }
14
- }
15
- function _createClass(Constructor, protoProps, staticProps) {
16
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
17
- if (staticProps) _defineProperties(Constructor, staticProps);
18
- return Constructor;
19
- }
20
- function _defineProperty(obj, key, value) {
21
- if (key in obj) {
22
- Object.defineProperty(obj, key, {
23
- value: value,
24
- enumerable: true,
25
- configurable: true,
26
- writable: true
27
- });
28
- } else {
29
- obj[key] = value;
30
- }
31
- return obj;
32
- }
33
- var DevServerPlugin = /*#__PURE__*/ function() {
34
- "use strict";
35
- function DevServerPlugin(options) {
36
- _classCallCheck(this, DevServerPlugin);
37
- _defineProperty(this, "options", void 0);
38
- this.options = options;
39
- }
40
- _createClass(DevServerPlugin, [
41
- {
42
- key: "injectHMRClient",
43
- value: function injectHMRClient(compiler) {
44
- var client = this.options.client;
45
- var host = (client === null || client === void 0 ? void 0 : client.host) ? "&host=".concat(client.host) : "";
46
- var path = (client === null || client === void 0 ? void 0 : client.path) ? "&path=".concat(client.path) : "";
47
- var port = (client === null || client === void 0 ? void 0 : client.port) ? "&port=".concat(client.port) : "";
48
- var clientEntry = "".concat(require.resolve("./hmr-client"), "?").concat(host).concat(path).concat(port);
49
- new compiler.webpack.EntryPlugin(compiler.context, clientEntry, {
50
- name: void 0
51
- }).apply(compiler);
52
- }
53
- },
54
- {
55
- key: "apply",
56
- value: function apply(compiler) {
57
- if (this.options.hot || this.options.liveReload) {
58
- this.injectHMRClient(compiler);
59
- }
60
- var compilerOptions = compiler.options;
61
- var HotModuleReplacementPlugin = compiler.webpack.HotModuleReplacementPlugin;
62
- compilerOptions.plugins = compilerOptions.plugins || [];
63
- if (!compilerOptions.plugins.find(function(p) {
64
- return p.constructor === HotModuleReplacementPlugin;
65
- })) {
66
- var plugin = new HotModuleReplacementPlugin();
67
- plugin.apply(compiler);
68
- }
69
- }
70
- }
71
- ]);
72
- return DevServerPlugin;
73
- }();
74
- export { DevServerPlugin as default };
@@ -1,8 +0,0 @@
1
- import type webpack from 'webpack';
2
- import type { DevServerOptions } from '../../types';
3
- export default class DevServerPlugin {
4
- private readonly options;
5
- constructor(options: DevServerOptions);
6
- injectHMRClient(compiler: webpack.Compiler): void;
7
- apply(compiler: webpack.Compiler): void;
8
- }