@modern-js/plugin-server 1.21.2 → 2.0.0-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,31 @@
1
1
  # @modern-js/plugin-server
2
2
 
3
+ ## 2.0.0-beta.0
4
+
5
+ ### Major Changes
6
+
7
+ - dda38c9: chore: v2
8
+
9
+ ### Patch Changes
10
+
11
+ - 3bbea92b2: feat: support Hook、Middleware new API
12
+ feat: 支持 Hook、Middleware 的新 API
13
+ - Updated dependencies [9b915e0c1]
14
+ - Updated dependencies [edd1cfb1a]
15
+ - Updated dependencies [cc971eabf]
16
+ - Updated dependencies [5b9049f]
17
+ - Updated dependencies [a8642da]
18
+ - Updated dependencies [b8bbe036c]
19
+ - Updated dependencies [d5a31df78]
20
+ - Updated dependencies [dda38c9]
21
+ - Updated dependencies [3bbea92b2]
22
+ - Updated dependencies [abf3421]
23
+ - Updated dependencies [543be95]
24
+ - Updated dependencies [14b712d]
25
+ - @modern-js/server-utils@2.0.0-beta.0
26
+ - @modern-js/utils@2.0.0-beta.0
27
+ - @modern-js/babel-compiler@2.0.0-beta.0
28
+
3
29
  ## 1.21.2
4
30
 
5
31
  ### Patch Changes
@@ -1,8 +1,20 @@
1
+ const _excluded = ["default", "middleware"];
2
+
3
+ function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
4
+
5
+ function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
6
+
1
7
  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
8
 
3
9
  import path from 'path';
4
- import { isProd, requireExistModule, SERVER_DIR } from '@modern-js/utils';
10
+ import { isProd, logger, requireExistModule, SERVER_DIR } from '@modern-js/utils';
5
11
  const WEB_APP_NAME = 'index';
12
+ var HOOKS;
13
+
14
+ (function (HOOKS) {
15
+ HOOKS["AFTER_MATCH"] = "afterMatch";
16
+ HOOKS["AFTER_RENDER"] = "afterRender";
17
+ })(HOOKS || (HOOKS = {}));
6
18
 
7
19
  class Storage {
8
20
  constructor() {
@@ -24,11 +36,34 @@ const createTransformAPI = storage => new Proxy({}, {
24
36
  return fn => storage.middlewares.push(fn);
25
37
  }
26
38
 
27
- return fn => storage.hooks[name] = fn;
39
+ return fn => {
40
+ logger.warn('declare hook in default export is to be deprecated, use named export instead');
41
+ storage.hooks[name] = fn;
42
+ };
28
43
  }
29
44
 
30
45
  });
31
46
 
