@modern-js/plugin-server 1.21.5 → 2.0.0-beta.1
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +45 -15
- package/dist/js/modern/cli.js +0 -6
- package/dist/js/modern/server.js +82 -62
- package/dist/js/node/cli.js +0 -19
- package/dist/js/node/server.js +81 -68
- package/dist/js/treeshaking/cli.js +0 -7
- package/dist/js/treeshaking/server.js +72 -57
- package/dist/types/cli.d.ts +0 -2
- package/dist/types/server.d.ts +0 -2
- package/package.json +12 -35
package/CHANGELOG.md
CHANGED
@@ -1,28 +1,58 @@
|
|
1
1
|
# @modern-js/plugin-server
|
2
2
|
|
3
|
-
##
|
3
|
+
## 2.0.0-beta.1
|
4
4
|
|
5
|
-
###
|
6
|
-
|
7
|
-
- @modern-js/server-utils@1.21.5
|
8
|
-
- @modern-js/babel-compiler@1.21.5
|
9
|
-
- @modern-js/utils@1.21.5
|
5
|
+
### Major Changes
|
10
6
|
|
11
|
-
|
7
|
+
- dda38c9: chore: v2
|
12
8
|
|
13
9
|
### Patch Changes
|
14
10
|
|
15
|
-
-
|
16
|
-
|
17
|
-
-
|
18
|
-
|
19
|
-
|
11
|
+
- 3bbea92b2a: feat: support Hook、Middleware new API
|
12
|
+
feat: 支持 Hook、Middleware 的新 API
|
13
|
+
- Updated dependencies [9b915e0c10]
|
14
|
+
- Updated dependencies [92f0ead]
|
15
|
+
- Updated dependencies [edd1cfb1af]
|
16
|
+
- Updated dependencies [cc971eabfc]
|
17
|
+
- Updated dependencies [5b9049f]
|
18
|
+
- Updated dependencies [a8642da]
|
19
|
+
- Updated dependencies [92004d1]
|
20
|
+
- Updated dependencies [b8bbe036c7]
|
21
|
+
- Updated dependencies [d5a31df781]
|
22
|
+
- Updated dependencies [dda38c9]
|
23
|
+
- Updated dependencies [3bbea92b2a]
|
24
|
+
- Updated dependencies [abf3421]
|
25
|
+
- Updated dependencies [543be9558e]
|
26
|
+
- Updated dependencies [14b712d]
|
27
|
+
- @modern-js/server-utils@2.0.0-beta.1
|
28
|
+
- @modern-js/utils@2.0.0-beta.1
|
29
|
+
- @modern-js/babel-compiler@2.0.0-beta.1
|
30
|
+
|
31
|
+
## 2.0.0-beta.0
|
32
|
+
|
33
|
+
### Major Changes
|
34
|
+
|
35
|
+
- dda38c9: chore: v2
|
20
36
|
|
21
37
|
### Patch Changes
|
22
38
|
|
23
|
-
-
|
24
|
-
|
25
|
-
-
|
39
|
+
- 3bbea92b2: feat: support Hook、Middleware new API
|
40
|
+
feat: 支持 Hook、Middleware 的新 API
|
41
|
+
- Updated dependencies [9b915e0c1]
|
42
|
+
- Updated dependencies [edd1cfb1a]
|
43
|
+
- Updated dependencies [cc971eabf]
|
44
|
+
- Updated dependencies [5b9049f]
|
45
|
+
- Updated dependencies [a8642da]
|
46
|
+
- Updated dependencies [b8bbe036c]
|
47
|
+
- Updated dependencies [d5a31df78]
|
48
|
+
- Updated dependencies [dda38c9]
|
49
|
+
- Updated dependencies [3bbea92b2]
|
50
|
+
- Updated dependencies [abf3421]
|
51
|
+
- Updated dependencies [543be95]
|
52
|
+
- Updated dependencies [14b712d]
|
53
|
+
- @modern-js/server-utils@2.0.0-beta.0
|
54
|
+
- @modern-js/utils@2.0.0-beta.0
|
55
|
+
- @modern-js/babel-compiler@2.0.0-beta.0
|
26
56
|
|
27
57
|
## 1.21.2
|
28
58
|
|
package/dist/js/modern/cli.js
CHANGED
@@ -9,7 +9,6 @@ export default (() => ({
|
|
9
9
|
config() {
|
10
10
|
return {};
|
11
11
|
},
|
12
|
-
|
13
12
|
async afterBuild() {
|
14
13
|
const {
|
15
14
|
appDirectory,
|
@@ -21,15 +20,12 @@ export default (() => ({
|
|
21
20
|
const sharedDir = path.resolve(appDirectory, SHARED_DIR);
|
22
21
|
const tsconfigPath = path.resolve(appDirectory, TS_CONFIG_FILENAME);
|
23
22
|
const sourceDirs = [];
|
24
|
-
|
25
23
|
if (fs.existsSync(serverDir)) {
|
26
24
|
sourceDirs.push(serverDir);
|
27
25
|
}
|
28
|
-
|
29
26
|
if (fs.existsSync(sharedDir)) {
|
30
27
|
sourceDirs.push(sharedDir);
|
31
28
|
}
|
32
|
-
|
33
29
|
const {
|
34
30
|
server
|
35
31
|
} = modernConfig;
|
@@ -41,7 +37,6 @@ export default (() => ({
|
|
41
37
|
const {
|
42
38
|
babel
|
43
39
|
} = modernConfig.tools;
|
44
|
-
|
45
40
|
if (sourceDirs.length > 0) {
|
46
41
|
await compile(appDirectory, {
|
47
42
|
server,
|
@@ -56,6 +51,5 @@ export default (() => ({
|
|
56
51
|
});
|
57
52
|
}
|
58
53
|
}
|
59
|
-
|
60
54
|
})
|
61
55
|
}));
|
package/dist/js/modern/server.js
CHANGED
@@ -1,34 +1,50 @@
|
|
1
|
+
const _excluded = ["default", "middleware"];
|
2
|
+
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; }
|
3
|
+
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; }
|
1
4
|
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
|
-
|
3
5
|
import path from 'path';
|
4
|
-
import { isProd, requireExistModule, SERVER_DIR } from '@modern-js/utils';
|
6
|
+
import { isProd, logger, requireExistModule, SERVER_DIR } from '@modern-js/utils';
|
5
7
|
const WEB_APP_NAME = 'index';
|
6
|
-
|
8
|
+
var HOOKS;
|
9
|
+
(function (HOOKS) {
|
10
|
+
HOOKS["AFTER_MATCH"] = "afterMatch";
|
11
|
+
HOOKS["AFTER_RENDER"] = "afterRender";
|
12
|
+
})(HOOKS || (HOOKS = {}));
|
7
13
|
class Storage {
|
8
14
|
constructor() {
|
9
15
|
_defineProperty(this, "middlewares", []);
|
10
|
-
|
11
16
|
_defineProperty(this, "hooks", {});
|
12
17
|
}
|
13
|
-
|
14
18
|
reset() {
|
15
19
|
this.middlewares = [];
|
16
20
|
this.hooks = {};
|
17
21
|
}
|
18
|
-
|
19
22
|
}
|
20
|
-
|
21
23
|
const createTransformAPI = storage => new Proxy({}, {
|
22
24
|
get(target, name) {
|
23
25
|
if (name === 'addMiddleware') {
|
24
26
|
return fn => storage.middlewares.push(fn);
|
25
27
|
}
|
26
|
-
|
27
|
-
|
28
|
+
return fn => {
|
29
|
+
logger.warn('declare hook in default export is to be deprecated, use named export instead');
|
30
|
+
storage.hooks[name] = fn;
|
31
|
+
};
|
28
32
|
}
|
29
|
-
|
30
33
|
});
|
31
|
-
|
34
|
+
const compose = middlewares => {
|
35
|
+
return (ctx, resolve, reject) => {
|
36
|
+
let i = 0;
|
37
|
+
const dispatch = () => {
|
38
|
+
var _handler, _handler$catch;
|
39
|
+
const handler = middlewares[i++];
|
40
|
+
if (!handler) {
|
41
|
+
return resolve();
|
42
|
+
}
|
43
|
+
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);
|
44
|
+
};
|
45
|
+
return dispatch;
|
46
|
+
};
|
47
|
+
};
|
32
48
|
export default (() => ({
|
33
49
|
name: '@modern-js/plugin-server',
|
34
50
|
setup: api => {
|
@@ -38,28 +54,40 @@ export default (() => ({
|
|
38
54
|
} = api.useAppContext();
|
39
55
|
const storage = new Storage();
|
40
56
|
const transformAPI = createTransformAPI(storage);
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
57
|
+
const pwd = isProd() ? distDirectory : appDirectory;
|
58
|
+
const webAppPath = path.resolve(pwd, SERVER_DIR, WEB_APP_NAME);
|
59
|
+
const loadMod = () => {
|
60
|
+
const mod = requireExistModule(webAppPath, {
|
61
|
+
interop: false
|
62
|
+
});
|
63
|
+
const {
|
64
|
+
default: defaultExports,
|
65
|
+
middleware
|
66
|
+
} = mod,
|
67
|
+
hooks = _objectWithoutProperties(mod, _excluded);
|
68
|
+
if (defaultExports) {
|
69
|
+
defaultExports(transformAPI);
|
70
|
+
}
|
48
71
|
|
49
|
-
|
50
|
-
|
72
|
+
// named export hooks will overrides hooks in default export function
|
73
|
+
Object.values(HOOKS).forEach(key => {
|
74
|
+
const fn = hooks[key];
|
75
|
+
if (fn) {
|
76
|
+
storage.hooks[key] = fn;
|
51
77
|
}
|
78
|
+
});
|
79
|
+
if (middleware) {
|
80
|
+
storage.middlewares = [].concat(middleware);
|
81
|
+
}
|
82
|
+
};
|
83
|
+
return {
|
84
|
+
prepare() {
|
85
|
+
loadMod();
|
52
86
|
},
|
53
|
-
|
54
87
|
reset() {
|
55
88
|
storage.reset();
|
56
|
-
|
57
|
-
|
58
|
-
if (newWebModule) {
|
59
|
-
newWebModule(transformAPI);
|
60
|
-
}
|
89
|
+
loadMod();
|
61
90
|
},
|
62
|
-
|
63
91
|
gather({
|
64
92
|
addWebMiddleware
|
65
93
|
}) {
|
@@ -67,51 +95,43 @@ export default (() => ({
|
|
67
95
|
addWebMiddleware(mid);
|
68
96
|
});
|
69
97
|
},
|
70
|
-
|
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) {
|
98
|
+
afterMatch(context, next) {
|
85
99
|
if (!storage.hooks.afterMatch) {
|
86
100
|
return next();
|
87
101
|
}
|
88
|
-
|
89
|
-
context.router = routeAPI;
|
90
102
|
return storage.hooks.afterMatch(context, next);
|
91
103
|
},
|
92
|
-
|
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) {
|
104
|
+
afterRender(context, next) {
|
107
105
|
if (!storage.hooks.afterRender) {
|
108
106
|
return next();
|
109
107
|
}
|
110
|
-
|
111
|
-
context.template = templateAPI;
|
112
108
|
return storage.hooks.afterRender(context, next);
|
109
|
+
},
|
110
|
+
prepareWebServer({
|
111
|
+
config
|
112
|
+
}) {
|
113
|
+
const {
|
114
|
+
middleware
|
115
|
+
} = config;
|
116
|
+
const factory = compose(middleware);
|
117
|
+
return ctx => {
|
118
|
+
const {
|
119
|
+
source: {
|
120
|
+
res
|
121
|
+
}
|
122
|
+
} = ctx;
|
123
|
+
return new Promise((resolve, reject) => {
|
124
|
+
res.on('finish', err => {
|
125
|
+
if (err) {
|
126
|
+
return reject(err);
|
127
|
+
}
|
128
|
+
return resolve();
|
129
|
+
});
|
130
|
+
const dispatch = factory(ctx, resolve, reject);
|
131
|
+
dispatch();
|
132
|
+
});
|
133
|
+
};
|
113
134
|
}
|
114
|
-
|
115
135
|
};
|
116
136
|
}
|
117
137
|
}));
|
package/dist/js/node/cli.js
CHANGED
@@ -4,51 +4,35 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.default = void 0;
|
7
|
-
|
8
7
|
var _fs = _interopRequireDefault(require("fs"));
|
9
|
-
|
10
8
|
var _path = _interopRequireDefault(require("path"));
|
11
|
-
|
12
9
|
var _serverUtils = require("@modern-js/server-utils");
|
13
|
-
|
14
10
|
var _utils = require("@modern-js/utils");
|
15
|
-
|
16
11
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
17
|
-
|
18
12
|
const TS_CONFIG_FILENAME = 'tsconfig.json';
|
19
|
-
|
20
13
|
var _default = () => ({
|
21
14
|
name: '@modern-js/plugin-server',
|
22
15
|
setup: api => ({
|
23
16
|
config() {
|
24
17
|
return {};
|
25
18
|
},
|
26
|
-
|
27
19
|
async afterBuild() {
|
28
20
|
const {
|
29
21
|
appDirectory,
|
30
22
|
distDirectory
|
31
23
|
} = api.useAppContext();
|
32
24
|
const modernConfig = api.useResolvedConfigContext();
|
33
|
-
|
34
25
|
const distDir = _path.default.resolve(distDirectory);
|
35
|
-
|
36
26
|
const serverDir = _path.default.resolve(appDirectory, _utils.SERVER_DIR);
|
37
|
-
|
38
27
|
const sharedDir = _path.default.resolve(appDirectory, _utils.SHARED_DIR);
|
39
|
-
|
40
28
|
const tsconfigPath = _path.default.resolve(appDirectory, TS_CONFIG_FILENAME);
|
41
|
-
|
42
29
|
const sourceDirs = [];
|
43
|
-
|
44
30
|
if (_fs.default.existsSync(serverDir)) {
|
45
31
|
sourceDirs.push(serverDir);
|
46
32
|
}
|
47
|
-
|
48
33
|
if (_fs.default.existsSync(sharedDir)) {
|
49
34
|
sourceDirs.push(sharedDir);
|
50
35
|
}
|
51
|
-
|
52
36
|
const {
|
53
37
|
server
|
54
38
|
} = modernConfig;
|
@@ -60,7 +44,6 @@ var _default = () => ({
|
|
60
44
|
const {
|
61
45
|
babel
|
62
46
|
} = modernConfig.tools;
|
63
|
-
|
64
47
|
if (sourceDirs.length > 0) {
|
65
48
|
await (0, _serverUtils.compile)(appDirectory, {
|
66
49
|
server,
|
@@ -75,8 +58,6 @@ var _default = () => ({
|
|
75
58
|
});
|
76
59
|
}
|
77
60
|
}
|
78
|
-
|
79
61
|
})
|
80
62
|
});
|
81
|
-
|
82
63
|
exports.default = _default;
|
package/dist/js/node/server.js
CHANGED
@@ -4,42 +4,54 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.default = void 0;
|
7
|
-
|
8
7
|
var _path = _interopRequireDefault(require("path"));
|
9
|
-
|
10
8
|
var _utils = require("@modern-js/utils");
|
11
|
-
|
9
|
+
const _excluded = ["default", "middleware"];
|
12
10
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
13
|
-
|
11
|
+
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; }
|
12
|
+
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; }
|
14
13
|
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
|
-
|
16
14
|
const WEB_APP_NAME = 'index';
|
17
|
-
|
15
|
+
var HOOKS;
|
16
|
+
(function (HOOKS) {
|
17
|
+
HOOKS["AFTER_MATCH"] = "afterMatch";
|
18
|
+
HOOKS["AFTER_RENDER"] = "afterRender";
|
19
|
+
})(HOOKS || (HOOKS = {}));
|
18
20
|
class Storage {
|
19
21
|
constructor() {
|
20
22
|
_defineProperty(this, "middlewares", []);
|
21
|
-
|
22
23
|
_defineProperty(this, "hooks", {});
|
23
24
|
}
|
24
|
-
|
25
25
|
reset() {
|
26
26
|
this.middlewares = [];
|
27
27
|
this.hooks = {};
|
28
28
|
}
|
29
|
-
|
30
29
|
}
|
31
|
-
|
32
30
|
const createTransformAPI = storage => new Proxy({}, {
|
33
31
|
get(target, name) {
|
34
32
|
if (name === 'addMiddleware') {
|
35
33
|
return fn => storage.middlewares.push(fn);
|
36
34
|
}
|
37
|
-
|
38
|
-
|
35
|
+
return fn => {
|
36
|
+
_utils.logger.warn('declare hook in default export is to be deprecated, use named export instead');
|
37
|
+
storage.hooks[name] = fn;
|
38
|
+
};
|
39
39
|
}
|
40
|
-
|
41
40
|
});
|
42
|
-
|
41
|
+
const compose = middlewares => {
|
42
|
+
return (ctx, resolve, reject) => {
|
43
|
+
let i = 0;
|
44
|
+
const dispatch = () => {
|
45
|
+
var _handler, _handler$catch;
|
46
|
+
const handler = middlewares[i++];
|
47
|
+
if (!handler) {
|
48
|
+
return resolve();
|
49
|
+
}
|
50
|
+
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);
|
51
|
+
};
|
52
|
+
return dispatch;
|
53
|
+
};
|
54
|
+
};
|
43
55
|
var _default = () => ({
|
44
56
|
name: '@modern-js/plugin-server',
|
45
57
|
setup: api => {
|
@@ -49,30 +61,40 @@ var _default = () => ({
|
|
49
61
|
} = api.useAppContext();
|
50
62
|
const storage = new Storage();
|
51
63
|
const transformAPI = createTransformAPI(storage);
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
64
|
+
const pwd = (0, _utils.isProd)() ? distDirectory : appDirectory;
|
65
|
+
const webAppPath = _path.default.resolve(pwd, _utils.SERVER_DIR, WEB_APP_NAME);
|
66
|
+
const loadMod = () => {
|
67
|
+
const mod = (0, _utils.requireExistModule)(webAppPath, {
|
68
|
+
interop: false
|
69
|
+
});
|
70
|
+
const {
|
71
|
+
default: defaultExports,
|
72
|
+
middleware
|
73
|
+
} = mod,
|
74
|
+
hooks = _objectWithoutProperties(mod, _excluded);
|
75
|
+
if (defaultExports) {
|
76
|
+
defaultExports(transformAPI);
|
77
|
+
}
|
61
78
|
|
62
|
-
|
63
|
-
|
79
|
+
// named export hooks will overrides hooks in default export function
|
80
|
+
Object.values(HOOKS).forEach(key => {
|
81
|
+
const fn = hooks[key];
|
82
|
+
if (fn) {
|
83
|
+
storage.hooks[key] = fn;
|
64
84
|
}
|
85
|
+
});
|
86
|
+
if (middleware) {
|
87
|
+
storage.middlewares = [].concat(middleware);
|
88
|
+
}
|
89
|
+
};
|
90
|
+
return {
|
91
|
+
prepare() {
|
92
|
+
loadMod();
|
65
93
|
},
|
66
|
-
|
67
94
|
reset() {
|
68
95
|
storage.reset();
|
69
|
-
|
70
|
-
|
71
|
-
if (newWebModule) {
|
72
|
-
newWebModule(transformAPI);
|
73
|
-
}
|
96
|
+
loadMod();
|
74
97
|
},
|
75
|
-
|
76
98
|
gather({
|
77
99
|
addWebMiddleware
|
78
100
|
}) {
|
@@ -80,53 +102,44 @@ var _default = () => ({
|
|
80
102
|
addWebMiddleware(mid);
|
81
103
|
});
|
82
104
|
},
|
83
|
-
|
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) {
|
105
|
+
afterMatch(context, next) {
|
98
106
|
if (!storage.hooks.afterMatch) {
|
99
107
|
return next();
|
100
108
|
}
|
101
|
-
|
102
|
-
context.router = routeAPI;
|
103
109
|
return storage.hooks.afterMatch(context, next);
|
104
110
|
},
|
105
|
-
|
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) {
|
111
|
+
afterRender(context, next) {
|
120
112
|
if (!storage.hooks.afterRender) {
|
121
113
|
return next();
|
122
114
|
}
|
123
|
-
|
124
|
-
context.template = templateAPI;
|
125
115
|
return storage.hooks.afterRender(context, next);
|
116
|
+
},
|
117
|
+
prepareWebServer({
|
118
|
+
config
|
119
|
+
}) {
|
120
|
+
const {
|
121
|
+
middleware
|
122
|
+
} = config;
|
123
|
+
const factory = compose(middleware);
|
124
|
+
return ctx => {
|
125
|
+
const {
|
126
|
+
source: {
|
127
|
+
res
|
128
|
+
}
|
129
|
+
} = ctx;
|
130
|
+
return new Promise((resolve, reject) => {
|
131
|
+
res.on('finish', err => {
|
132
|
+
if (err) {
|
133
|
+
return reject(err);
|
134
|
+
}
|
135
|
+
return resolve();
|
136
|
+
});
|
137
|
+
const dispatch = factory(ctx, resolve, reject);
|
138
|
+
dispatch();
|
139
|
+
});
|
140
|
+
};
|
126
141
|
}
|
127
|
-
|
128
142
|
};
|
129
143
|
}
|
130
144
|
});
|
131
|
-
|
132
145
|
exports.default = _default;
|
@@ -16,7 +16,6 @@ export default (function () {
|
|
16
16
|
afterBuild: function afterBuild() {
|
17
17
|
return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
18
18
|
var _api$useAppContext, appDirectory, distDirectory, modernConfig, distDir, serverDir, sharedDir, tsconfigPath, sourceDirs, server, _modernConfig$source, alias, envVars, globalVars, babel;
|
19
|
-
|
20
19
|
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
21
20
|
while (1) {
|
22
21
|
switch (_context.prev = _context.next) {
|
@@ -28,24 +27,19 @@ export default (function () {
|
|
28
27
|
sharedDir = path.resolve(appDirectory, SHARED_DIR);
|
29
28
|
tsconfigPath = path.resolve(appDirectory, TS_CONFIG_FILENAME);
|
30
29
|
sourceDirs = [];
|
31
|
-
|
32
30
|
if (fs.existsSync(serverDir)) {
|
33
31
|
sourceDirs.push(serverDir);
|
34
32
|
}
|
35
|
-
|
36
33
|
if (fs.existsSync(sharedDir)) {
|
37
34
|
sourceDirs.push(sharedDir);
|
38
35
|
}
|
39
|
-
|
40
36
|
server = modernConfig.server;
|
41
37
|
_modernConfig$source = modernConfig.source, alias = _modernConfig$source.alias, envVars = _modernConfig$source.envVars, globalVars = _modernConfig$source.globalVars;
|
42
38
|
babel = modernConfig.tools.babel;
|
43
|
-
|
44
39
|
if (!(sourceDirs.length > 0)) {
|
45
40
|
_context.next = 15;
|
46
41
|
break;
|
47
42
|
}
|
48
|
-
|
49
43
|
_context.next = 15;
|
50
44
|
return compile(appDirectory, {
|
51
45
|
server: server,
|
@@ -58,7 +52,6 @@ export default (function () {
|
|
58
52
|
distDir: distDir,
|
59
53
|
tsconfigPath: tsconfigPath
|
60
54
|
});
|
61
|
-
|
62
55
|
case 15:
|
63
56
|
case "end":
|
64
57
|
return _context.stop();
|
@@ -1,19 +1,22 @@
|
|
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';
|
7
|
-
|
9
|
+
var HOOKS;
|
10
|
+
(function (HOOKS) {
|
11
|
+
HOOKS["AFTER_MATCH"] = "afterMatch";
|
12
|
+
HOOKS["AFTER_RENDER"] = "afterRender";
|
13
|
+
})(HOOKS || (HOOKS = {}));
|
8
14
|
var Storage = /*#__PURE__*/function () {
|
9
15
|
function Storage() {
|
10
16
|
_classCallCheck(this, Storage);
|
11
|
-
|
12
17
|
_defineProperty(this, "middlewares", []);
|
13
|
-
|
14
18
|
_defineProperty(this, "hooks", {});
|
15
19
|
}
|
16
|
-
|
17
20
|
_createClass(Storage, [{
|
18
21
|
key: "reset",
|
19
22
|
value: function reset() {
|
@@ -21,10 +24,8 @@ var Storage = /*#__PURE__*/function () {
|
|
21
24
|
this.hooks = {};
|
22
25
|
}
|
23
26
|
}]);
|
24
|
-
|
25
27
|
return Storage;
|
26
28
|
}();
|
27
|
-
|
28
29
|
var createTransformAPI = function createTransformAPI(storage) {
|
29
30
|
return new Proxy({}, {
|
30
31
|
get: function get(target, name) {
|
@@ -33,43 +34,67 @@ var createTransformAPI = function createTransformAPI(storage) {
|
|
33
34
|
return storage.middlewares.push(fn);
|
34
35
|
};
|
35
36
|
}
|
36
|
-
|
37
37
|
return function (fn) {
|
38
|
-
|
38
|
+
logger.warn('declare hook in default export is to be deprecated, use named export instead');
|
39
|
+
storage.hooks[name] = fn;
|
39
40
|
};
|
40
41
|
}
|
41
42
|
});
|
42
43
|
};
|
43
|
-
|
44
|
+
var compose = function compose(middlewares) {
|
45
|
+
return function (ctx, resolve, reject) {
|
46
|
+
var i = 0;
|
47
|
+
var dispatch = function dispatch() {
|
48
|
+
var _handler, _handler$catch;
|
49
|
+
var handler = middlewares[i++];
|
50
|
+
if (!handler) {
|
51
|
+
return resolve();
|
52
|
+
}
|
53
|
+
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);
|
54
|
+
};
|
55
|
+
return dispatch;
|
56
|
+
};
|
57
|
+
};
|
44
58
|
export default (function () {
|
45
59
|
return {
|
46
60
|
name: '@modern-js/plugin-server',
|
47
61
|
setup: function setup(api) {
|
48
62
|
var _api$useAppContext = api.useAppContext(),
|
49
|
-
|
50
|
-
|
51
|
-
|
63
|
+
appDirectory = _api$useAppContext.appDirectory,
|
64
|
+
distDirectory = _api$useAppContext.distDirectory;
|
52
65
|
var storage = new Storage();
|
53
66
|
var transformAPI = createTransformAPI(storage);
|
54
|
-
var
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
67
|
+
var pwd = isProd() ? distDirectory : appDirectory;
|
68
|
+
var webAppPath = path.resolve(pwd, SERVER_DIR, WEB_APP_NAME);
|
69
|
+
var loadMod = function loadMod() {
|
70
|
+
var mod = requireExistModule(webAppPath, {
|
71
|
+
interop: false
|
72
|
+
});
|
73
|
+
var defaultExports = mod["default"],
|
74
|
+
middleware = mod.middleware,
|
75
|
+
hooks = _objectWithoutProperties(mod, _excluded);
|
76
|
+
if (defaultExports) {
|
77
|
+
defaultExports(transformAPI);
|
78
|
+
}
|
61
79
|
|
62
|
-
|
63
|
-
|
80
|
+
// named export hooks will overrides hooks in default export function
|
81
|
+
Object.values(HOOKS).forEach(function (key) {
|
82
|
+
var fn = hooks[key];
|
83
|
+
if (fn) {
|
84
|
+
storage.hooks[key] = fn;
|
64
85
|
}
|
86
|
+
});
|
87
|
+
if (middleware) {
|
88
|
+
storage.middlewares = [].concat(middleware);
|
89
|
+
}
|
90
|
+
};
|
91
|
+
return {
|
92
|
+
prepare: function prepare() {
|
93
|
+
loadMod();
|
65
94
|
},
|
66
95
|
reset: function reset() {
|
67
96
|
storage.reset();
|
68
|
-
|
69
|
-
|
70
|
-
if (newWebModule) {
|
71
|
-
newWebModule(transformAPI);
|
72
|
-
}
|
97
|
+
loadMod();
|
73
98
|
},
|
74
99
|
gather: function gather(_ref) {
|
75
100
|
var addWebMiddleware = _ref.addWebMiddleware;
|
@@ -77,45 +102,35 @@ export default (function () {
|
|
77
102
|
addWebMiddleware(mid);
|
78
103
|
});
|
79
104
|
},
|
80
|
-
|
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
|
-
|
105
|
+
afterMatch: function afterMatch(context, next) {
|
93
106
|
if (!storage.hooks.afterMatch) {
|
94
107
|
return next();
|
95
108
|
}
|
96
|
-
|
97
|
-
context.router = routeAPI;
|
98
109
|
return storage.hooks.afterMatch(context, next);
|
99
110
|
},
|
100
|
-
|
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
|
-
|
111
|
+
afterRender: function afterRender(context, next) {
|
113
112
|
if (!storage.hooks.afterRender) {
|
114
113
|
return next();
|
115
114
|
}
|
116
|
-
|
117
|
-
context.template = templateAPI;
|
118
115
|
return storage.hooks.afterRender(context, next);
|
116
|
+
},
|
117
|
+
prepareWebServer: function prepareWebServer(_ref2) {
|
118
|
+
var config = _ref2.config;
|
119
|
+
var middleware = config.middleware;
|
120
|
+
var factory = compose(middleware);
|
121
|
+
return function (ctx) {
|
122
|
+
var res = ctx.source.res;
|
123
|
+
return new Promise(function (resolve, reject) {
|
124
|
+
res.on('finish', function (err) {
|
125
|
+
if (err) {
|
126
|
+
return reject(err);
|
127
|
+
}
|
128
|
+
return resolve();
|
129
|
+
});
|
130
|
+
var dispatch = factory(ctx, resolve, reject);
|
131
|
+
dispatch();
|
132
|
+
});
|
133
|
+
};
|
119
134
|
}
|
120
135
|
};
|
121
136
|
}
|
package/dist/types/cli.d.ts
CHANGED
package/dist/types/server.d.ts
CHANGED
package/package.json
CHANGED
@@ -11,7 +11,7 @@
|
|
11
11
|
"modern",
|
12
12
|
"modern.js"
|
13
13
|
],
|
14
|
-
"version": "
|
14
|
+
"version": "2.0.0-beta.1",
|
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": "
|
52
|
-
"@modern-js/server-utils": "
|
53
|
-
"@modern-js/utils": "
|
51
|
+
"@modern-js/babel-compiler": "2.0.0-beta.1",
|
52
|
+
"@modern-js/server-utils": "2.0.0-beta.1",
|
53
|
+
"@modern-js/utils": "2.0.0-beta.1"
|
54
54
|
},
|
55
55
|
"devDependencies": {
|
56
56
|
"typescript": "^4",
|
57
57
|
"@types/jest": "^27",
|
58
58
|
"@types/node": "^14",
|
59
59
|
"jest": "^27",
|
60
|
-
"@modern-js/server-core": "
|
61
|
-
"@modern-js/core": "
|
62
|
-
"@scripts/build": "
|
63
|
-
"@modern-js/types": "
|
64
|
-
"@scripts/jest-config": "
|
60
|
+
"@modern-js/server-core": "2.0.0-beta.1",
|
61
|
+
"@modern-js/core": "2.0.0-beta.1",
|
62
|
+
"@scripts/build": "2.0.0-beta.1",
|
63
|
+
"@modern-js/types": "2.0.0-beta.1",
|
64
|
+
"@scripts/jest-config": "2.0.0-beta.1"
|
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
|
101
|
-
"build": "
|
77
|
+
"dev": "modern build --watch",
|
78
|
+
"build": "modern build",
|
102
79
|
"new": "modern new",
|
103
|
-
"test": "
|
80
|
+
"test": "jest --passWithNoTests"
|
104
81
|
}
|
105
82
|
}
|