@modern-js/server 1.4.16 → 1.4.19-beta.0

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,36 @@
1
1
  # @modern-js/server
2
2
 
3
+ ## 1.4.18
4
+
5
+ ### Patch Changes
6
+
7
+ - 8d508c6ed: feat(devServer): support disable hmr or live reload
8
+ - a1198d509: feat: bump babel 7.18.0
9
+ - Updated dependencies [8d508c6ed]
10
+ - Updated dependencies [a1198d509]
11
+ - Updated dependencies [29728812e]
12
+ - Updated dependencies [147e090f7]
13
+ - Updated dependencies [18892c65c]
14
+ - Updated dependencies [a1198d509]
15
+ - @modern-js/webpack@1.10.0
16
+ - @modern-js/bff-utils@1.2.9
17
+ - @modern-js/hmr-client@1.2.8
18
+ - @modern-js/prod-server@1.1.8
19
+ - @modern-js/server-utils@1.2.10
20
+
21
+ ## 1.4.17
22
+
23
+ ### Patch Changes
24
+
25
+ - d4afeba71: fix: remove cors for prod-server
26
+ - f8e713253: fix(server): using correct webpack instance
27
+ - Updated dependencies [6c8ab42dd]
28
+ - Updated dependencies [ed90859ba]
29
+ - Updated dependencies [0ef2431cb]
30
+ - Updated dependencies [d4afeba71]
31
+ - @modern-js/webpack@1.9.1
32
+ - @modern-js/prod-server@1.1.7
33
+
3
34
  ## 1.4.16
4
35
 
5
36
  ### Patch Changes
@@ -1,9 +1,9 @@
1
1
  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; }
2
2
 
