@modern-js/plugin-server 1.1.1 → 1.1.4
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/.eslintrc.js +7 -0
- package/CHANGELOG.md +34 -0
- package/dist/js/modern/cli.js +38 -40
- package/dist/js/modern/server.js +72 -73
- package/dist/js/node/cli.js +46 -49
- package/dist/js/node/server.js +70 -73
- package/dist/js/treeshaking/cli.js +52 -52
- package/dist/js/treeshaking/server.js +61 -60
- package/dist/types/cli.d.ts +3 -1
- package/dist/types/server.d.ts +3 -1
- package/jest.config.js +0 -1
- package/package.json +19 -10
- package/tests/.eslintrc.js +6 -0
- package/tests/fixtures/foo/server/index.ts +23 -0
- package/tests/index.test.ts +39 -0
- package/tests/tsconfig.json +9 -0
package/.eslintrc.js
ADDED
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,39 @@
|
|
1
1
|
# @modern-js/plugin-server
|
2
2
|
|
3
|
+
## 1.1.4
|
4
|
+
|
5
|
+
### Patch Changes
|
6
|
+
|
7
|
+
- 18db013a: support load plugin instace
|
8
|
+
- Updated dependencies [c2046f37]
|
9
|
+
- @modern-js/utils@1.3.6
|
10
|
+
|
11
|
+
## 1.1.3
|
12
|
+
|
13
|
+
### Patch Changes
|
14
|
+
|
15
|
+
- c7dc7f54: migrate to new plugin style
|
16
|
+
- 2008fdbd: convert two packages server part, support server load plugin itself
|
17
|
+
- Updated dependencies [5bf5868d]
|
18
|
+
- @modern-js/utils@1.3.5
|
19
|
+
|
20
|
+
## 1.1.2
|
21
|
+
|
22
|
+
### Patch Changes
|
23
|
+
|
24
|
+
- 272cab15: refactor server plugin manager
|
25
|
+
- Updated dependencies [d9cc5ea9]
|
26
|
+
- Updated dependencies [bd819a8d]
|
27
|
+
- Updated dependencies [ec4dbffb]
|
28
|
+
- Updated dependencies [d099e5c5]
|
29
|
+
- Updated dependencies [bada2879]
|
30
|
+
- Updated dependencies [24f616ca]
|
31
|
+
- Updated dependencies [bd819a8d]
|
32
|
+
- Updated dependencies [272cab15]
|
33
|
+
- @modern-js/core@1.4.0
|
34
|
+
- @modern-js/utils@1.3.0
|
35
|
+
- @modern-js/server-core@1.2.2
|
36
|
+
|
3
37
|
## 1.1.1
|
4
38
|
|
5
39
|
### Patch Changes
|
package/dist/js/modern/cli.js
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
import fs from 'fs';
|
2
2
|
import path from 'path';
|
3
|
-
import { createPlugin, useAppContext, useResolvedConfigContext } from '@modern-js/core';
|
4
3
|
import { compiler } from '@modern-js/babel-compiler';
|
5
4
|
import { resolveBabelConfig } from '@modern-js/server-utils';
|
6
5
|
import { SHARED_DIR, SERVER_DIR } from '@modern-js/utils';
|
@@ -36,48 +35,47 @@ const compile = async (appDirectory, modernConfig, compileOptions) => {
|
|
36
35
|
});
|
37
36
|
};
|
38
37
|
|
39
|
-
export default
|
40
|
-
|
41
|
-
|
42
|
-
|
38
|
+
export default (() => ({
|
39
|
+
name: '@modern-js/plugin-server',
|
40
|
+
setup: api => ({
|
41
|
+
config() {
|
42
|
+
return {};
|
43
|
+
},
|
43
44
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
const tsconfigPath = path.resolve(appDirectory, TS_CONFIG_FILENAME);
|
56
|
-
const patterns = [];
|
45
|
+
async afterBuild() {
|
46
|
+
const {
|
47
|
+
appDirectory,
|
48
|
+
distDirectory
|
49
|
+
} = api.useAppContext();
|
50
|
+
const modernConfig = api.useResolvedConfigContext();
|
51
|
+
const distDir = path.resolve(distDirectory);
|
52
|
+
const serverDir = path.resolve(appDirectory, SERVER_DIR);
|
53
|
+
const sharedDir = path.resolve(appDirectory, SHARED_DIR);
|
54
|
+
const tsconfigPath = path.resolve(appDirectory, TS_CONFIG_FILENAME);
|
55
|
+
const patterns = [];
|
57
56
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
57
|
+
if (fs.existsSync(serverDir)) {
|
58
|
+
patterns.push({
|
59
|
+
from: serverDir,
|
60
|
+
to: distDir,
|
61
|
+
tsconfigPath
|
62
|
+
});
|
63
|
+
}
|
65
64
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
65
|
+
if (fs.existsSync(sharedDir)) {
|
66
|
+
patterns.push({
|
67
|
+
from: sharedDir,
|
68
|
+
to: distDir,
|
69
|
+
tsconfigPath
|
70
|
+
});
|
71
|
+
}
|
73
72
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
73
|
+
if (patterns.length > 0) {
|
74
|
+
await compile(appDirectory, modernConfig, {
|
75
|
+
patterns
|
76
|
+
});
|
77
|
+
}
|
78
78
|
}
|
79
|
-
}
|
80
79
|
|
81
|
-
})
|
82
|
-
|
83
|
-
});
|
80
|
+
})
|
81
|
+
}));
|
package/dist/js/modern/server.js
CHANGED
@@ -1,6 +1,4 @@
|
|
1
1
|
import path from 'path';
|
2
|
-
import { createPlugin } from '@modern-js/server-plugin';
|
3
|
-
import { useAppContext } from '@modern-js/core';
|
4
2
|
import { isProd, requireExistModule, SERVER_DIR } from '@modern-js/utils';
|
5
3
|
const WEB_APP_NAME = 'index';
|
6
4
|
|
@@ -28,77 +26,78 @@ const createTransformAPI = storage => new Proxy({}, {
|
|
28
26
|
|
29
27
|
});
|
30
28
|
|
31
|
-
export default
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
reset() {
|
49
|
-
storage.reset();
|
50
|
-
const newWebModule = requireExistModule(webAppPath);
|
29
|
+
export default (() => ({
|
30
|
+
name: '@modern-js/plugin-server',
|
31
|
+
setup: api => {
|
32
|
+
const {
|
33
|
+
appDirectory,
|
34
|
+
distDirectory
|
35
|
+
} = api.useAppContext();
|
36
|
+
const pwd = isProd() ? distDirectory : appDirectory;
|
37
|
+
const serverPath = path.resolve(pwd, SERVER_DIR);
|
38
|
+
const webAppPath = path.resolve(serverPath, WEB_APP_NAME);
|
39
|
+
const storage = new Storage();
|
40
|
+
const transformAPI = createTransformAPI(storage);
|
41
|
+
const webMod = requireExistModule(webAppPath);
|
42
|
+
|
43
|
+
if (webMod) {
|
44
|
+
webMod(transformAPI);
|
45
|
+
}
|
51
46
|
|
52
|
-
|
53
|
-
|
47
|
+
return {
|
48
|
+
reset() {
|
49
|
+
storage.reset();
|
50
|
+
const newWebModule = requireExistModule(webAppPath);
|
51
|
+
|
52
|
+
if (newWebModule) {
|
53
|
+
newWebModule(transformAPI);
|
54
|
+
}
|
55
|
+
},
|
56
|
+
|
57
|
+
gather({
|
58
|
+
addWebMiddleware
|
59
|
+
}) {
|
60
|
+
storage.middlewares.forEach(mid => {
|
61
|
+
addWebMiddleware(mid);
|
62
|
+
});
|
63
|
+
},
|
64
|
+
|
65
|
+
beforeMatch({
|
66
|
+
context
|
67
|
+
}, next) {
|
68
|
+
var _storage$hooks$before, _storage$hooks;
|
69
|
+
|
70
|
+
return (_storage$hooks$before = (_storage$hooks = storage.hooks).beforeMatch) === null || _storage$hooks$before === void 0 ? void 0 : _storage$hooks$before.call(_storage$hooks, context, next);
|
71
|
+
},
|
72
|
+
|
73
|
+
afterMatch({
|
74
|
+
context,
|
75
|
+
routeAPI
|
76
|
+
}, next) {
|
77
|
+
var _storage$hooks$afterM, _storage$hooks2;
|
78
|
+
|
79
|
+
context.router = routeAPI;
|
80
|
+
return (_storage$hooks$afterM = (_storage$hooks2 = storage.hooks).afterMatch) === null || _storage$hooks$afterM === void 0 ? void 0 : _storage$hooks$afterM.call(_storage$hooks2, context, next);
|
81
|
+
},
|
82
|
+
|
83
|
+
beforeRender({
|
84
|
+
context
|
85
|
+
}, next) {
|
86
|
+
var _storage$hooks$before2, _storage$hooks3;
|
87
|
+
|
88
|
+
return (_storage$hooks$before2 = (_storage$hooks3 = storage.hooks).beforeRender) === null || _storage$hooks$before2 === void 0 ? void 0 : _storage$hooks$before2.call(_storage$hooks3, context, next);
|
89
|
+
},
|
90
|
+
|
91
|
+
afterRender({
|
92
|
+
context,
|
93
|
+
templateAPI
|
94
|
+
}, next) {
|
95
|
+
var _storage$hooks$afterR, _storage$hooks4;
|
96
|
+
|
97
|
+
context.template = templateAPI;
|
98
|
+
return (_storage$hooks$afterR = (_storage$hooks4 = storage.hooks).afterRender) === null || _storage$hooks$afterR === void 0 ? void 0 : _storage$hooks$afterR.call(_storage$hooks4, context, next);
|
54
99
|
}
|
55
|
-
},
|
56
|
-
|
57
|
-
gather({
|
58
|
-
addWebMiddleware
|
59
|
-
}) {
|
60
|
-
storage.middlewares.forEach(mid => {
|
61
|
-
addWebMiddleware(mid);
|
62
|
-
});
|
63
|
-
},
|
64
|
-
|
65
|
-
beforeMatch({
|
66
|
-
context
|
67
|
-
}, next) {
|
68
|
-
var _storage$hooks$before, _storage$hooks;
|
69
|
-
|
70
|
-
return (_storage$hooks$before = (_storage$hooks = storage.hooks).beforeMatch) === null || _storage$hooks$before === void 0 ? void 0 : _storage$hooks$before.call(_storage$hooks, context, next);
|
71
|
-
},
|
72
|
-
|
73
|
-
afterMatch({
|
74
|
-
context,
|
75
|
-
routeAPI
|
76
|
-
}, next) {
|
77
|
-
var _storage$hooks$afterM, _storage$hooks2;
|
78
|
-
|
79
|
-
context.router = routeAPI;
|
80
|
-
return (_storage$hooks$afterM = (_storage$hooks2 = storage.hooks).afterMatch) === null || _storage$hooks$afterM === void 0 ? void 0 : _storage$hooks$afterM.call(_storage$hooks2, context, next);
|
81
|
-
},
|
82
|
-
|
83
|
-
beforeRender({
|
84
|
-
context
|
85
|
-
}, next) {
|
86
|
-
var _storage$hooks$before2, _storage$hooks3;
|
87
|
-
|
88
|
-
return (_storage$hooks$before2 = (_storage$hooks3 = storage.hooks).beforeRender) === null || _storage$hooks$before2 === void 0 ? void 0 : _storage$hooks$before2.call(_storage$hooks3, context, next);
|
89
|
-
},
|
90
|
-
|
91
|
-
afterRender({
|
92
|
-
context,
|
93
|
-
templateAPI
|
94
|
-
}, next) {
|
95
|
-
var _storage$hooks$afterR, _storage$hooks4;
|
96
|
-
|
97
|
-
context.template = templateAPI;
|
98
|
-
return (_storage$hooks$afterR = (_storage$hooks4 = storage.hooks).afterRender) === null || _storage$hooks$afterR === void 0 ? void 0 : _storage$hooks$afterR.call(_storage$hooks4, context, next);
|
99
|
-
}
|
100
100
|
|
101
|
-
|
102
|
-
}
|
103
|
-
|
104
|
-
});
|
101
|
+
};
|
102
|
+
}
|
103
|
+
}));
|
package/dist/js/node/cli.js
CHANGED
@@ -9,8 +9,6 @@ var _fs = _interopRequireDefault(require("fs"));
|
|
9
9
|
|
10
10
|
var _path = _interopRequireDefault(require("path"));
|
11
11
|
|
12
|
-
var _core = require("@modern-js/core");
|
13
|
-
|
14
12
|
var _babelCompiler = require("@modern-js/babel-compiler");
|
15
13
|
|
16
14
|
var _serverUtils = require("@modern-js/server-utils");
|
@@ -51,55 +49,54 @@ const compile = async (appDirectory, modernConfig, compileOptions) => {
|
|
51
49
|
});
|
52
50
|
};
|
53
51
|
|
54
|
-
var _default = (
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
52
|
+
var _default = () => ({
|
53
|
+
name: '@modern-js/plugin-server',
|
54
|
+
setup: api => ({
|
55
|
+
config() {
|
56
|
+
return {};
|
57
|
+
},
|
58
|
+
|
59
|
+
async afterBuild() {
|
60
|
+
const {
|
61
|
+
appDirectory,
|
62
|
+
distDirectory
|
63
|
+
} = api.useAppContext();
|
64
|
+
const modernConfig = api.useResolvedConfigContext();
|
65
|
+
|
66
|
+
const distDir = _path.default.resolve(distDirectory);
|
67
|
+
|
68
|
+
const serverDir = _path.default.resolve(appDirectory, _utils.SERVER_DIR);
|
69
|
+
|
70
|
+
const sharedDir = _path.default.resolve(appDirectory, _utils.SHARED_DIR);
|
71
|
+
|
72
|
+
const tsconfigPath = _path.default.resolve(appDirectory, TS_CONFIG_FILENAME);
|
73
|
+
|
74
|
+
const patterns = [];
|
75
|
+
|
76
|
+
if (_fs.default.existsSync(serverDir)) {
|
77
|
+
patterns.push({
|
78
|
+
from: serverDir,
|
79
|
+
to: distDir,
|
80
|
+
tsconfigPath
|
81
|
+
});
|
82
|
+
}
|
83
|
+
|
84
|
+
if (_fs.default.existsSync(sharedDir)) {
|
85
|
+
patterns.push({
|
86
|
+
from: sharedDir,
|
87
|
+
to: distDir,
|
88
|
+
tsconfigPath
|
89
|
+
});
|
90
|
+
}
|
91
|
+
|
92
|
+
if (patterns.length > 0) {
|
93
|
+
await compile(appDirectory, modernConfig, {
|
94
|
+
patterns
|
95
|
+
});
|
96
|
+
}
|
98
97
|
}
|
99
|
-
}
|
100
98
|
|
101
|
-
})
|
102
|
-
name: '@modern-js/plugin-server'
|
99
|
+
})
|
103
100
|
});
|
104
101
|
|
105
102
|
exports.default = _default;
|
package/dist/js/node/server.js
CHANGED
@@ -7,10 +7,6 @@ exports.default = void 0;
|
|
7
7
|
|
8
8
|
var _path = _interopRequireDefault(require("path"));
|
9
9
|
|
10
|
-
var _serverPlugin = require("@modern-js/server-plugin");
|
11
|
-
|
12
|
-
var _core = require("@modern-js/core");
|
13
|
-
|
14
10
|
var _utils = require("@modern-js/utils");
|
15
11
|
|
16
12
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
@@ -41,82 +37,83 @@ const createTransformAPI = storage => new Proxy({}, {
|
|
41
37
|
|
42
38
|
});
|
43
39
|
|
44
|
-
var _default = (
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
40
|
+
var _default = () => ({
|
41
|
+
name: '@modern-js/plugin-server',
|
42
|
+
setup: api => {
|
43
|
+
const {
|
44
|
+
appDirectory,
|
45
|
+
distDirectory
|
46
|
+
} = api.useAppContext();
|
47
|
+
const pwd = (0, _utils.isProd)() ? distDirectory : appDirectory;
|
50
48
|
|
51
|
-
|
49
|
+
const serverPath = _path.default.resolve(pwd, _utils.SERVER_DIR);
|
52
50
|
|
53
|
-
|
51
|
+
const webAppPath = _path.default.resolve(serverPath, WEB_APP_NAME);
|
54
52
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
if (webMod) {
|
60
|
-
webMod(transformAPI);
|
61
|
-
}
|
53
|
+
const storage = new Storage();
|
54
|
+
const transformAPI = createTransformAPI(storage);
|
55
|
+
const webMod = (0, _utils.requireExistModule)(webAppPath);
|
62
56
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
const newWebModule = (0, _utils.requireExistModule)(webAppPath);
|
57
|
+
if (webMod) {
|
58
|
+
webMod(transformAPI);
|
59
|
+
}
|
67
60
|
|
68
|
-
|
69
|
-
|
61
|
+
return {
|
62
|
+
reset() {
|
63
|
+
storage.reset();
|
64
|
+
const newWebModule = (0, _utils.requireExistModule)(webAppPath);
|
65
|
+
|
66
|
+
if (newWebModule) {
|
67
|
+
newWebModule(transformAPI);
|
68
|
+
}
|
69
|
+
},
|
70
|
+
|
71
|
+
gather({
|
72
|
+
addWebMiddleware
|
73
|
+
}) {
|
74
|
+
storage.middlewares.forEach(mid => {
|
75
|
+
addWebMiddleware(mid);
|
76
|
+
});
|
77
|
+
},
|
78
|
+
|
79
|
+
beforeMatch({
|
80
|
+
context
|
81
|
+
}, next) {
|
82
|
+
var _storage$hooks$before, _storage$hooks;
|
83
|
+
|
84
|
+
return (_storage$hooks$before = (_storage$hooks = storage.hooks).beforeMatch) === null || _storage$hooks$before === void 0 ? void 0 : _storage$hooks$before.call(_storage$hooks, context, next);
|
85
|
+
},
|
86
|
+
|
87
|
+
afterMatch({
|
88
|
+
context,
|
89
|
+
routeAPI
|
90
|
+
}, next) {
|
91
|
+
var _storage$hooks$afterM, _storage$hooks2;
|
92
|
+
|
93
|
+
context.router = routeAPI;
|
94
|
+
return (_storage$hooks$afterM = (_storage$hooks2 = storage.hooks).afterMatch) === null || _storage$hooks$afterM === void 0 ? void 0 : _storage$hooks$afterM.call(_storage$hooks2, context, next);
|
95
|
+
},
|
96
|
+
|
97
|
+
beforeRender({
|
98
|
+
context
|
99
|
+
}, next) {
|
100
|
+
var _storage$hooks$before2, _storage$hooks3;
|
101
|
+
|
102
|
+
return (_storage$hooks$before2 = (_storage$hooks3 = storage.hooks).beforeRender) === null || _storage$hooks$before2 === void 0 ? void 0 : _storage$hooks$before2.call(_storage$hooks3, context, next);
|
103
|
+
},
|
104
|
+
|
105
|
+
afterRender({
|
106
|
+
context,
|
107
|
+
templateAPI
|
108
|
+
}, next) {
|
109
|
+
var _storage$hooks$afterR, _storage$hooks4;
|
110
|
+
|
111
|
+
context.template = templateAPI;
|
112
|
+
return (_storage$hooks$afterR = (_storage$hooks4 = storage.hooks).afterRender) === null || _storage$hooks$afterR === void 0 ? void 0 : _storage$hooks$afterR.call(_storage$hooks4, context, next);
|
70
113
|
}
|
71
|
-
},
|
72
|
-
|
73
|
-
gather({
|
74
|
-
addWebMiddleware
|
75
|
-
}) {
|
76
|
-
storage.middlewares.forEach(mid => {
|
77
|
-
addWebMiddleware(mid);
|
78
|
-
});
|
79
|
-
},
|
80
|
-
|
81
|
-
beforeMatch({
|
82
|
-
context
|
83
|
-
}, next) {
|
84
|
-
var _storage$hooks$before, _storage$hooks;
|
85
|
-
|
86
|
-
return (_storage$hooks$before = (_storage$hooks = storage.hooks).beforeMatch) === null || _storage$hooks$before === void 0 ? void 0 : _storage$hooks$before.call(_storage$hooks, context, next);
|
87
|
-
},
|
88
|
-
|
89
|
-
afterMatch({
|
90
|
-
context,
|
91
|
-
routeAPI
|
92
|
-
}, next) {
|
93
|
-
var _storage$hooks$afterM, _storage$hooks2;
|
94
|
-
|
95
|
-
context.router = routeAPI;
|
96
|
-
return (_storage$hooks$afterM = (_storage$hooks2 = storage.hooks).afterMatch) === null || _storage$hooks$afterM === void 0 ? void 0 : _storage$hooks$afterM.call(_storage$hooks2, context, next);
|
97
|
-
},
|
98
|
-
|
99
|
-
beforeRender({
|
100
|
-
context
|
101
|
-
}, next) {
|
102
|
-
var _storage$hooks$before2, _storage$hooks3;
|
103
|
-
|
104
|
-
return (_storage$hooks$before2 = (_storage$hooks3 = storage.hooks).beforeRender) === null || _storage$hooks$before2 === void 0 ? void 0 : _storage$hooks$before2.call(_storage$hooks3, context, next);
|
105
|
-
},
|
106
|
-
|
107
|
-
afterRender({
|
108
|
-
context,
|
109
|
-
templateAPI
|
110
|
-
}, next) {
|
111
|
-
var _storage$hooks$afterR, _storage$hooks4;
|
112
|
-
|
113
|
-
context.template = templateAPI;
|
114
|
-
return (_storage$hooks$afterR = (_storage$hooks4 = storage.hooks).afterRender) === null || _storage$hooks$afterR === void 0 ? void 0 : _storage$hooks$afterR.call(_storage$hooks4, context, next);
|
115
|
-
}
|
116
114
|
|
117
|
-
|
118
|
-
}
|
119
|
-
name: '@modern-js/plugin-server'
|
115
|
+
};
|
116
|
+
}
|
120
117
|
});
|
121
118
|
|
122
119
|
exports.default = _default;
|
@@ -6,7 +6,6 @@ function _asyncToGenerator(fn) { return function () { var self = this, args = ar
|
|
6
6
|
|
7
7
|
import fs from 'fs';
|
8
8
|
import path from 'path';
|
9
|
-
import { createPlugin, useAppContext, useResolvedConfigContext } from '@modern-js/core';
|
10
9
|
import { compiler } from '@modern-js/babel-compiler';
|
11
10
|
import { resolveBabelConfig } from '@modern-js/server-utils';
|
12
11
|
import { SHARED_DIR, SERVER_DIR } from '@modern-js/utils';
|
@@ -60,64 +59,65 @@ var compile = /*#__PURE__*/function () {
|
|
60
59
|
};
|
61
60
|
}();
|
62
61
|
|
63
|
-
export default
|
62
|
+
export default (function () {
|
64
63
|
return {
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
64
|
+
name: '@modern-js/plugin-server',
|
65
|
+
setup: function setup(api) {
|
66
|
+
return {
|
67
|
+
config: function config() {
|
68
|
+
return {};
|
69
|
+
},
|
70
|
+
afterBuild: function afterBuild() {
|
71
|
+
return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
|
72
|
+
var _api$useAppContext, appDirectory, distDirectory, modernConfig, distDir, serverDir, sharedDir, tsconfigPath, patterns;
|
71
73
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
74
|
+
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
|
75
|
+
while (1) {
|
76
|
+
switch (_context2.prev = _context2.next) {
|
77
|
+
case 0:
|
78
|
+
_api$useAppContext = api.useAppContext(), appDirectory = _api$useAppContext.appDirectory, distDirectory = _api$useAppContext.distDirectory;
|
79
|
+
modernConfig = api.useResolvedConfigContext();
|
80
|
+
distDir = path.resolve(distDirectory);
|
81
|
+
serverDir = path.resolve(appDirectory, SERVER_DIR);
|
82
|
+
sharedDir = path.resolve(appDirectory, SHARED_DIR);
|
83
|
+
tsconfigPath = path.resolve(appDirectory, TS_CONFIG_FILENAME);
|
84
|
+
patterns = [];
|
78
85
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
86
|
+
if (fs.existsSync(serverDir)) {
|
87
|
+
patterns.push({
|
88
|
+
from: serverDir,
|
89
|
+
to: distDir,
|
90
|
+
tsconfigPath: tsconfigPath
|
91
|
+
});
|
92
|
+
}
|
85
93
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
if (fs.existsSync(sharedDir)) {
|
95
|
-
patterns.push({
|
96
|
-
from: sharedDir,
|
97
|
-
to: distDir,
|
98
|
-
tsconfigPath: tsconfigPath
|
99
|
-
});
|
100
|
-
}
|
94
|
+
if (fs.existsSync(sharedDir)) {
|
95
|
+
patterns.push({
|
96
|
+
from: sharedDir,
|
97
|
+
to: distDir,
|
98
|
+
tsconfigPath: tsconfigPath
|
99
|
+
});
|
100
|
+
}
|
101
101
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
102
|
+
if (!(patterns.length > 0)) {
|
103
|
+
_context2.next = 12;
|
104
|
+
break;
|
105
|
+
}
|
106
106
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
107
|
+
_context2.next = 12;
|
108
|
+
return compile(appDirectory, modernConfig, {
|
109
|
+
patterns: patterns
|
110
|
+
});
|
111
111
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
112
|
+
case 12:
|
113
|
+
case "end":
|
114
|
+
return _context2.stop();
|
115
|
+
}
|
116
|
+
}
|
117
|
+
}, _callee2);
|
118
|
+
}))();
|
119
|
+
}
|
120
|
+
};
|
119
121
|
}
|
120
122
|
};
|
121
|
-
}, {
|
122
|
-
name: '@modern-js/plugin-server'
|
123
123
|
});
|
@@ -5,8 +5,6 @@ function _defineProperties(target, props) { for (var i = 0; i < props.length; i+
|
|
5
5
|
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
6
6
|
|
7
7
|
import path from 'path';
|
8
|
-
import { createPlugin } from '@modern-js/server-plugin';
|
9
|
-
import { useAppContext } from '@modern-js/core';
|
10
8
|
import { isProd, requireExistModule, SERVER_DIR } from '@modern-js/utils';
|
11
9
|
var WEB_APP_NAME = 'index';
|
12
10
|
|
@@ -45,66 +43,69 @@ var createTransformAPI = function createTransformAPI(storage) {
|
|
45
43
|
});
|
46
44
|
};
|
47
45
|
|
48
|
-
export default
|
49
|
-
var _useAppContext = useAppContext(),
|
50
|
-
appDirectory = _useAppContext.appDirectory,
|
51
|
-
distDirectory = _useAppContext.distDirectory;
|
52
|
-
|
53
|
-
var pwd = isProd() ? distDirectory : appDirectory;
|
54
|
-
var serverPath = path.resolve(pwd, SERVER_DIR);
|
55
|
-
var webAppPath = path.resolve(serverPath, WEB_APP_NAME);
|
56
|
-
var storage = new Storage();
|
57
|
-
var transformAPI = createTransformAPI(storage);
|
58
|
-
var webMod = requireExistModule(webAppPath);
|
59
|
-
|
60
|
-
if (webMod) {
|
61
|
-
webMod(transformAPI);
|
62
|
-
}
|
63
|
-
|
46
|
+
export default (function () {
|
64
47
|
return {
|
65
|
-
|
66
|
-
|
67
|
-
var
|
68
|
-
|
69
|
-
|
70
|
-
|
48
|
+
name: '@modern-js/plugin-server',
|
49
|
+
setup: function setup(api) {
|
50
|
+
var _api$useAppContext = api.useAppContext(),
|
51
|
+
appDirectory = _api$useAppContext.appDirectory,
|
52
|
+
distDirectory = _api$useAppContext.distDirectory;
|
53
|
+
|
54
|
+
var pwd = isProd() ? distDirectory : appDirectory;
|
55
|
+
var serverPath = path.resolve(pwd, SERVER_DIR);
|
56
|
+
var webAppPath = path.resolve(serverPath, WEB_APP_NAME);
|
57
|
+
var storage = new Storage();
|
58
|
+
var transformAPI = createTransformAPI(storage);
|
59
|
+
var webMod = requireExistModule(webAppPath);
|
60
|
+
|
61
|
+
if (webMod) {
|
62
|
+
webMod(transformAPI);
|
71
63
|
}
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
64
|
+
|
65
|
+
return {
|
66
|
+
reset: function reset() {
|
67
|
+
storage.reset();
|
68
|
+
var newWebModule = requireExistModule(webAppPath);
|
69
|
+
|
70
|
+
if (newWebModule) {
|
71
|
+
newWebModule(transformAPI);
|
72
|
+
}
|
73
|
+
},
|
74
|
+
gather: function gather(_ref) {
|
75
|
+
var addWebMiddleware = _ref.addWebMiddleware;
|
76
|
+
storage.middlewares.forEach(function (mid) {
|
77
|
+
addWebMiddleware(mid);
|
78
|
+
});
|
79
|
+
},
|
80
|
+
beforeMatch: function beforeMatch(_ref2, next) {
|
81
|
+
var _storage$hooks$before, _storage$hooks;
|
82
|
+
|
83
|
+
var context = _ref2.context;
|
84
|
+
return (_storage$hooks$before = (_storage$hooks = storage.hooks).beforeMatch) === null || _storage$hooks$before === void 0 ? void 0 : _storage$hooks$before.call(_storage$hooks, context, next);
|
85
|
+
},
|
86
|
+
afterMatch: function afterMatch(_ref3, next) {
|
87
|
+
var _storage$hooks$afterM, _storage$hooks2;
|
88
|
+
|
89
|
+
var context = _ref3.context,
|
90
|
+
routeAPI = _ref3.routeAPI;
|
91
|
+
context.router = routeAPI;
|
92
|
+
return (_storage$hooks$afterM = (_storage$hooks2 = storage.hooks).afterMatch) === null || _storage$hooks$afterM === void 0 ? void 0 : _storage$hooks$afterM.call(_storage$hooks2, context, next);
|
93
|
+
},
|
94
|
+
beforeRender: function beforeRender(_ref4, next) {
|
95
|
+
var _storage$hooks$before2, _storage$hooks3;
|
96
|
+
|
97
|
+
var context = _ref4.context;
|
98
|
+
return (_storage$hooks$before2 = (_storage$hooks3 = storage.hooks).beforeRender) === null || _storage$hooks$before2 === void 0 ? void 0 : _storage$hooks$before2.call(_storage$hooks3, context, next);
|
99
|
+
},
|
100
|
+
afterRender: function afterRender(_ref5, next) {
|
101
|
+
var _storage$hooks$afterR, _storage$hooks4;
|
102
|
+
|
103
|
+
var context = _ref5.context,
|
104
|
+
templateAPI = _ref5.templateAPI;
|
105
|
+
context.template = templateAPI;
|
106
|
+
return (_storage$hooks$afterR = (_storage$hooks4 = storage.hooks).afterRender) === null || _storage$hooks$afterR === void 0 ? void 0 : _storage$hooks$afterR.call(_storage$hooks4, context, next);
|
107
|
+
}
|
108
|
+
};
|
106
109
|
}
|
107
110
|
};
|
108
|
-
}, {
|
109
|
-
name: '@modern-js/plugin-server'
|
110
111
|
});
|
package/dist/types/cli.d.ts
CHANGED
package/dist/types/server.d.ts
CHANGED
package/jest.config.js
CHANGED
package/package.json
CHANGED
@@ -11,7 +11,7 @@
|
|
11
11
|
"modern",
|
12
12
|
"modern.js"
|
13
13
|
],
|
14
|
-
"version": "1.1.
|
14
|
+
"version": "1.1.4",
|
15
15
|
"types": "./dist/types/server.d.ts",
|
16
16
|
"jsnext:source": "./src/server.ts",
|
17
17
|
"main": "./dist/js/node/server.js",
|
@@ -33,17 +33,30 @@
|
|
33
33
|
"default": "./dist/js/node/server.js"
|
34
34
|
}
|
35
35
|
},
|
36
|
+
"typesVersions": {
|
37
|
+
"*": {
|
38
|
+
".": [
|
39
|
+
"./dist/types/index.d.ts"
|
40
|
+
],
|
41
|
+
"cli": [
|
42
|
+
"./dist/types/cli.d.ts"
|
43
|
+
],
|
44
|
+
"server": [
|
45
|
+
"./dist/types/server.d.ts"
|
46
|
+
]
|
47
|
+
}
|
48
|
+
},
|
36
49
|
"dependencies": {
|
37
50
|
"@babel/runtime": "^7",
|
38
51
|
"@modern-js/babel-compiler": "^1.2.1",
|
39
52
|
"@modern-js/server-utils": "^1.2.1",
|
40
|
-
"@modern-js/utils": "^1.
|
41
|
-
"@modern-js/server-plugin": "^1.2.1"
|
53
|
+
"@modern-js/utils": "^1.3.6"
|
42
54
|
},
|
43
55
|
"devDependencies": {
|
44
|
-
"@modern-js/core": "^1.
|
56
|
+
"@modern-js/server-core": "^1.2.4",
|
57
|
+
"@modern-js/core": "^1.6.0",
|
45
58
|
"@scripts/build": "0.0.0",
|
46
|
-
"@modern-js/types": "^1.
|
59
|
+
"@modern-js/types": "^1.3.5",
|
47
60
|
"del-cli": "^4.0.1",
|
48
61
|
"typescript": "^4",
|
49
62
|
"@types/jest": "^26.0.9",
|
@@ -51,9 +64,6 @@
|
|
51
64
|
"jest": "^27",
|
52
65
|
"@scripts/jest-config": "0.0.0"
|
53
66
|
},
|
54
|
-
"peerDependencies": {
|
55
|
-
"@modern-js/core": "^1.3.2"
|
56
|
-
},
|
57
67
|
"sideEffects": [
|
58
68
|
"*.css",
|
59
69
|
"*.less",
|
@@ -62,8 +72,7 @@
|
|
62
72
|
],
|
63
73
|
"publishConfig": {
|
64
74
|
"access": "public",
|
65
|
-
"registry": "https://registry.npmjs.org/"
|
66
|
-
"types": "./dist/types/server.d.ts"
|
75
|
+
"registry": "https://registry.npmjs.org/"
|
67
76
|
},
|
68
77
|
"scripts": {
|
69
78
|
"dev": "modern dev",
|
@@ -0,0 +1,23 @@
|
|
1
|
+
type Sign = { status: number };
|
2
|
+
|
3
|
+
const fn = (sign: Sign) => {
|
4
|
+
sign.status += 1;
|
5
|
+
};
|
6
|
+
|
7
|
+
export default ({
|
8
|
+
addMiddleware,
|
9
|
+
beforeMatch,
|
10
|
+
beforeRender,
|
11
|
+
afterMatch,
|
12
|
+
afterRender,
|
13
|
+
}: any) => {
|
14
|
+
addMiddleware(fn);
|
15
|
+
|
16
|
+
beforeMatch(fn);
|
17
|
+
|
18
|
+
beforeRender(fn);
|
19
|
+
|
20
|
+
afterMatch(fn);
|
21
|
+
|
22
|
+
afterRender(fn);
|
23
|
+
};
|
@@ -0,0 +1,39 @@
|
|
1
|
+
import path from 'path';
|
2
|
+
import cliPugin from '../src/cli';
|
3
|
+
import serverPlugin from '../src/server';
|
4
|
+
|
5
|
+
describe('plugin-server', () => {
|
6
|
+
it('cli', () => {
|
7
|
+
expect(cliPugin).toBeDefined();
|
8
|
+
expect(cliPugin().name).toBe('@modern-js/plugin-server');
|
9
|
+
});
|
10
|
+
|
11
|
+
it('server', () => {
|
12
|
+
expect(serverPlugin).toBeDefined();
|
13
|
+
|
14
|
+
const plugin = serverPlugin();
|
15
|
+
expect(plugin.name).toBe('@modern-js/plugin-server');
|
16
|
+
|
17
|
+
const hooks: any = plugin.setup!({
|
18
|
+
useAppContext: () => ({
|
19
|
+
appDirectory: path.join(__dirname, './fixtures/foo'),
|
20
|
+
}),
|
21
|
+
} as any);
|
22
|
+
|
23
|
+
const sign = { status: 0 };
|
24
|
+
hooks.gather({
|
25
|
+
addWebMiddleware: (fn: any) => {
|
26
|
+
fn(sign);
|
27
|
+
},
|
28
|
+
});
|
29
|
+
expect(sign.status).toBe(1);
|
30
|
+
|
31
|
+
const params = { context: sign };
|
32
|
+
hooks.beforeMatch(params);
|
33
|
+
hooks.afterMatch(params);
|
34
|
+
hooks.beforeRender(params);
|
35
|
+
hooks.afterRender(params);
|
36
|
+
hooks.reset();
|
37
|
+
expect(sign.status).toBe(5);
|
38
|
+
});
|
39
|
+
});
|