47
+ const compose = middlewares => {
48
+ return (ctx, resolve, reject) => {
49
+ let i = 0;
50
+
51
+ const dispatch = () => {
52
+ var _handler, _handler$catch;
53
+
54
+ const handler = middlewares[i++];
55
+
56
+ if (!handler) {
57
+ return resolve();
58
+ }
59
+
60
+ return (_handler = handler(ctx, dispatch)) === null || _handler === void 0 ? void 0 : (_handler$catch = _handler.catch) === null || _handler$catch === void 0 ? void 0 : _handler$catch.call(_handler, reject);
61
+ };
62
+
63
+ return dispatch;
64
+ };
65
+ };
66
+
32
67
  export default (() => ({
33
68
  name: '@modern-js/plugin-server',
34
69
  setup: api => {
@@ -38,26 +73,46 @@ export default (() => ({
38
73
  } = api.useAppContext();
39
74
  const storage = new Storage();
40
75
  const transformAPI = createTransformAPI(storage);
41
- let webAppPath = '';
42
- return {
43
- prepare() {
44
- const pwd = isProd() ? distDirectory : appDirectory;
45
- const serverPath = path.resolve(pwd, SERVER_DIR);
46
- webAppPath = path.resolve(serverPath, WEB_APP_NAME);
47
- const webMod = requireExistModule(webAppPath);
76
+ const pwd = isProd() ? distDirectory : appDirectory;
77
+ const webAppPath = path.resolve(pwd, SERVER_DIR, WEB_APP_NAME);
78
+
79
+ const loadMod = () => {
80
+ const mod = requireExistModule(webAppPath, {
81
+ interop: false
82
+ });
48
83
 
49
- if (webMod) {
50
- webMod(transformAPI);
84
+ const {
85
+ default: defaultExports,
86
+ middleware
87
+ } = mod,
88
+ hooks = _objectWithoutProperties(mod, _excluded);
89
+
90
+ if (defaultExports) {
91
+ defaultExports(transformAPI);
92
+ } // named export hooks will overrides hooks in default export function
93
+
94
+
95
+ Object.values(HOOKS).forEach(key => {
96
+ const fn = hooks[key];
97
+
98
+ if (fn) {
99
+ storage.hooks[key] = fn;
51
100
  }
101
+ });
102
+
103
+ if (middleware) {
104
+ storage.middlewares = [].concat(middleware);
105
+ }
106
+ };
107
+
108
+ return {
109
+ prepare() {
110
+ loadMod();
52
111
  },
53
112
 
54
113
  reset() {
55
114
  storage.reset();
56
- const newWebModule = requireExistModule(webAppPath);
57
-
58
- if (newWebModule) {
59
- newWebModule(transformAPI);
60
- }
115
+ loadMod();
61
116
  },
62
117
 
63
118
  gather({
@@ -68,48 +123,47 @@ export default (() => ({
68
123
  });
69
124
  },
70
125
 
71
- beforeMatch({
72
- context
73
- }, next) {
74
- if (!storage.hooks.beforeMatch) {
75
- return next();
76
- }
77
-
78
- return storage.hooks.beforeMatch(context, next);
79
- },
80
-
81
- afterMatch({
82
- context,
83
- routeAPI
84
- }, next) {
126
+ afterMatch(context, next) {
85
127
  if (!storage.hooks.afterMatch) {
86
128
  return next();
87
129
  }
88
130
 
89
- context.router = routeAPI;
90
131
  return storage.hooks.afterMatch(context, next);
91
132
  },
92
133
 
93
- beforeRender({
94
- context
95
- }, next) {
96
- if (!storage.hooks.beforeRender) {
97
- return next();
98
- }
99
-
100
- return storage.hooks.beforeRender(context, next);
101
- },
102
-
103
- afterRender({
104
- context,
105
- templateAPI
106
- }, next) {
134
+ afterRender(context, next) {
107
135
  if (!storage.hooks.afterRender) {
108
136
  return next();
109
137
  }
110
138
 
111
- context.template = templateAPI;
112
139
  return storage.hooks.afterRender(context, next);
140
+ },
141
+
142
+ prepareWebServer({
143
+ config
144
+ }) {
145
+ const {
146
+ middleware
147
+ } = config;
148
+ const factory = compose(middleware);
149
+ return ctx => {
150
+ const {
151
+ source: {
152
+ res
153
+ }
154
+ } = ctx;
155
+ return new Promise((resolve, reject) => {
156
+ res.on('finish', err => {
157
+ if (err) {
158
+ return reject(err);
159
+ }
160
+
161
+ return resolve();
162
+ });
163
+ const dispatch = factory(ctx, resolve, reject);
164
+ dispatch();
165
+ });
166
+ };
113
167
  }
114
168
 
115
169
  };
@@ -9,11 +9,23 @@ var _path = _interopRequireDefault(require("path"));
9
9
 
10
10
  var _utils = require("@modern-js/utils");
11
11
 
12
+ const _excluded = ["default", "middleware"];
13
+
12
14
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
15
 
16
+ function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
17
+
18
+ function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
19
+
14
20
  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; }
15
21
 
16
22
  const WEB_APP_NAME = 'index';
23
+ var HOOKS;
24
+
25
+ (function (HOOKS) {
26
+ HOOKS["AFTER_MATCH"] = "afterMatch";
27
+ HOOKS["AFTER_RENDER"] = "afterRender";
28
+ })(HOOKS || (HOOKS = {}));
17
29
 
18
30
  class Storage {
19
31
  constructor() {
@@ -35,11 +47,35 @@ const createTransformAPI = storage => new Proxy({}, {
35
47
  return fn => storage.middlewares.push(fn);
36
48
  }
37
49
 
38
- return fn => storage.hooks[name] = fn;
50
+ return fn => {
51
+ _utils.logger.warn('declare hook in default export is to be deprecated, use named export instead');
52
+
53
+ storage.hooks[name] = fn;
54
+ };
39
55
  }
40
56
 
41
57
  });
42
58
 
59
+ const compose = middlewares => {
60
+ return (ctx, resolve, reject) => {
61
+ let i = 0;
62
+
63
+ const dispatch = () => {
64
+ var _handler, _handler$catch;
65
+
66
+ const handler = middlewares[i++];
67
+
68
+ if (!handler) {
69
+ return resolve();
70
+ }
71
+
72
+ return (_handler = handler(ctx, dispatch)) === null || _handler === void 0 ? void 0 : (_handler$catch = _handler.catch) === null || _handler$catch === void 0 ? void 0 : _handler$catch.call(_handler, reject);
73
+ };
74
+
75
+ return dispatch;
76
+ };
77
+ };
78
+
43
79
  var _default = () => ({
44
80
  name: '@modern-js/plugin-server',
45
81
  setup: api => {
@@ -49,28 +85,47 @@ var _default = () => ({
49
85
  } = api.useAppContext();
50
86
  const storage = new Storage();
51
87
  const transformAPI = createTransformAPI(storage);
52
- let webAppPath = '';
53
- return {
54
- prepare() {
55
- const pwd = (0, _utils.isProd)() ? distDirectory : appDirectory;
88
+ const pwd = (0, _utils.isProd)() ? distDirectory : appDirectory;
56
89
 
57
- const serverPath = _path.default.resolve(pwd, _utils.SERVER_DIR);
90
+ const webAppPath = _path.default.resolve(pwd, _utils.SERVER_DIR, WEB_APP_NAME);
58
91
 
59
- webAppPath = _path.default.resolve(serverPath, WEB_APP_NAME);
60
- const webMod = (0, _utils.requireExistModule)(webAppPath);
92
+ const loadMod = () => {
93
+ const mod = (0, _utils.requireExistModule)(webAppPath, {
94
+ interop: false
95
+ });
61
96
 
62
- if (webMod) {
63
- webMod(transformAPI);
97
+ const {
98
+ default: defaultExports,
99
+ middleware
100
+ } = mod,
101
+ hooks = _objectWithoutProperties(mod, _excluded);
102
+
103
+ if (defaultExports) {
104
+ defaultExports(transformAPI);
105
+ } // named export hooks will overrides hooks in default export function
106
+
107
+
108
+ Object.values(HOOKS).forEach(key => {
109
+ const fn = hooks[key];
110
+
111
+ if (fn) {
112
+ storage.hooks[key] = fn;
64
113
  }
114
+ });
115
+
116
+ if (middleware) {
117
+ storage.middlewares = [].concat(middleware);
118
+ }
119
+ };
120
+
121
+ return {
122
+ prepare() {
123
+ loadMod();
65
124
  },
66
125
 
67
126
  reset() {
68
127
  storage.reset();
69
- const newWebModule = (0, _utils.requireExistModule)(webAppPath);
70
-
71
- if (newWebModule) {
72
- newWebModule(transformAPI);
73
- }
128
+ loadMod();
74
129
  },
75
130
 
76
131
  gather({
@@ -81,48 +136,47 @@ var _default = () => ({
81
136
  });
82
137
  },
83
138
 
84
- beforeMatch({
85
- context
86
- }, next) {
87
- if (!storage.hooks.beforeMatch) {
88
- return next();
89
- }
90
-
91
- return storage.hooks.beforeMatch(context, next);
92
- },
93
-
94
- afterMatch({
95
- context,
96
- routeAPI
97
- }, next) {
139
+ afterMatch(context, next) {
98
140
  if (!storage.hooks.afterMatch) {
99
141
  return next();
100
142
  }
101
143
 
102
- context.router = routeAPI;
103
144
  return storage.hooks.afterMatch(context, next);
104
145
  },
105
146
 
106
- beforeRender({
107
- context
108
- }, next) {
109
- if (!storage.hooks.beforeRender) {
110
- return next();
111
- }
112
-
113
- return storage.hooks.beforeRender(context, next);
114
- },
115
-
116
- afterRender({
117
- context,
118
- templateAPI
119
- }, next) {
147
+ afterRender(context, next) {
120
148
  if (!storage.hooks.afterRender) {
121
149
  return next();
122
150
  }
123
151
 
124
- context.template = templateAPI;
125
152
  return storage.hooks.afterRender(context, next);
153
+ },
154
+
155
+ prepareWebServer({
156
+ config
157
+ }) {
158
+ const {
159
+ middleware
160
+ } = config;
161
+ const factory = compose(middleware);
162
+ return ctx => {
163
+ const {
164
+ source: {
165
+ res
166
+ }
167
+ } = ctx;
168
+ return new Promise((resolve, reject) => {
169
+ res.on('finish', err => {
170
+ if (err) {
171
+ return reject(err);
172
+ }
173
+
174
+ return resolve();
175
+ });
176
+ const dispatch = factory(ctx, resolve, reject);
177
+ dispatch();
178
+ });
179
+ };
126
180
  }
127
181
 
128
182
  };
@@ -1,9 +1,17 @@
1
+ import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
1
2
  import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
2
3
  import _createClass from "@babel/runtime/helpers/esm/createClass";
3
4
  import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
5
+ var _excluded = ["default", "middleware"];
4
6
  import path from 'path';
5
- import { isProd, requireExistModule, SERVER_DIR } from '@modern-js/utils';
7
+ import { isProd, logger, requireExistModule, SERVER_DIR } from '@modern-js/utils';
6
8
  var WEB_APP_NAME = 'index';
9
+ var HOOKS;
10
+
11
+ (function (HOOKS) {
12
+ HOOKS["AFTER_MATCH"] = "afterMatch";
13
+ HOOKS["AFTER_RENDER"] = "afterRender";
14
+ })(HOOKS || (HOOKS = {}));
7
15
 
8
16
  var Storage = /*#__PURE__*/function () {
9
17
  function Storage() {
@@ -35,12 +43,33 @@ var createTransformAPI = function createTransformAPI(storage) {
35
43
  }
36
44
 
37
45
  return function (fn) {
38
- return storage.hooks[name] = fn;
46
+ logger.warn('declare hook in default export is to be deprecated, use named export instead');
47
+ storage.hooks[name] = fn;
39
48
  };
40
49
  }
41
50
  });
42
51
  };
43
52
 
53
+ var compose = function compose(middlewares) {
54
+ return function (ctx, resolve, reject) {
55
+ var i = 0;
56
+
57
+ var dispatch = function dispatch() {
58
+ var _handler, _handler$catch;
59
+
60
+ var handler = middlewares[i++];
61
+
62
+ if (!handler) {
63
+ return resolve();
64
+ }
65
+
66
+ return (_handler = handler(ctx, dispatch)) === null || _handler === void 0 ? void 0 : (_handler$catch = _handler["catch"]) === null || _handler$catch === void 0 ? void 0 : _handler$catch.call(_handler, reject);
67
+ };
68
+
69
+ return dispatch;
70
+ };
71
+ };
72
+
44
73
  export default (function () {
45
74
  return {
46
75
  name: '@modern-js/plugin-server',
@@ -51,25 +80,43 @@ export default (function () {
51
80
 
52
81
  var storage = new Storage();
53
82
  var transformAPI = createTransformAPI(storage);
54
- var webAppPath = '';
55
- return {
56
- prepare: function prepare() {
57
- var pwd = isProd() ? distDirectory : appDirectory;
58
- var serverPath = path.resolve(pwd, SERVER_DIR);
59
- webAppPath = path.resolve(serverPath, WEB_APP_NAME);
60
- var webMod = requireExistModule(webAppPath);
83
+ var pwd = isProd() ? distDirectory : appDirectory;
84
+ var webAppPath = path.resolve(pwd, SERVER_DIR, WEB_APP_NAME);
85
+
86
+ var loadMod = function loadMod() {
87
+ var mod = requireExistModule(webAppPath, {
88
+ interop: false
89
+ });
90
+
91
+ var defaultExports = mod["default"],
92
+ middleware = mod.middleware,
93
+ hooks = _objectWithoutProperties(mod, _excluded);
94
+
95
+ if (defaultExports) {
96
+ defaultExports(transformAPI);
97
+ } // named export hooks will overrides hooks in default export function
61
98
 
62
- if (webMod) {
63
- webMod(transformAPI);
99
+
100
+ Object.values(HOOKS).forEach(function (key) {
101
+ var fn = hooks[key];
102
+
103
+ if (fn) {
104
+ storage.hooks[key] = fn;
64
105
  }
106
+ });
107
+
108
+ if (middleware) {
109
+ storage.middlewares = [].concat(middleware);
110
+ }
111
+ };
112
+
113
+ return {
114
+ prepare: function prepare() {
115
+ loadMod();
65
116
  },
66
117
  reset: function reset() {
67
118
  storage.reset();
68
- var newWebModule = requireExistModule(webAppPath);
69
-
70
- if (newWebModule) {
71
- newWebModule(transformAPI);
72
- }
119
+ loadMod();
73
120
  },
74
121
  gather: function gather(_ref) {
75
122
  var addWebMiddleware = _ref.addWebMiddleware;
@@ -77,45 +124,38 @@ export default (function () {
77
124
  addWebMiddleware(mid);
78
125
  });
79
126
  },
80
- beforeMatch: function beforeMatch(_ref2, next) {
81
- var context = _ref2.context;
82
-
83
- if (!storage.hooks.beforeMatch) {
84
- return next();
85
- }
86
-
87
- return storage.hooks.beforeMatch(context, next);
88
- },
89
- afterMatch: function afterMatch(_ref3, next) {
90
- var context = _ref3.context,
91
- routeAPI = _ref3.routeAPI;
92
-
127
+ afterMatch: function afterMatch(context, next) {
93
128
  if (!storage.hooks.afterMatch) {
94
129
  return next();
95
130
  }
96
131
 
97
- context.router = routeAPI;
98
132
  return storage.hooks.afterMatch(context, next);
99
133
  },
100
- beforeRender: function beforeRender(_ref4, next) {
101
- var context = _ref4.context;
102
-
103
- if (!storage.hooks.beforeRender) {
104
- return next();
105
- }
106
-
107
- return storage.hooks.beforeRender(context, next);
108
- },
109
- afterRender: function afterRender(_ref5, next) {
110
- var context = _ref5.context,
111
- templateAPI = _ref5.templateAPI;
112
-
134
+ afterRender: function afterRender(context, next) {
113
135
  if (!storage.hooks.afterRender) {
114
136
  return next();
115
137
  }
116
138
 
117
- context.template = templateAPI;
118
139
  return storage.hooks.afterRender(context, next);
140
+ },
141
+ prepareWebServer: function prepareWebServer(_ref2) {
142
+ var config = _ref2.config;
143
+ var middleware = config.middleware;
144
+ var factory = compose(middleware);
145
+ return function (ctx) {
146
+ var res = ctx.source.res;
147
+ return new Promise(function (resolve, reject) {
148
+ res.on('finish', function (err) {
149
+ if (err) {
150
+ return reject(err);
151
+ }
152
+
153
+ return resolve();
154
+ });
155
+ var dispatch = factory(ctx, resolve, reject);
156
+ dispatch();
157
+ });
158
+ };
119
159
  }
120
160
  };
121
161
  }
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.21.2",
14
+ "version": "2.0.0-beta.0",
15
15
  "types": "./dist/types/server.d.ts",
16
16
  "jsnext:source": "./src/server.ts",
17
17
  "main": "./dist/js/node/server.js",
@@ -48,20 +48,20 @@
48
48
  },
49
49
  "dependencies": {
50
50
  "@babel/runtime": "^7.18.0",
51
- "@modern-js/babel-compiler": "1.21.2",
52
- "@modern-js/server-utils": "1.21.2",
53
- "@modern-js/utils": "1.21.2"
51
+ "@modern-js/babel-compiler": "2.0.0-beta.0",
52
+ "@modern-js/server-utils": "2.0.0-beta.0",
53
+ "@modern-js/utils": "2.0.0-beta.0"
54
54
  },
55
55
  "devDependencies": {
56
- "@modern-js/server-core": "1.21.2",
57
- "@modern-js/core": "1.21.2",
58
- "@scripts/build": "1.21.2",
59
- "@modern-js/types": "1.21.2",
56
+ "@modern-js/server-core": "2.0.0-beta.0",
57
+ "@modern-js/core": "2.0.0-beta.0",
58
+ "@scripts/build": "2.0.0-beta.0",
59
+ "@modern-js/types": "2.0.0-beta.0",
60
60
  "typescript": "^4",
61
61
  "@types/jest": "^27",
62
62
  "@types/node": "^14",
63
63
  "jest": "^27",
64
- "@scripts/jest-config": "1.21.2"
64
+ "@scripts/jest-config": "2.0.0-beta.0"
65
65
  },
66
66
  "sideEffects": [
67
67
  "*.css",
@@ -73,33 +73,10 @@
73
73
  "access": "public",
74
74
  "registry": "https://registry.npmjs.org/"
75
75
  },
76
- "wireit": {
77
- "build": {
78
- "command": "modern build",
79
- "files": [
80
- "src/**/*",
81
- "tsconfig.json",
82
- "package.json"
83
- ],
84
- "output": [
85
- "dist/**/*"
86
- ]
87
- },
88
- "test": {
89
- "command": "jest --passWithNoTests",
90
- "files": [
91
- "src/**/*",
92
- "tsconfig.json",
93
- "package.json",
94
- "tests/**/*"
95
- ],
96
- "output": []
97
- }
98
- },
99
76
  "scripts": {
100
- "dev": "modern dev",
101
- "build": "wireit",
77
+ "dev": "modern build --watch",
78
+ "build": "modern build",
102
79
  "new": "modern new",
103
- "test": "wireit"
80
+ "test": "jest --passWithNoTests"
104
81
  }
105
82
  }