3
- import Webpack from 'webpack';
3
+ import { webpack } from '@modern-js/webpack';
4
4
  const {
5
5
  EntryPlugin
6
- } = Webpack;
6
+ } = webpack;
7
7
  export default class DevServerPlugin {
8
8
  constructor(options) {
9
9
  _defineProperty(this, "options", void 0);
@@ -11,29 +11,32 @@ export default class DevServerPlugin {
11
11
  this.options = options;
12
12
  }
13
13
 
14
- apply(compiler) {
14
+ injectHMRClient(compiler) {
15
15
  const {
16
16
  client
17
17
  } = this.options;
18
18
  const host = client.host ? `&host=${client.host}` : '';
19
19
  const path = client.path ? `&path=${client.path}` : '';
20
20
  const port = client.port ? `&port=${client.port}` : '';
21
- const clientEntry = `${require.resolve('@modern-js/hmr-client')}?${host}${path}${port}`;
22
- const additionalEntries = [clientEntry]; // use a hook to add entries if available
21
+ const clientEntry = `${require.resolve('@modern-js/hmr-client')}?${host}${path}${port}`; // use a hook to add entries if available
22
+
23
+ new EntryPlugin(compiler.context, clientEntry, {
24
+ name: undefined
25
+ }).apply(compiler);
26
+ }
23
27
 
24
- for (const additionalEntry of additionalEntries) {
25
- new EntryPlugin(compiler.context, additionalEntry, {
26
- name: undefined
27
- }).apply(compiler);
28
+ apply(compiler) {
29
+ if (this.options.hot || this.options.liveReload) {
30
+ this.injectHMRClient(compiler);
28
31
  } // Todo remove, client must inject.
29
32
 
30
33
 
31
34
  const compilerOptions = compiler.options;
32
35
  compilerOptions.plugins = compilerOptions.plugins || [];
33
36
 
34
- if (!compilerOptions.plugins.find(p => p.constructor === Webpack.HotModuleReplacementPlugin)) {
37
+ if (!compilerOptions.plugins.find(p => p.constructor === webpack.HotModuleReplacementPlugin)) {
35
38
  // apply the HMR plugin, if it didn't exist before.
36
- const plugin = new Webpack.HotModuleReplacementPlugin();
39
+ const plugin = new webpack.HotModuleReplacementPlugin();
37
40
  plugin.apply(compiler);
38
41
  }
39
42
  }
@@ -69,6 +69,26 @@ export class ModernDevServer extends ModernServer {
69
69
  } = ctx;
70
70
  return handler(req, res, next);
71
71
  });
72
+ });
73
+ this.addHandler((ctx, next) => {
74
+ var _this$conf$tools$devS;
75
+
76
+ // allow hmr request cross-domain, because the user may use global proxy
77
+ if (ctx.path.includes('hot-update')) {
78
+ ctx.res.setHeader('Access-Control-Allow-Origin', '*');
79
+ ctx.res.setHeader('Access-Control-Allow-Credentials', 'false');
80
+ } // 用户在 devServer 上配置的 headers 不会对 html 的请求生效,加入下面代码,使配置的 headers 对所有请求生效
81
+
82
+
83
+ const confHeaders = (_this$conf$tools$devS = this.conf.tools.devServer) === null || _this$conf$tools$devS === void 0 ? void 0 : _this$conf$tools$devS.headers;
84
+
85
+ if (confHeaders) {
86
+ for (const [key, value] of Object.entries(confHeaders)) {
87
+ ctx.res.setHeader(key, value);
88
+ }
89
+ }
90
+
91
+ next();
72
92
  }); // mock handler
73
93
 
74
94
  this.mockHandler = createMockHandler({
@@ -5,15 +5,13 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
 
8
- var _webpack = _interopRequireDefault(require("webpack"));
9
-
10
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
8
+ var _webpack = require("@modern-js/webpack");
11
9
 
12
10
  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; }
13
11
 
14
12
  const {
15
13
  EntryPlugin
16
- } = _webpack.default;
14
+ } = _webpack.webpack;
17
15
 
18
16
  class DevServerPlugin {
19
17
  constructor(options) {
@@ -22,29 +20,32 @@ class DevServerPlugin {
22
20
  this.options = options;
23
21
  }
24
22
 
25
- apply(compiler) {
23
+ injectHMRClient(compiler) {
26
24
  const {
27
25
  client
28
26
  } = this.options;
29
27
  const host = client.host ? `&host=${client.host}` : '';
30
28
  const path = client.path ? `&path=${client.path}` : '';
31
29
  const port = client.port ? `&port=${client.port}` : '';
32
- const clientEntry = `${require.resolve('@modern-js/hmr-client')}?${host}${path}${port}`;
33
- const additionalEntries = [clientEntry]; // use a hook to add entries if available
30
+ const clientEntry = `${require.resolve('@modern-js/hmr-client')}?${host}${path}${port}`; // use a hook to add entries if available
34
31
 
35
- for (const additionalEntry of additionalEntries) {
36
- new EntryPlugin(compiler.context, additionalEntry, {
37
- name: undefined
38
- }).apply(compiler);
32
+ new EntryPlugin(compiler.context, clientEntry, {
33
+ name: undefined
34
+ }).apply(compiler);
35
+ }
36
+
37
+ apply(compiler) {
38
+ if (this.options.hot || this.options.liveReload) {
39
+ this.injectHMRClient(compiler);
39
40
  } // Todo remove, client must inject.
40
41
 
41
42
 
42
43
  const compilerOptions = compiler.options;
43
44
  compilerOptions.plugins = compilerOptions.plugins || [];
44
45
 
45
- if (!compilerOptions.plugins.find(p => p.constructor === _webpack.default.HotModuleReplacementPlugin)) {
46
+ if (!compilerOptions.plugins.find(p => p.constructor === _webpack.webpack.HotModuleReplacementPlugin)) {
46
47
  // apply the HMR plugin, if it didn't exist before.
47
- const plugin = new _webpack.default.HotModuleReplacementPlugin();
48
+ const plugin = new _webpack.webpack.HotModuleReplacementPlugin();
48
49
  plugin.apply(compiler);
49
50
  }
50
51
  }
@@ -90,6 +90,26 @@ class ModernDevServer extends _prodServer.ModernServer {
90
90
  } = ctx;
91
91
  return handler(req, res, next);
92
92
  });
93
+ });
94
+ this.addHandler((ctx, next) => {
95
+ var _this$conf$tools$devS;
96
+
97
+ // allow hmr request cross-domain, because the user may use global proxy
98
+ if (ctx.path.includes('hot-update')) {
99
+ ctx.res.setHeader('Access-Control-Allow-Origin', '*');
100
+ ctx.res.setHeader('Access-Control-Allow-Credentials', 'false');
101
+ } // 用户在 devServer 上配置的 headers 不会对 html 的请求生效,加入下面代码,使配置的 headers 对所有请求生效
102
+
103
+
104
+ const confHeaders = (_this$conf$tools$devS = this.conf.tools.devServer) === null || _this$conf$tools$devS === void 0 ? void 0 : _this$conf$tools$devS.headers;
105
+
106
+ if (confHeaders) {
107
+ for (const [key, value] of Object.entries(confHeaders)) {
108
+ ctx.res.setHeader(key, value);
109
+ }
110
+ }
111
+
112
+ next();
93
113
  }); // mock handler
94
114
 
95
115
  this.mockHandler = (0, _mock.createMockHandler)({
@@ -1,7 +1,8 @@
1
- import Webpack from 'webpack';
1
+ import { webpack } from '@modern-js/webpack';
2
2
  import { DevServerOptions } from '../types';
3
3
  export default class DevServerPlugin {
4
4
  private readonly options;
5
5
  constructor(options: DevServerOptions);
6
- apply(compiler: Webpack.Compiler): void;
6
+ injectHMRClient(compiler: webpack.Compiler): void;
7
+ apply(compiler: webpack.Compiler): void;
7
8
  }
@@ -1,7 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  import { Server } from 'http';
3
3
  import ws from 'ws';
4
- import type { Stats } from 'webpack';
4
+ import type { webpack } from '@modern-js/webpack';
5
5
  import { DevServerOptions } from '../types';
6
6
  export default class SocketServer {
7
7
  private wsServer;
@@ -12,7 +12,7 @@ export default class SocketServer {
12
12
  private timer;
13
13
  constructor(options: DevServerOptions);
14
14
  prepare(app: Server): void;
15
- updateStats(stats: Stats): void;
15
+ updateStats(stats: webpack.Stats): void;
16
16
  sockWrite(type: string, data?: Record<string, any> | string | boolean): void;
17
17
  singleWrite(socket: ws, type: string, data?: Record<string, any> | string | boolean): void;
18
18
  close(): void;
@@ -1,5 +1,5 @@
1
1
  import { ModernServerOptions } from '@modern-js/prod-server';
2
- import type Webpack from 'webpack';
2
+ import type { webpack } from '@modern-js/webpack';
3
3
  export declare type DevServerOptions = {
4
4
  client: {
5
5
  path?: string;
@@ -23,6 +23,6 @@ export declare type DevServerOptions = {
23
23
  };
24
24
  export declare type ExtraOptions = {
25
25
  dev?: boolean | Partial<DevServerOptions>;
26
- compiler?: Webpack.MultiCompiler | Webpack.Compiler | null;
26
+ compiler?: webpack.MultiCompiler | webpack.Compiler | null;
27
27
  };
28
28
  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": "1.4.16",
14
+ "version": "1.4.19-beta.0",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -30,7 +30,6 @@
30
30
  "dependencies": {
31
31
  "@babel/core": "^7.17.0",
32
32
  "@babel/register": "^7.15.3",
33
- "@modern-js/bff-utils": "^1.2.8",
34
33
  "@modern-js/hmr-client": "^1.2.7",
35
34
  "@modern-js/prod-server": "^1.1.6",
36
35
  "@modern-js/server-utils": "^1.2.9",
@@ -42,7 +41,7 @@
42
41
  "ws": "^8.2.0"
43
42
  },
44
43
  "devDependencies": {
45
- "@modern-js/core": "1.11.1",
44
+ "@modern-js/core": "1.11.2",
46
45
  "@modern-js/server-core": "1.3.5",
47
46
  "@modern-js/types": "1.5.4",
48
47
  "@scripts/build": "0.0.0",
@@ -53,7 +52,6 @@
53
52
  "@types/ws": "^7.4.7",
54
53
  "jest": "^27",
55
54
  "typescript": "^4",
56
- "webpack": "^5.71.0",
57
55
  "websocket": "^1"
58
56
  },
59
57
  "sideEffects": false,
@@ -64,7 +62,8 @@
64
62
  },
65
63
  "publishConfig": {
66
64
  "registry": "https://registry.npmjs.org/",
67
- "access": "public"
65
+ "access": "public",
66
+ "types": "./dist/types/index.d.ts"
68
67
  },
69
68
  "wireit": {
70
69
  "build": {