@modern-js/plugin-server 1.1.2 → 1.1.3
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 +9 -0
- package/dist/js/modern/cli.js +38 -40
- package/dist/js/modern/server.js +72 -72
- package/dist/js/node/cli.js +46 -49
- package/dist/js/node/server.js +70 -71
- package/dist/js/treeshaking/cli.js +52 -52
- package/dist/js/treeshaking/server.js +61 -59
- 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 +6 -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,14 @@
|
|
1
1
|
# @modern-js/plugin-server
|
2
2
|
|
3
|
+
## 1.1.3
|
4
|
+
|
5
|
+
### Patch Changes
|
6
|
+
|
7
|
+
- c7dc7f54: migrate to new plugin style
|
8
|
+
- 2008fdbd: convert two packages server part, support server load plugin itself
|
9
|
+
- Updated dependencies [5bf5868d]
|
10
|
+
- @modern-js/utils@1.3.5
|
11
|
+
|
3
12
|
## 1.1.2
|
4
13
|
|
5
14
|
### 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,5 +1,4 @@
|
|
1
1
|
import path from 'path';
|
2
|
-
import { createPlugin, useAppContext } from '@modern-js/server-core';
|
3
2
|
import { isProd, requireExistModule, SERVER_DIR } from '@modern-js/utils';
|
4
3
|
const WEB_APP_NAME = 'index';
|
5
4
|
|
@@ -27,77 +26,78 @@ const createTransformAPI = storage => new Proxy({}, {
|
|
27
26
|
|
28
27
|
});
|
29
28
|
|
30
|
-
export default
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
reset() {
|
48
|
-
storage.reset();
|
49
|
-
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
|
+
}
|
50
46
|
|
51
|
-
|
52
|
-
|
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);
|
53
99
|
}
|
54
|
-
},
|
55
|
-
|
56
|
-
gather({
|
57
|
-
addWebMiddleware
|
58
|
-
}) {
|
59
|
-
storage.middlewares.forEach(mid => {
|
60
|
-
addWebMiddleware(mid);
|
61
|
-
});
|
62
|
-
},
|
63
|
-
|
64
|
-
beforeMatch({
|
65
|
-
context
|
66
|
-
}, next) {
|
67
|
-
var _storage$hooks$before, _storage$hooks;
|
68
|
-
|
69
|
-
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);
|
70
|
-
},
|
71
|
-
|
72
|
-
afterMatch({
|
73
|
-
context,
|
74
|
-
routeAPI
|
75
|
-
}, next) {
|
76
|
-
var _storage$hooks$afterM, _storage$hooks2;
|
77
|
-
|
78
|
-
context.router = routeAPI;
|
79
|
-
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);
|
80
|
-
},
|
81
|
-
|
82
|
-
beforeRender({
|
83
|
-
context
|
84
|
-
}, next) {
|
85
|
-
var _storage$hooks$before2, _storage$hooks3;
|
86
|
-
|
87
|
-
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);
|
88
|
-
},
|
89
|
-
|
90
|
-
afterRender({
|
91
|
-
context,
|
92
|
-
templateAPI
|
93
|
-
}, next) {
|
94
|
-
var _storage$hooks$afterR, _storage$hooks4;
|
95
|
-
|
96
|
-
context.template = templateAPI;
|
97
|
-
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);
|
98
|
-
}
|
99
100
|
|
100
|
-
|
101
|
-
}
|
102
|
-
|
103
|
-
});
|
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,8 +7,6 @@ exports.default = void 0;
|
|
7
7
|
|
8
8
|
var _path = _interopRequireDefault(require("path"));
|
9
9
|
|
10
|
-
var _serverCore = require("@modern-js/server-core");
|
11
|
-
|
12
10
|
var _utils = require("@modern-js/utils");
|
13
11
|
|
14
12
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
@@ -39,82 +37,83 @@ const createTransformAPI = storage => new Proxy({}, {
|
|
39
37
|
|
40
38
|
});
|
41
39
|
|
42
|
-
var _default = (
|
43
|
-
|
44
|
-
|
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
|
-
|
54
|
-
const transformAPI = createTransformAPI(storage);
|
55
|
-
const webMod = (0, _utils.requireExistModule)(webAppPath);
|
51
|
+
const webAppPath = _path.default.resolve(serverPath, WEB_APP_NAME);
|
56
52
|
|
57
|
-
|
58
|
-
|
59
|
-
|
53
|
+
const storage = new Storage();
|
54
|
+
const transformAPI = createTransformAPI(storage);
|
55
|
+
const webMod = (0, _utils.requireExistModule)(webAppPath);
|
60
56
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
const newWebModule = (0, _utils.requireExistModule)(webAppPath);
|
57
|
+
if (webMod) {
|
58
|
+
webMod(transformAPI);
|
59
|
+
}
|
65
60
|
|
66
|
-
|
67
|
-
|
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);
|
68
113
|
}
|
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);
|
113
|
-
}
|
114
114
|
|
115
|
-
|
116
|
-
}
|
117
|
-
name: '@modern-js/plugin-server'
|
115
|
+
};
|
116
|
+
}
|
118
117
|
});
|
119
118
|
|
120
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,7 +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, useAppContext } from '@modern-js/server-core';
|
9
8
|
import { isProd, requireExistModule, SERVER_DIR } from '@modern-js/utils';
|
10
9
|
var WEB_APP_NAME = 'index';
|
11
10
|
|
@@ -44,66 +43,69 @@ var createTransformAPI = function createTransformAPI(storage) {
|
|
44
43
|
});
|
45
44
|
};
|
46
45
|
|
47
|
-
export default
|
48
|
-
var _useAppContext = useAppContext(),
|
49
|
-
appDirectory = _useAppContext.appDirectory,
|
50
|
-
distDirectory = _useAppContext.distDirectory;
|
51
|
-
|
52
|
-
var pwd = isProd() ? distDirectory : appDirectory;
|
53
|
-
var serverPath = path.resolve(pwd, SERVER_DIR);
|
54
|
-
var webAppPath = path.resolve(serverPath, WEB_APP_NAME);
|
55
|
-
var storage = new Storage();
|
56
|
-
var transformAPI = createTransformAPI(storage);
|
57
|
-
var webMod = requireExistModule(webAppPath);
|
58
|
-
|
59
|
-
if (webMod) {
|
60
|
-
webMod(transformAPI);
|
61
|
-
}
|
62
|
-
|
46
|
+
export default (function () {
|
63
47
|
return {
|
64
|
-
|
65
|
-
|
66
|
-
var
|
67
|
-
|
68
|
-
|
69
|
-
|
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);
|
70
63
|
}
|
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
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
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
|
+
};
|
105
109
|
}
|
106
110
|
};
|
107
|
-
}, {
|
108
|
-
name: '@modern-js/plugin-server'
|
109
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.3",
|
15
15
|
"types": "./dist/types/server.d.ts",
|
16
16
|
"jsnext:source": "./src/server.ts",
|
17
17
|
"main": "./dist/js/node/server.js",
|
@@ -37,13 +37,13 @@
|
|
37
37
|
"@babel/runtime": "^7",
|
38
38
|
"@modern-js/babel-compiler": "^1.2.1",
|
39
39
|
"@modern-js/server-utils": "^1.2.1",
|
40
|
-
"@modern-js/utils": "^1.3.
|
41
|
-
"@modern-js/server-core": "^1.2.2"
|
40
|
+
"@modern-js/utils": "^1.3.5"
|
42
41
|
},
|
43
42
|
"devDependencies": {
|
44
|
-
"@modern-js/core": "^1.
|
43
|
+
"@modern-js/server-core": "^1.2.3",
|
44
|
+
"@modern-js/core": "^1.5.0",
|
45
45
|
"@scripts/build": "0.0.0",
|
46
|
-
"@modern-js/types": "^1.3.
|
46
|
+
"@modern-js/types": "^1.3.5",
|
47
47
|
"del-cli": "^4.0.1",
|
48
48
|
"typescript": "^4",
|
49
49
|
"@types/jest": "^26.0.9",
|
@@ -51,9 +51,6 @@
|
|
51
51
|
"jest": "^27",
|
52
52
|
"@scripts/jest-config": "0.0.0"
|
53
53
|
},
|
54
|
-
"peerDependencies": {
|
55
|
-
"@modern-js/core": "^1.4.0"
|
56
|
-
},
|
57
54
|
"sideEffects": [
|
58
55
|
"*.css",
|
59
56
|
"*.less",
|
@@ -62,8 +59,7 @@
|
|
62
59
|
],
|
63
60
|
"publishConfig": {
|
64
61
|
"access": "public",
|
65
|
-
"registry": "https://registry.npmjs.org/"
|
66
|
-
"types": "./dist/types/server.d.ts"
|
62
|
+
"registry": "https://registry.npmjs.org/"
|
67
63
|
},
|
68
64
|
"scripts": {
|
69
65
|
"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
|
+
});
|