@modern-js/plugin-bff 1.6.2-beta.0 → 1.16.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 +52 -0
- package/dist/js/modern/cli.js +10 -1
- package/dist/js/modern/loader.js +24 -3
- package/dist/js/node/cli.js +11 -1
- package/dist/js/node/loader.js +27 -3
- package/dist/js/treeshaking/cli.js +260 -0
- package/dist/js/treeshaking/constants.js +2 -0
- package/dist/js/treeshaking/helper.js +14 -0
- package/dist/js/treeshaking/index.js +1 -0
- package/dist/js/treeshaking/loader.js +88 -0
- package/dist/js/treeshaking/server.js +90 -0
- package/dist/types/loader.d.ts +2 -0
- package/package.json +14 -15
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,57 @@
|
|
|
1
1
|
# @modern-js/plugin-bff
|
|
2
2
|
|
|
3
|
+
## 1.16.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 020b9bd52: feat: support frame mode without lambda directories
|
|
8
|
+
feat: 支持无 lambda 目录的框架模式
|
|
9
|
+
|
|
10
|
+
### Patch Changes
|
|
11
|
+
|
|
12
|
+
- Updated dependencies [641592f52]
|
|
13
|
+
- Updated dependencies [3904b30a5]
|
|
14
|
+
- Updated dependencies [1100dd58c]
|
|
15
|
+
- Updated dependencies [e04e6e76a]
|
|
16
|
+
- Updated dependencies [81c66e4a4]
|
|
17
|
+
- Updated dependencies [2c305b6f5]
|
|
18
|
+
- Updated dependencies [020b9bd52]
|
|
19
|
+
- @modern-js/utils@1.16.0
|
|
20
|
+
- @modern-js/create-request@1.16.0
|
|
21
|
+
- @modern-js/server-utils@1.16.0
|
|
22
|
+
- @modern-js/bff-core@1.16.0
|
|
23
|
+
- @modern-js/babel-compiler@1.16.0
|
|
24
|
+
|
|
25
|
+
## 1.15.0
|
|
26
|
+
|
|
27
|
+
### Patch Changes
|
|
28
|
+
|
|
29
|
+
- c0d8dac: fix: remove package adapter-helpers and bff runtime export
|
|
30
|
+
- Updated dependencies [c0d8dac]
|
|
31
|
+
- Updated dependencies [8658a78]
|
|
32
|
+
- Updated dependencies [05d4a4f]
|
|
33
|
+
- Updated dependencies [7bfaaf9]
|
|
34
|
+
- Updated dependencies [b1f7000]
|
|
35
|
+
- Updated dependencies [ad05af9]
|
|
36
|
+
- Updated dependencies [5d53d1c]
|
|
37
|
+
- Updated dependencies [37cd159]
|
|
38
|
+
- @modern-js/bff-core@1.15.0
|
|
39
|
+
- @modern-js/utils@1.15.0
|
|
40
|
+
- @modern-js/create-request@1.15.0
|
|
41
|
+
- @modern-js/server-utils@1.15.0
|
|
42
|
+
- @modern-js/babel-compiler@1.15.0
|
|
43
|
+
|
|
44
|
+
## 1.6.2
|
|
45
|
+
|
|
46
|
+
### Patch Changes
|
|
47
|
+
|
|
48
|
+
- a27ab8d: feat: add onApiChange hook for bff hot reload
|
|
49
|
+
feat: 为 BFF 热更新优化,添加 onApiChange 钩子
|
|
50
|
+
- Updated dependencies [a27ab8d]
|
|
51
|
+
- @modern-js/bff-core@1.1.2
|
|
52
|
+
- @modern-js/server-utils@1.2.11
|
|
53
|
+
- @modern-js/utils@1.7.12
|
|
54
|
+
|
|
3
55
|
## 1.6.1
|
|
4
56
|
|
|
5
57
|
### Patch Changes
|
package/dist/js/modern/cli.js
CHANGED
|
@@ -2,6 +2,7 @@ import path from 'path';
|
|
|
2
2
|
import { compiler } from '@modern-js/babel-compiler';
|
|
3
3
|
import { fs, API_DIR, PLUGIN_SCHEMAS, normalizeOutputPath, SHARED_DIR, isProd } from '@modern-js/utils';
|
|
4
4
|
import { resolveBabelConfig } from '@modern-js/server-utils';
|
|
5
|
+
import { ApiRouter } from '@modern-js/bff-core';
|
|
5
6
|
import { registerModernRuntimePath } from "./helper";
|
|
6
7
|
const DEFAULT_API_PREFIX = '/api';
|
|
7
8
|
const TS_CONFIG_FILENAME = 'tsconfig.json';
|
|
@@ -75,10 +76,18 @@ export default (() => ({
|
|
|
75
76
|
const prefix = (bff === null || bff === void 0 ? void 0 : bff.prefix) || DEFAULT_API_PREFIX;
|
|
76
77
|
const rootDir = path.resolve(appDirectory, API_DIR);
|
|
77
78
|
chain.resolve.alias.set('@api', rootDir);
|
|
78
|
-
const
|
|
79
|
+
const apiRouter = new ApiRouter({
|
|
80
|
+
apiDir: rootDir,
|
|
81
|
+
prefix
|
|
82
|
+
});
|
|
83
|
+
const lambdaDir = apiRouter.getLambdaDir();
|
|
84
|
+
const existLambda = apiRouter.isExistLambda();
|
|
85
|
+
const apiRegexp = new RegExp(normalizeOutputPath(`${rootDir}${path.sep}.*(.[tj]s)$`));
|
|
79
86
|
chain.module.rule(CHAIN_ID.RULE.LOADERS).oneOf(CHAIN_ID.ONE_OF.BFF_CLIENT).before(CHAIN_ID.ONE_OF.FALLBACK).test(apiRegexp).use('custom-loader').loader(require.resolve("./loader").replace(/\\/g, '/')).options({
|
|
80
87
|
prefix,
|
|
81
88
|
apiDir: rootDir,
|
|
89
|
+
lambdaDir,
|
|
90
|
+
existLambda,
|
|
82
91
|
port,
|
|
83
92
|
fetcher,
|
|
84
93
|
target: name,
|
package/dist/js/modern/loader.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { generateClient } from '@modern-js/bff-core';
|
|
2
|
+
import { logger } from '@modern-js/utils';
|
|
2
3
|
|
|
3
4
|
async function loader(source) {
|
|
4
5
|
// eslint-disable-next-line @babel/no-invalid-this
|
|
@@ -6,16 +7,36 @@ async function loader(source) {
|
|
|
6
7
|
|
|
7
8
|
const callback = this.async(); // eslint-disable-next-line @babel/no-invalid-this
|
|
8
9
|
|
|
9
|
-
const draftOptions = this.getOptions();
|
|
10
|
+
const draftOptions = this.getOptions(); // eslint-disable-next-line @babel/no-invalid-this
|
|
11
|
+
|
|
12
|
+
const {
|
|
13
|
+
resourcePath
|
|
14
|
+
} = this;
|
|
15
|
+
const warning = `The file ${resourcePath} is not allowd to be imported in src directory, only API definition files are allowed.`;
|
|
16
|
+
|
|
17
|
+
if (!draftOptions.existLambda) {
|
|
18
|
+
logger.warn(warning);
|
|
19
|
+
callback(null, `throw new Error('${warning}')`);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
10
23
|
const options = {
|
|
11
24
|
prefix: Array.isArray(draftOptions.prefix) ? draftOptions.prefix[0] : draftOptions.prefix,
|
|
12
25
|
apiDir: draftOptions.apiDir,
|
|
13
26
|
target: draftOptions.target,
|
|
14
27
|
port: Number(draftOptions.port),
|
|
15
28
|
source,
|
|
16
|
-
|
|
17
|
-
resourcePath: this.resourcePath
|
|
29
|
+
resourcePath
|
|
18
30
|
};
|
|
31
|
+
const {
|
|
32
|
+
lambdaDir
|
|
33
|
+
} = draftOptions;
|
|
34
|
+
|
|
35
|
+
if (!resourcePath.startsWith(lambdaDir)) {
|
|
36
|
+
logger.warn(warning);
|
|
37
|
+
callback(null, `throw new Error('${warning}')`);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
19
40
|
|
|
20
41
|
if (draftOptions.fetcher) {
|
|
21
42
|
options.fetcher = draftOptions.fetcher;
|
package/dist/js/node/cli.js
CHANGED
|
@@ -13,6 +13,8 @@ var _utils = require("@modern-js/utils");
|
|
|
13
13
|
|
|
14
14
|
var _serverUtils = require("@modern-js/server-utils");
|
|
15
15
|
|
|
16
|
+
var _bffCore = require("@modern-js/bff-core");
|
|
17
|
+
|
|
16
18
|
var _helper = require("./helper");
|
|
17
19
|
|
|
18
20
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
@@ -93,10 +95,18 @@ var _default = () => ({
|
|
|
93
95
|
const rootDir = _path.default.resolve(appDirectory, _utils.API_DIR);
|
|
94
96
|
|
|
95
97
|
chain.resolve.alias.set('@api', rootDir);
|
|
96
|
-
const
|
|
98
|
+
const apiRouter = new _bffCore.ApiRouter({
|
|
99
|
+
apiDir: rootDir,
|
|
100
|
+
prefix
|
|
101
|
+
});
|
|
102
|
+
const lambdaDir = apiRouter.getLambdaDir();
|
|
103
|
+
const existLambda = apiRouter.isExistLambda();
|
|
104
|
+
const apiRegexp = new RegExp((0, _utils.normalizeOutputPath)(`${rootDir}${_path.default.sep}.*(.[tj]s)$`));
|
|
97
105
|
chain.module.rule(CHAIN_ID.RULE.LOADERS).oneOf(CHAIN_ID.ONE_OF.BFF_CLIENT).before(CHAIN_ID.ONE_OF.FALLBACK).test(apiRegexp).use('custom-loader').loader(require.resolve("./loader").replace(/\\/g, '/')).options({
|
|
98
106
|
prefix,
|
|
99
107
|
apiDir: rootDir,
|
|
108
|
+
lambdaDir,
|
|
109
|
+
existLambda,
|
|
100
110
|
port,
|
|
101
111
|
fetcher,
|
|
102
112
|
target: name,
|
package/dist/js/node/loader.js
CHANGED
|
@@ -7,22 +7,46 @@ exports.default = void 0;
|
|
|
7
7
|
|
|
8
8
|
var _bffCore = require("@modern-js/bff-core");
|
|
9
9
|
|
|
10
|
+
var _utils = require("@modern-js/utils");
|
|
11
|
+
|
|
10
12
|
async function loader(source) {
|
|
11
13
|
// eslint-disable-next-line @babel/no-invalid-this
|
|
12
14
|
this.cacheable(); // eslint-disable-next-line @babel/no-invalid-this
|
|
13
15
|
|
|
14
16
|
const callback = this.async(); // eslint-disable-next-line @babel/no-invalid-this
|
|
15
17
|
|
|
16
|
-
const draftOptions = this.getOptions();
|
|
18
|
+
const draftOptions = this.getOptions(); // eslint-disable-next-line @babel/no-invalid-this
|
|
19
|
+
|
|
20
|
+
const {
|
|
21
|
+
resourcePath
|
|
22
|
+
} = this;
|
|
23
|
+
const warning = `The file ${resourcePath} is not allowd to be imported in src directory, only API definition files are allowed.`;
|
|
24
|
+
|
|
25
|
+
if (!draftOptions.existLambda) {
|
|
26
|
+
_utils.logger.warn(warning);
|
|
27
|
+
|
|
28
|
+
callback(null, `throw new Error('${warning}')`);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
17
32
|
const options = {
|
|
18
33
|
prefix: Array.isArray(draftOptions.prefix) ? draftOptions.prefix[0] : draftOptions.prefix,
|
|
19
34
|
apiDir: draftOptions.apiDir,
|
|
20
35
|
target: draftOptions.target,
|
|
21
36
|
port: Number(draftOptions.port),
|
|
22
37
|
source,
|
|
23
|
-
|
|
24
|
-
resourcePath: this.resourcePath
|
|
38
|
+
resourcePath
|
|
25
39
|
};
|
|
40
|
+
const {
|
|
41
|
+
lambdaDir
|
|
42
|
+
} = draftOptions;
|
|
43
|
+
|
|
44
|
+
if (!resourcePath.startsWith(lambdaDir)) {
|
|
45
|
+
_utils.logger.warn(warning);
|
|
46
|
+
|
|
47
|
+
callback(null, `throw new Error('${warning}')`);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
26
50
|
|
|
27
51
|
if (draftOptions.fetcher) {
|
|
28
52
|
options.fetcher = draftOptions.fetcher;
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
|
2
|
+
import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
|
|
3
|
+
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
|
|
4
|
+
import path from 'path';
|
|
5
|
+
import { compiler } from '@modern-js/babel-compiler';
|
|
6
|
+
import { fs, API_DIR, PLUGIN_SCHEMAS, normalizeOutputPath, SHARED_DIR, isProd } from '@modern-js/utils';
|
|
7
|
+
import { resolveBabelConfig } from '@modern-js/server-utils';
|
|
8
|
+
import { ApiRouter } from '@modern-js/bff-core';
|
|
9
|
+
import { registerModernRuntimePath } from "./helper";
|
|
10
|
+
var DEFAULT_API_PREFIX = '/api';
|
|
11
|
+
var TS_CONFIG_FILENAME = 'tsconfig.json';
|
|
12
|
+
var FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs']; // TODO: 封装服务端编译函数
|
|
13
|
+
|
|
14
|
+
var compile = /*#__PURE__*/function () {
|
|
15
|
+
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(appDirectory, modernConfig, compileOptions) {
|
|
16
|
+
var patterns, results;
|
|
17
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
18
|
+
while (1) {
|
|
19
|
+
switch (_context2.prev = _context2.next) {
|
|
20
|
+
case 0:
|
|
21
|
+
patterns = compileOptions.patterns;
|
|
22
|
+
_context2.next = 3;
|
|
23
|
+
return Promise.all(patterns.map( /*#__PURE__*/function () {
|
|
24
|
+
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(pattern) {
|
|
25
|
+
var from, to, tsconfigPath, babelConfig, basename, targetDir;
|
|
26
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
27
|
+
while (1) {
|
|
28
|
+
switch (_context.prev = _context.next) {
|
|
29
|
+
case 0:
|
|
30
|
+
from = pattern.from, to = pattern.to, tsconfigPath = pattern.tsconfigPath;
|
|
31
|
+
babelConfig = resolveBabelConfig(appDirectory, modernConfig, {
|
|
32
|
+
tsconfigPath: tsconfigPath ? tsconfigPath : '',
|
|
33
|
+
syntax: 'es6+',
|
|
34
|
+
type: 'commonjs'
|
|
35
|
+
});
|
|
36
|
+
_context.next = 4;
|
|
37
|
+
return fs.pathExists(from);
|
|
38
|
+
|
|
39
|
+
case 4:
|
|
40
|
+
if (!_context.sent) {
|
|
41
|
+
_context.next = 9;
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
basename = path.basename(from);
|
|
46
|
+
targetDir = path.join(to, basename);
|
|
47
|
+
_context.next = 9;
|
|
48
|
+
return fs.copy(from, targetDir, {
|
|
49
|
+
filter: function filter(src) {
|
|
50
|
+
return !['.ts', '.js'].includes(path.extname(src)) && src !== tsconfigPath;
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
case 9:
|
|
55
|
+
return _context.abrupt("return", compiler({
|
|
56
|
+
rootDir: appDirectory,
|
|
57
|
+
distDir: to,
|
|
58
|
+
sourceDir: from,
|
|
59
|
+
extensions: FILE_EXTENSIONS
|
|
60
|
+
}, babelConfig));
|
|
61
|
+
|
|
62
|
+
case 10:
|
|
63
|
+
case "end":
|
|
64
|
+
return _context.stop();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}, _callee);
|
|
68
|
+
}));
|
|
69
|
+
|
|
70
|
+
return function (_x4) {
|
|
71
|
+
return _ref2.apply(this, arguments);
|
|
72
|
+
};
|
|
73
|
+
}()));
|
|
74
|
+
|
|
75
|
+
case 3:
|
|
76
|
+
results = _context2.sent;
|
|
77
|
+
results.forEach(function (result) {
|
|
78
|
+
if (result.code === 1) {
|
|
79
|
+
throw new Error(result.message);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
case 5:
|
|
84
|
+
case "end":
|
|
85
|
+
return _context2.stop();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}, _callee2);
|
|
89
|
+
}));
|
|
90
|
+
|
|
91
|
+
return function compile(_x, _x2, _x3) {
|
|
92
|
+
return _ref.apply(this, arguments);
|
|
93
|
+
};
|
|
94
|
+
}();
|
|
95
|
+
|
|
96
|
+
export default (function () {
|
|
97
|
+
return {
|
|
98
|
+
name: '@modern-js/plugin-bff',
|
|
99
|
+
setup: function setup(api) {
|
|
100
|
+
var unRegisterResolveRuntimePath = null;
|
|
101
|
+
return {
|
|
102
|
+
validateSchema: function validateSchema() {
|
|
103
|
+
return PLUGIN_SCHEMAS['@modern-js/plugin-bff'];
|
|
104
|
+
},
|
|
105
|
+
config: function config() {
|
|
106
|
+
return {
|
|
107
|
+
tools: {
|
|
108
|
+
webpackChain: function webpackChain(chain, _ref3) {
|
|
109
|
+
var name = _ref3.name,
|
|
110
|
+
CHAIN_ID = _ref3.CHAIN_ID;
|
|
111
|
+
|
|
112
|
+
var _api$useAppContext = api.useAppContext(),
|
|
113
|
+
appDirectory = _api$useAppContext.appDirectory,
|
|
114
|
+
port = _api$useAppContext.port;
|
|
115
|
+
|
|
116
|
+
var modernConfig = api.useResolvedConfigContext();
|
|
117
|
+
|
|
118
|
+
var _ref4 = modernConfig || {},
|
|
119
|
+
bff = _ref4.bff;
|
|
120
|
+
|
|
121
|
+
var _ref5 = bff || {},
|
|
122
|
+
fetcher = _ref5.fetcher;
|
|
123
|
+
|
|
124
|
+
var prefix = (bff === null || bff === void 0 ? void 0 : bff.prefix) || DEFAULT_API_PREFIX;
|
|
125
|
+
var rootDir = path.resolve(appDirectory, API_DIR);
|
|
126
|
+
chain.resolve.alias.set('@api', rootDir);
|
|
127
|
+
var apiRouter = new ApiRouter({
|
|
128
|
+
apiDir: rootDir,
|
|
129
|
+
prefix: prefix
|
|
130
|
+
});
|
|
131
|
+
var lambdaDir = apiRouter.getLambdaDir();
|
|
132
|
+
var existLambda = apiRouter.isExistLambda();
|
|
133
|
+
var apiRegexp = new RegExp(normalizeOutputPath("".concat(rootDir).concat(path.sep, ".*(.[tj]s)$")));
|
|
134
|
+
chain.module.rule(CHAIN_ID.RULE.LOADERS).oneOf(CHAIN_ID.ONE_OF.BFF_CLIENT).before(CHAIN_ID.ONE_OF.FALLBACK).test(apiRegexp).use('custom-loader').loader(require.resolve("./loader").replace(/\\/g, '/')).options({
|
|
135
|
+
prefix: prefix,
|
|
136
|
+
apiDir: rootDir,
|
|
137
|
+
lambdaDir: lambdaDir,
|
|
138
|
+
existLambda: existLambda,
|
|
139
|
+
port: port,
|
|
140
|
+
fetcher: fetcher,
|
|
141
|
+
target: name,
|
|
142
|
+
requestCreator: bff === null || bff === void 0 ? void 0 : bff.requestCreator
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
source: {
|
|
147
|
+
moduleScopes: ["./".concat(API_DIR), /create-request/]
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
},
|
|
151
|
+
modifyServerRoutes: function modifyServerRoutes(_ref6) {
|
|
152
|
+
var routes = _ref6.routes;
|
|
153
|
+
var modernConfig = api.useResolvedConfigContext();
|
|
154
|
+
|
|
155
|
+
var _ref7 = modernConfig || {},
|
|
156
|
+
bff = _ref7.bff;
|
|
157
|
+
|
|
158
|
+
var prefix = (bff === null || bff === void 0 ? void 0 : bff.prefix) || '/api';
|
|
159
|
+
var prefixList = [];
|
|
160
|
+
|
|
161
|
+
if (Array.isArray(prefix)) {
|
|
162
|
+
prefixList.push.apply(prefixList, _toConsumableArray(prefix));
|
|
163
|
+
} else {
|
|
164
|
+
prefixList.push(prefix);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
var apiServerRoutes = prefixList.map(function (pre) {
|
|
168
|
+
return {
|
|
169
|
+
urlPath: pre,
|
|
170
|
+
isApi: true,
|
|
171
|
+
entryPath: '',
|
|
172
|
+
isSPA: false,
|
|
173
|
+
isSSR: false // FIXME: })) as IAppContext[`serverRoutes`];
|
|
174
|
+
|
|
175
|
+
};
|
|
176
|
+
});
|
|
177
|
+
return {
|
|
178
|
+
routes: routes.concat(apiServerRoutes)
|
|
179
|
+
};
|
|
180
|
+
},
|
|
181
|
+
beforeBuild: function beforeBuild() {
|
|
182
|
+
return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
|
|
183
|
+
var _api$useAppContext2, internalDirectory;
|
|
184
|
+
|
|
185
|
+
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
186
|
+
while (1) {
|
|
187
|
+
switch (_context3.prev = _context3.next) {
|
|
188
|
+
case 0:
|
|
189
|
+
// help esbuild-register resolve @modern-js/server/runtime
|
|
190
|
+
if (isProd()) {
|
|
191
|
+
_api$useAppContext2 = api.useAppContext(), internalDirectory = _api$useAppContext2.internalDirectory;
|
|
192
|
+
unRegisterResolveRuntimePath = registerModernRuntimePath(internalDirectory);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
case 1:
|
|
196
|
+
case "end":
|
|
197
|
+
return _context3.stop();
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}, _callee3);
|
|
201
|
+
}))();
|
|
202
|
+
},
|
|
203
|
+
afterBuild: function afterBuild() {
|
|
204
|
+
return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4() {
|
|
205
|
+
var _api$useAppContext3, appDirectory, distDirectory, modernConfig, distDir, apiDir, sharedDir, tsconfigPath, patterns;
|
|
206
|
+
|
|
207
|
+
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
208
|
+
while (1) {
|
|
209
|
+
switch (_context4.prev = _context4.next) {
|
|
210
|
+
case 0:
|
|
211
|
+
if (unRegisterResolveRuntimePath) {
|
|
212
|
+
unRegisterResolveRuntimePath();
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
_api$useAppContext3 = api.useAppContext(), appDirectory = _api$useAppContext3.appDirectory, distDirectory = _api$useAppContext3.distDirectory;
|
|
216
|
+
modernConfig = api.useResolvedConfigContext();
|
|
217
|
+
distDir = path.resolve(distDirectory);
|
|
218
|
+
apiDir = path.resolve(appDirectory, API_DIR);
|
|
219
|
+
sharedDir = path.resolve(appDirectory, SHARED_DIR);
|
|
220
|
+
tsconfigPath = path.resolve(appDirectory, TS_CONFIG_FILENAME);
|
|
221
|
+
patterns = [];
|
|
222
|
+
|
|
223
|
+
if (fs.existsSync(apiDir)) {
|
|
224
|
+
patterns.push({
|
|
225
|
+
from: apiDir,
|
|
226
|
+
to: distDir,
|
|
227
|
+
tsconfigPath: tsconfigPath
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (fs.existsSync(sharedDir)) {
|
|
232
|
+
patterns.push({
|
|
233
|
+
from: sharedDir,
|
|
234
|
+
to: distDir,
|
|
235
|
+
tsconfigPath: tsconfigPath
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (!(patterns.length > 0)) {
|
|
240
|
+
_context4.next = 13;
|
|
241
|
+
break;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
_context4.next = 13;
|
|
245
|
+
return compile(appDirectory, modernConfig, {
|
|
246
|
+
patterns: patterns
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
case 13:
|
|
250
|
+
case "end":
|
|
251
|
+
return _context4.stop();
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}, _callee4);
|
|
255
|
+
}))();
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
2
|
+
import * as path from 'path';
|
|
3
|
+
import { registerPaths } from '@modern-js/bff-core';
|
|
4
|
+
var serverRuntimeAlias = '@modern-js/runtime/server';
|
|
5
|
+
var serverRuntimePath = '.runtime-exports/server';
|
|
6
|
+
|
|
7
|
+
var registerModernRuntimePath = function registerModernRuntimePath(internalDirectory) {
|
|
8
|
+
var paths = _defineProperty({}, serverRuntimeAlias, path.join(internalDirectory, serverRuntimePath));
|
|
9
|
+
|
|
10
|
+
var unRegister = registerPaths(paths);
|
|
11
|
+
return unRegister;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export { registerModernRuntimePath };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./constants";
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
|
|
2
|
+
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
|
|
3
|
+
import { generateClient } from '@modern-js/bff-core';
|
|
4
|
+
import { logger } from '@modern-js/utils';
|
|
5
|
+
|
|
6
|
+
function loader(_x) {
|
|
7
|
+
return _loader.apply(this, arguments);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function _loader() {
|
|
11
|
+
_loader = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(source) {
|
|
12
|
+
var callback, draftOptions, resourcePath, warning, options, lambdaDir, result;
|
|
13
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
14
|
+
while (1) {
|
|
15
|
+
switch (_context.prev = _context.next) {
|
|
16
|
+
case 0:
|
|
17
|
+
// eslint-disable-next-line @babel/no-invalid-this
|
|
18
|
+
this.cacheable(); // eslint-disable-next-line @babel/no-invalid-this
|
|
19
|
+
|
|
20
|
+
callback = this.async(); // eslint-disable-next-line @babel/no-invalid-this
|
|
21
|
+
|
|
22
|
+
draftOptions = this.getOptions(); // eslint-disable-next-line @babel/no-invalid-this
|
|
23
|
+
|
|
24
|
+
resourcePath = this.resourcePath;
|
|
25
|
+
warning = "The file ".concat(resourcePath, " is not allowd to be imported in src directory, only API definition files are allowed.");
|
|
26
|
+
|
|
27
|
+
if (draftOptions.existLambda) {
|
|
28
|
+
_context.next = 9;
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
logger.warn(warning);
|
|
33
|
+
callback(null, "throw new Error('".concat(warning, "')"));
|
|
34
|
+
return _context.abrupt("return");
|
|
35
|
+
|
|
36
|
+
case 9:
|
|
37
|
+
options = {
|
|
38
|
+
prefix: Array.isArray(draftOptions.prefix) ? draftOptions.prefix[0] : draftOptions.prefix,
|
|
39
|
+
apiDir: draftOptions.apiDir,
|
|
40
|
+
target: draftOptions.target,
|
|
41
|
+
port: Number(draftOptions.port),
|
|
42
|
+
source: source,
|
|
43
|
+
resourcePath: resourcePath
|
|
44
|
+
};
|
|
45
|
+
lambdaDir = draftOptions.lambdaDir;
|
|
46
|
+
|
|
47
|
+
if (resourcePath.startsWith(lambdaDir)) {
|
|
48
|
+
_context.next = 15;
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
logger.warn(warning);
|
|
53
|
+
callback(null, "throw new Error('".concat(warning, "')"));
|
|
54
|
+
return _context.abrupt("return");
|
|
55
|
+
|
|
56
|
+
case 15:
|
|
57
|
+
if (draftOptions.fetcher) {
|
|
58
|
+
options.fetcher = draftOptions.fetcher;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (draftOptions.requestCreator) {
|
|
62
|
+
options.requestCreator = draftOptions.requestCreator;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
options.requireResolve = require.resolve;
|
|
66
|
+
_context.next = 20;
|
|
67
|
+
return generateClient(options);
|
|
68
|
+
|
|
69
|
+
case 20:
|
|
70
|
+
result = _context.sent;
|
|
71
|
+
|
|
72
|
+
if (result.isOk) {
|
|
73
|
+
callback(undefined, result.value);
|
|
74
|
+
} else {
|
|
75
|
+
callback(undefined, "throw new Error('".concat(result.value, "')"));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
case 22:
|
|
79
|
+
case "end":
|
|
80
|
+
return _context.stop();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}, _callee, this);
|
|
84
|
+
}));
|
|
85
|
+
return _loader.apply(this, arguments);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export default loader;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
2
|
+
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
|
|
3
|
+
import _createClass from "@babel/runtime/helpers/esm/createClass";
|
|
4
|
+
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
5
|
+
import path from 'path';
|
|
6
|
+
import { ApiRouter } from '@modern-js/bff-core';
|
|
7
|
+
import { API_DIR, isProd, requireExistModule } from '@modern-js/utils';
|
|
8
|
+
import { API_APP_NAME } from "./constants";
|
|
9
|
+
|
|
10
|
+
var Storage = /*#__PURE__*/function () {
|
|
11
|
+
function Storage() {
|
|
12
|
+
_classCallCheck(this, Storage);
|
|
13
|
+
|
|
14
|
+
_defineProperty(this, "middlewares", []);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
_createClass(Storage, [{
|
|
18
|
+
key: "reset",
|
|
19
|
+
value: function reset() {
|
|
20
|
+
this.middlewares = [];
|
|
21
|
+
}
|
|
22
|
+
}]);
|
|
23
|
+
|
|
24
|
+
return Storage;
|
|
25
|
+
}();
|
|
26
|
+
|
|
27
|
+
var createTransformAPI = function createTransformAPI(storage) {
|
|
28
|
+
return {
|
|
29
|
+
addMiddleware: function addMiddleware(fn) {
|
|
30
|
+
storage.middlewares.push(fn);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export default (function () {
|
|
36
|
+
return {
|
|
37
|
+
name: '@modern-js/plugin-bff',
|
|
38
|
+
setup: function setup(api) {
|
|
39
|
+
var storage = new Storage();
|
|
40
|
+
var transformAPI = createTransformAPI(storage);
|
|
41
|
+
var apiAppPath = '';
|
|
42
|
+
return {
|
|
43
|
+
prepare: function prepare() {
|
|
44
|
+
var _api$useAppContext = api.useAppContext(),
|
|
45
|
+
appDirectory = _api$useAppContext.appDirectory,
|
|
46
|
+
distDirectory = _api$useAppContext.distDirectory;
|
|
47
|
+
|
|
48
|
+
var root = isProd() ? distDirectory : appDirectory;
|
|
49
|
+
var apiPath = path.resolve(root || process.cwd(), API_DIR);
|
|
50
|
+
apiAppPath = path.resolve(apiPath, API_APP_NAME);
|
|
51
|
+
var apiMod = requireExistModule(apiAppPath);
|
|
52
|
+
|
|
53
|
+
if (apiMod && typeof apiMod === 'function') {
|
|
54
|
+
apiMod(transformAPI);
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
reset: function reset() {
|
|
58
|
+
storage.reset();
|
|
59
|
+
var newApiModule = requireExistModule(apiAppPath);
|
|
60
|
+
|
|
61
|
+
if (newApiModule && typeof newApiModule === 'function') {
|
|
62
|
+
newApiModule(transformAPI);
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
gather: function gather(_ref) {
|
|
66
|
+
var addAPIMiddleware = _ref.addAPIMiddleware;
|
|
67
|
+
storage.middlewares.forEach(function (mid) {
|
|
68
|
+
addAPIMiddleware(mid);
|
|
69
|
+
});
|
|
70
|
+
},
|
|
71
|
+
prepareApiServer: function prepareApiServer(props, next) {
|
|
72
|
+
var pwd = props.pwd,
|
|
73
|
+
prefix = props.prefix;
|
|
74
|
+
var apiDir = path.resolve(pwd, API_DIR);
|
|
75
|
+
var appContext = api.useAppContext();
|
|
76
|
+
var apiRouter = new ApiRouter({
|
|
77
|
+
apiDir: apiDir,
|
|
78
|
+
prefix: prefix
|
|
79
|
+
});
|
|
80
|
+
var apiHandlerInfos = apiRouter.getApiHandlers();
|
|
81
|
+
api.setAppContext(_objectSpread(_objectSpread({}, appContext), {}, {
|
|
82
|
+
apiRouter: apiRouter,
|
|
83
|
+
apiHandlerInfos: apiHandlerInfos
|
|
84
|
+
}));
|
|
85
|
+
return next(props);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
});
|
package/dist/types/loader.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.
|
|
14
|
+
"version": "1.16.0",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/index.d.ts",
|
|
17
17
|
"main": "./dist/js/node/index.js",
|
|
@@ -55,20 +55,20 @@
|
|
|
55
55
|
"dependencies": {
|
|
56
56
|
"@babel/core": "^7.18.0",
|
|
57
57
|
"@babel/runtime": "^7.18.0",
|
|
58
|
-
"@modern-js/babel-compiler": "
|
|
59
|
-
"@modern-js/bff-core": "
|
|
60
|
-
"@modern-js/create-request": "
|
|
61
|
-
"@modern-js/server-utils": "
|
|
62
|
-
"@modern-js/utils": "
|
|
58
|
+
"@modern-js/babel-compiler": "1.16.0",
|
|
59
|
+
"@modern-js/bff-core": "1.16.0",
|
|
60
|
+
"@modern-js/create-request": "1.16.0",
|
|
61
|
+
"@modern-js/server-utils": "1.16.0",
|
|
62
|
+
"@modern-js/utils": "1.16.0"
|
|
63
63
|
},
|
|
64
64
|
"devDependencies": {
|
|
65
|
-
"@modern-js/core": "1.
|
|
66
|
-
"@modern-js/
|
|
67
|
-
"@modern-js/runtime": "1.
|
|
68
|
-
"@modern-js/server-core": "1.
|
|
69
|
-
"@modern-js/types": "1.
|
|
70
|
-
"@scripts/build": "
|
|
71
|
-
"@scripts/jest-config": "
|
|
65
|
+
"@modern-js/core": "1.16.0",
|
|
66
|
+
"@modern-js/bff-runtime": "1.16.0",
|
|
67
|
+
"@modern-js/runtime": "1.16.0",
|
|
68
|
+
"@modern-js/server-core": "1.16.0",
|
|
69
|
+
"@modern-js/types": "1.16.0",
|
|
70
|
+
"@scripts/build": "1.15.0",
|
|
71
|
+
"@scripts/jest-config": "1.15.0",
|
|
72
72
|
"@types/babel__core": "^7.1.15",
|
|
73
73
|
"@types/jest": "^27",
|
|
74
74
|
"@types/node": "^14",
|
|
@@ -82,8 +82,7 @@
|
|
|
82
82
|
"sideEffects": false,
|
|
83
83
|
"publishConfig": {
|
|
84
84
|
"registry": "https://registry.npmjs.org/",
|
|
85
|
-
"access": "public"
|
|
86
|
-
"types": "./dist/types/index.d.ts"
|
|
85
|
+
"access": "public"
|
|
87
86
|
},
|
|
88
87
|
"wireit": {
|
|
89
88
|
"build": {
|