@modern-js/plugin-bff 1.5.2 → 1.6.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,26 @@
1
1
  # @modern-js/plugin-bff
2
2
 
3
+ ## 1.6.0-alpha.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 77a8e9e1b: feat: support bff operators
8
+
9
+ ### Patch Changes
10
+
11
+ - f29e9bacf: feat: simplify context usage, no longer depend on containers
12
+ - 6eebd9c1c: fix: resolve server runtime alias for esbuild-register
13
+ - Updated dependencies [77a8e9e1b]
14
+ - Updated dependencies [9cd364e06]
15
+ - Updated dependencies [77a8e9e1b]
16
+ - Updated dependencies [7b9e302e2]
17
+ - Updated dependencies [a90bc96bd]
18
+ - @modern-js/bff-core@1.1.0-alpha.0
19
+ - @modern-js/utils@1.7.9-alpha.0
20
+ - @modern-js/create-request@1.3.0-alpha.0
21
+ - @modern-js/server-utils@1.2.12-alpha.0
22
+ - @modern-js/babel-compiler@1.2.7-alpha.0
23
+
3
24
  ## 1.5.2
4
25
 
5
26
  ### Patch Changes
@@ -1,7 +1,8 @@
1
1
  import path from 'path';
2
2
  import { compiler } from '@modern-js/babel-compiler';
3
- import { fs, API_DIR, PLUGIN_SCHEMAS, normalizeOutputPath, SHARED_DIR } from '@modern-js/utils';
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 { registerModernRuntimePath } from "./helper";
5
6
  const DEFAULT_API_PREFIX = '/api';
6
7
  const TS_CONFIG_FILENAME = 'tsconfig.json';
7
8
  const FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs']; // TODO: 封装服务端编译函数
@@ -46,112 +47,129 @@ const compile = async (appDirectory, modernConfig, compileOptions) => {
46
47
 
47
48
  export default (() => ({
48
49
  name: '@modern-js/plugin-bff',
49
- setup: api => ({
50
- validateSchema() {
51
- return PLUGIN_SCHEMAS['@modern-js/plugin-bff'];
52
- },
53
-
54
- config() {
55
- return {
56
- tools: {
57
- webpackChain: (chain, {
58
- name,
59
- CHAIN_ID
60
- }) => {
61
- const {
62
- appDirectory,
63
- port
64
- } = api.useAppContext();
65
- const modernConfig = api.useResolvedConfigContext();
66
- const {
67
- bff
68
- } = modernConfig || {};
69
- const {
70
- fetcher
71
- } = bff || {};
72
- const prefix = (bff === null || bff === void 0 ? void 0 : bff.prefix) || DEFAULT_API_PREFIX;
73
- const rootDir = path.resolve(appDirectory, API_DIR);
74
- chain.resolve.alias.set('@api', rootDir);
75
- const apiRegexp = new RegExp(normalizeOutputPath(`${appDirectory}${path.sep}api${path.sep}.*(.[tj]s)$`));
76
- 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({
77
- prefix,
78
- apiDir: rootDir,
79
- port,
80
- fetcher,
81
- target: name,
82
- requestCreator: bff === null || bff === void 0 ? void 0 : bff.requestCreator
83
- });
50
+ setup: api => {
51
+ let unRegisterResolveRuntimePath = null;
52
+ return {
53
+ validateSchema() {
54
+ return PLUGIN_SCHEMAS['@modern-js/plugin-bff'];
55
+ },
56
+
57
+ config() {
58
+ return {
59
+ tools: {
60
+ webpackChain: (chain, {
61
+ name,
62
+ CHAIN_ID
63
+ }) => {
64
+ const {
65
+ appDirectory,
66
+ port
67
+ } = api.useAppContext();
68
+ const modernConfig = api.useResolvedConfigContext();
69
+ const {
70
+ bff
71
+ } = modernConfig || {};
72
+ const {
73
+ fetcher
74
+ } = bff || {};
75
+ const prefix = (bff === null || bff === void 0 ? void 0 : bff.prefix) || DEFAULT_API_PREFIX;
76
+ const rootDir = path.resolve(appDirectory, API_DIR);
77
+ chain.resolve.alias.set('@api', rootDir);
78
+ const apiRegexp = new RegExp(normalizeOutputPath(`${appDirectory}${path.sep}api${path.sep}.*(.[tj]s)$`));
79
+ 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
+ prefix,
81
+ apiDir: rootDir,
82
+ port,
83
+ fetcher,
84
+ target: name,
85
+ requestCreator: bff === null || bff === void 0 ? void 0 : bff.requestCreator
86
+ });
87
+ }
88
+ },
89
+ source: {
90
+ moduleScopes: [`./${API_DIR}`, /create-request/]
84
91
  }
85
- },
86
- source: {
87
- moduleScopes: [`./${API_DIR}`, /create-request/]
92
+ };
93
+ },
94
+
95
+ modifyServerRoutes({
96
+ routes
97
+ }) {
98
+ const modernConfig = api.useResolvedConfigContext();
99
+ const {
100
+ bff
101
+ } = modernConfig || {};
102
+ const prefix = (bff === null || bff === void 0 ? void 0 : bff.prefix) || '/api';
103
+ const prefixList = [];
104
+
105
+ if (Array.isArray(prefix)) {
106
+ prefixList.push(...prefix);
107
+ } else {
108
+ prefixList.push(prefix);
88
109
  }
89
- };
90
- },
91
-
92
- modifyServerRoutes({
93
- routes
94
- }) {
95
- const modernConfig = api.useResolvedConfigContext();
96
- const {
97
- bff
98
- } = modernConfig || {};
99
- const prefix = (bff === null || bff === void 0 ? void 0 : bff.prefix) || '/api';
100
- const prefixList = [];
101
-
102
- if (Array.isArray(prefix)) {
103
- prefixList.push(...prefix);
104
- } else {
105
- prefixList.push(prefix);
106
- }
107
110
 
108
- const apiServerRoutes = prefixList.map(pre => ({
109
- urlPath: pre,
110
- isApi: true,
111
- entryPath: '',
112
- isSPA: false,
113
- isSSR: false // FIXME: })) as IAppContext[`serverRoutes`];
114
-
115
- }));
116
- return {
117
- routes: routes.concat(apiServerRoutes)
118
- };
119
- },
120
-
121
- async afterBuild() {
122
- const {
123
- appDirectory,
124
- distDirectory
125
- } = api.useAppContext();
126
- const modernConfig = api.useResolvedConfigContext();
127
- const distDir = path.resolve(distDirectory);
128
- const apiDir = path.resolve(appDirectory, API_DIR);
129
- const sharedDir = path.resolve(appDirectory, SHARED_DIR);
130
- const tsconfigPath = path.resolve(appDirectory, TS_CONFIG_FILENAME);
131
- const patterns = [];
132
-
133
- if (fs.existsSync(apiDir)) {
134
- patterns.push({
135
- from: apiDir,
136
- to: distDir,
137
- tsconfigPath
138
- });
139
- }
111
+ const apiServerRoutes = prefixList.map(pre => ({
112
+ urlPath: pre,
113
+ isApi: true,
114
+ entryPath: '',
115
+ isSPA: false,
116
+ isSSR: false // FIXME: })) as IAppContext[`serverRoutes`];
140
117
 
141
- if (fs.existsSync(sharedDir)) {
142
- patterns.push({
143
- from: sharedDir,
144
- to: distDir,
145
- tsconfigPath
146
- });
147
- }
118
+ }));
119
+ return {
120
+ routes: routes.concat(apiServerRoutes)
121
+ };
122
+ },
123
+
124
+ async beforeBuild() {
125
+ // help esbuild-register resolve @modern-js/server/runtime
126
+ if (isProd()) {
127
+ const {
128
+ internalDirectory
129
+ } = api.useAppContext();
130
+ unRegisterResolveRuntimePath = registerModernRuntimePath(internalDirectory);
131
+ }
132
+ },
148
133
 
149
- if (patterns.length > 0) {
150
- await compile(appDirectory, modernConfig, {
151
- patterns
152
- });
134
+ async afterBuild() {
135
+ if (unRegisterResolveRuntimePath) {
136
+ unRegisterResolveRuntimePath();
137
+ }
138
+
139
+ const {
140
+ appDirectory,
141
+ distDirectory
142
+ } = api.useAppContext();
143
+ const modernConfig = api.useResolvedConfigContext();
144
+ const distDir = path.resolve(distDirectory);
145
+ const apiDir = path.resolve(appDirectory, API_DIR);
146
+ const sharedDir = path.resolve(appDirectory, SHARED_DIR);
147
+ const tsconfigPath = path.resolve(appDirectory, TS_CONFIG_FILENAME);
148
+ const patterns = [];
149
+
150
+ if (fs.existsSync(apiDir)) {
151
+ patterns.push({
152
+ from: apiDir,
153
+ to: distDir,
154
+ tsconfigPath
155
+ });
156
+ }
157
+
158
+ if (fs.existsSync(sharedDir)) {
159
+ patterns.push({
160
+ from: sharedDir,
161
+ to: distDir,
162
+ tsconfigPath
163
+ });
164
+ }
165
+
166
+ if (patterns.length > 0) {
167
+ await compile(appDirectory, modernConfig, {
168
+ patterns
169
+ });
170
+ }
153
171
  }
154
- }
155
172
 
156
- })
173
+ };
174
+ }
157
175
  }));
@@ -0,0 +1,38 @@
1
+ import Module from 'module';
2
+ import * as path from 'path';
3
+ const serverRuntimeAlias = '@modern-js/runtime/server';
4
+ const serverRuntimePath = '.runtime-exports/server';
5
+
6
+ const registerModernRuntimePath = internalDirectory => {
7
+ const originalResolveFilename = Module._resolveFilename; // eslint-disable-next-line node/no-unsupported-features/node-builtins
8
+
9
+ const {
10
+ builtinModules
11
+ } = Module;
12
+
13
+ Module._resolveFilename = function (request, _parent) {
14
+ const isCoreModule = builtinModules.includes(request);
15
+
16
+ if (!isCoreModule) {
17
+ if (request === serverRuntimeAlias) {
18
+ const found = path.join(internalDirectory, serverRuntimePath);
19
+
20
+ if (found) {
21
+ // eslint-disable-next-line prefer-rest-params
22
+ const modifiedArguments = [found, ...[].slice.call(arguments, 1)]; // Passes all arguments. Even those that is not specified above.
23
+
24
+ return originalResolveFilename.apply(this, modifiedArguments);
25
+ }
26
+ }
27
+ } // eslint-disable-next-line prefer-rest-params
28
+
29
+
30
+ return originalResolveFilename.apply(this, arguments);
31
+ };
32
+
33
+ return () => {
34
+ Module._resolveFilename = originalResolveFilename;
35
+ };
36
+ };
37
+
38
+ export { registerModernRuntimePath };
@@ -1,4 +1,4 @@
1
- import { generateClient } from '@modern-js/bff-utils';
1
+ import { generateClient } from '@modern-js/bff-core';
2
2
 
3
3
  async function loader(source) {
4
4
  // eslint-disable-next-line @babel/no-invalid-this
@@ -1,7 +1,11 @@
1
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
2
+
3
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
4
+
1
5
  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
6
 
3
7
  import path from 'path';
4
- import { injectAPIHandlerInfos } from '@modern-js/bff-utils';
8
+ import { ApiRouter } from '@modern-js/bff-core';
5
9
  import { API_DIR, isProd, requireExistModule } from '@modern-js/utils';
6
10
  import { API_APP_NAME } from "./constants";
7
11
 
@@ -68,7 +72,15 @@ export default (() => ({
68
72
  prefix
69
73
  } = props;
70
74
  const apiDir = path.resolve(pwd, API_DIR);
71
- injectAPIHandlerInfos(apiDir, prefix);
75
+ const appContext = api.useAppContext();
76
+ const apiRouter = new ApiRouter({
77
+ apiDir,
78
+ prefix
79
+ });
80
+ const apiHandlerInfos = apiRouter.getApiHandlers();
81
+ api.setAppContext(_objectSpread(_objectSpread({}, appContext), {}, {
82
+ apiHandlerInfos
83
+ }));
72
84
  return next(props);
73
85
  }
74
86
 
@@ -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 _helper = require("./helper");
17
+
16
18
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
19
 
18
20
  const DEFAULT_API_PREFIX = '/api';
@@ -61,121 +63,138 @@ const compile = async (appDirectory, modernConfig, compileOptions) => {
61
63
 
62
64
  var _default = () => ({
63
65
  name: '@modern-js/plugin-bff',
64
- setup: api => ({
65
- validateSchema() {
66
- return _utils.PLUGIN_SCHEMAS['@modern-js/plugin-bff'];
67
- },
68
-
69
- config() {
70
- return {
71
- tools: {
72
- webpackChain: (chain, {
73
- name,
74
- CHAIN_ID
75
- }) => {
76
- const {
77
- appDirectory,
78
- port
79
- } = api.useAppContext();
80
- const modernConfig = api.useResolvedConfigContext();
81
- const {
82
- bff
83
- } = modernConfig || {};
84
- const {
85
- fetcher
86
- } = bff || {};
87
- const prefix = (bff === null || bff === void 0 ? void 0 : bff.prefix) || DEFAULT_API_PREFIX;
88
-
89
- const rootDir = _path.default.resolve(appDirectory, _utils.API_DIR);
90
-
91
- chain.resolve.alias.set('@api', rootDir);
92
- const apiRegexp = new RegExp((0, _utils.normalizeOutputPath)(`${appDirectory}${_path.default.sep}api${_path.default.sep}.*(.[tj]s)$`));
93
- 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({
94
- prefix,
95
- apiDir: rootDir,
96
- port,
97
- fetcher,
98
- target: name,
99
- requestCreator: bff === null || bff === void 0 ? void 0 : bff.requestCreator
100
- });
66
+ setup: api => {
67
+ let unRegisterResolveRuntimePath = null;
68
+ return {
69
+ validateSchema() {
70
+ return _utils.PLUGIN_SCHEMAS['@modern-js/plugin-bff'];
71
+ },
72
+
73
+ config() {
74
+ return {
75
+ tools: {
76
+ webpackChain: (chain, {
77
+ name,
78
+ CHAIN_ID
79
+ }) => {
80
+ const {
81
+ appDirectory,
82
+ port
83
+ } = api.useAppContext();
84
+ const modernConfig = api.useResolvedConfigContext();
85
+ const {
86
+ bff
87
+ } = modernConfig || {};
88
+ const {
89
+ fetcher
90
+ } = bff || {};
91
+ const prefix = (bff === null || bff === void 0 ? void 0 : bff.prefix) || DEFAULT_API_PREFIX;
92
+
93
+ const rootDir = _path.default.resolve(appDirectory, _utils.API_DIR);
94
+
95
+ chain.resolve.alias.set('@api', rootDir);
96
+ const apiRegexp = new RegExp((0, _utils.normalizeOutputPath)(`${appDirectory}${_path.default.sep}api${_path.default.sep}.*(.[tj]s)$`));
97
+ 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
+ prefix,
99
+ apiDir: rootDir,
100
+ port,
101
+ fetcher,
102
+ target: name,
103
+ requestCreator: bff === null || bff === void 0 ? void 0 : bff.requestCreator
104
+ });
105
+ }
106
+ },
107
+ source: {
108
+ moduleScopes: [`./${_utils.API_DIR}`, /create-request/]
101
109
  }
102
- },
103
- source: {
104
- moduleScopes: [`./${_utils.API_DIR}`, /create-request/]
110
+ };
111
+ },
112
+
113
+ modifyServerRoutes({
114
+ routes
115
+ }) {
116
+ const modernConfig = api.useResolvedConfigContext();
117
+ const {
118
+ bff
119
+ } = modernConfig || {};
120
+ const prefix = (bff === null || bff === void 0 ? void 0 : bff.prefix) || '/api';
121
+ const prefixList = [];
122
+
123
+ if (Array.isArray(prefix)) {
124
+ prefixList.push(...prefix);
125
+ } else {
126
+ prefixList.push(prefix);
105
127
  }
106
- };
107
- },
108
-
109
- modifyServerRoutes({
110
- routes
111
- }) {
112
- const modernConfig = api.useResolvedConfigContext();
113
- const {
114
- bff
115
- } = modernConfig || {};
116
- const prefix = (bff === null || bff === void 0 ? void 0 : bff.prefix) || '/api';
117
- const prefixList = [];
118
-
119
- if (Array.isArray(prefix)) {
120
- prefixList.push(...prefix);
121
- } else {
122
- prefixList.push(prefix);
123
- }
124
128
 
125
- const apiServerRoutes = prefixList.map(pre => ({
126
- urlPath: pre,
127
- isApi: true,
128
- entryPath: '',
129
- isSPA: false,
130
- isSSR: false // FIXME: })) as IAppContext[`serverRoutes`];
129
+ const apiServerRoutes = prefixList.map(pre => ({
130
+ urlPath: pre,
131
+ isApi: true,
132
+ entryPath: '',
133
+ isSPA: false,
134
+ isSSR: false // FIXME: })) as IAppContext[`serverRoutes`];
135
+
136
+ }));
137
+ return {
138
+ routes: routes.concat(apiServerRoutes)
139
+ };
140
+ },
141
+
142
+ async beforeBuild() {
143
+ // help esbuild-register resolve @modern-js/server/runtime
144
+ if ((0, _utils.isProd)()) {
145
+ const {
146
+ internalDirectory
147
+ } = api.useAppContext();
148
+ unRegisterResolveRuntimePath = (0, _helper.registerModernRuntimePath)(internalDirectory);
149
+ }
150
+ },
131
151
 
132
- }));
133
- return {
134
- routes: routes.concat(apiServerRoutes)
135
- };
136
- },
152
+ async afterBuild() {
153
+ if (unRegisterResolveRuntimePath) {
154
+ unRegisterResolveRuntimePath();
155
+ }
137
156
 
138
- async afterBuild() {
139
- const {
140
- appDirectory,
141
- distDirectory
142
- } = api.useAppContext();
143
- const modernConfig = api.useResolvedConfigContext();
157
+ const {
158
+ appDirectory,
159
+ distDirectory
160
+ } = api.useAppContext();
161
+ const modernConfig = api.useResolvedConfigContext();
144
162
 
145
- const distDir = _path.default.resolve(distDirectory);
163
+ const distDir = _path.default.resolve(distDirectory);
146
164
 
147
- const apiDir = _path.default.resolve(appDirectory, _utils.API_DIR);
165
+ const apiDir = _path.default.resolve(appDirectory, _utils.API_DIR);
148
166
 
149
- const sharedDir = _path.default.resolve(appDirectory, _utils.SHARED_DIR);
167
+ const sharedDir = _path.default.resolve(appDirectory, _utils.SHARED_DIR);
150
168
 
151
- const tsconfigPath = _path.default.resolve(appDirectory, TS_CONFIG_FILENAME);
169
+ const tsconfigPath = _path.default.resolve(appDirectory, TS_CONFIG_FILENAME);
152
170
 
153
- const patterns = [];
171
+ const patterns = [];
154
172
 
155
- if (_utils.fs.existsSync(apiDir)) {
156
- patterns.push({
157
- from: apiDir,
158
- to: distDir,
159
- tsconfigPath
160
- });
161
- }
173
+ if (_utils.fs.existsSync(apiDir)) {
174
+ patterns.push({
175
+ from: apiDir,
176
+ to: distDir,
177
+ tsconfigPath
178
+ });
179
+ }
162
180
 
163
- if (_utils.fs.existsSync(sharedDir)) {
164
- patterns.push({
165
- from: sharedDir,
166
- to: distDir,
167
- tsconfigPath
168
- });
169
- }
181
+ if (_utils.fs.existsSync(sharedDir)) {
182
+ patterns.push({
183
+ from: sharedDir,
184
+ to: distDir,
185
+ tsconfigPath
186
+ });
187
+ }
170
188
 
171
- if (patterns.length > 0) {
172
- await compile(appDirectory, modernConfig, {
173
- patterns
174
- });
189
+ if (patterns.length > 0) {
190
+ await compile(appDirectory, modernConfig, {
191
+ patterns
192
+ });
193
+ }
175
194
  }
176
- }
177
195
 
178
- })
196
+ };
197
+ }
179
198
  });
180
199
 
181
200
  exports.default = _default;
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.registerModernRuntimePath = void 0;
7
+
8
+ var _module = _interopRequireDefault(require("module"));
9
+
10
+ var path = _interopRequireWildcard(require("path"));
11
+
12
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
13
+
14
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
15
+
16
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
+
18
+ const serverRuntimeAlias = '@modern-js/runtime/server';
19
+ const serverRuntimePath = '.runtime-exports/server';
20
+
21
+ const registerModernRuntimePath = internalDirectory => {
22
+ const originalResolveFilename = _module.default._resolveFilename; // eslint-disable-next-line node/no-unsupported-features/node-builtins
23
+
24
+ const {
25
+ builtinModules
26
+ } = _module.default;
27
+
28
+ _module.default._resolveFilename = function (request, _parent) {
29
+ const isCoreModule = builtinModules.includes(request);
30
+
31
+ if (!isCoreModule) {
32
+ if (request === serverRuntimeAlias) {
33
+ const found = path.join(internalDirectory, serverRuntimePath);
34
+
35
+ if (found) {
36
+ // eslint-disable-next-line prefer-rest-params
37
+ const modifiedArguments = [found, ...[].slice.call(arguments, 1)]; // Passes all arguments. Even those that is not specified above.
38
+
39
+ return originalResolveFilename.apply(this, modifiedArguments);
40
+ }
41
+ }
42
+ } // eslint-disable-next-line prefer-rest-params
43
+
44
+
45
+ return originalResolveFilename.apply(this, arguments);
46
+ };
47
+
48
+ return () => {
49
+ _module.default._resolveFilename = originalResolveFilename;
50
+ };
51
+ };
52
+
53
+ exports.registerModernRuntimePath = registerModernRuntimePath;
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
 
8
- var _bffUtils = require("@modern-js/bff-utils");
8
+ var _bffCore = require("@modern-js/bff-core");
9
9
 
10
10
  async function loader(source) {
11
11
  // eslint-disable-next-line @babel/no-invalid-this
@@ -33,7 +33,7 @@ async function loader(source) {
33
33
  }
34
34
 
35
35
  options.requireResolve = require.resolve;
36
- const result = await (0, _bffUtils.generateClient)(options);
36
+ const result = await (0, _bffCore.generateClient)(options);
37
37
 
38
38
  if (result.isOk) {
39
39
  callback(undefined, result.value);
@@ -7,7 +7,7 @@ exports.default = void 0;
7
7
 
8
8
  var _path = _interopRequireDefault(require("path"));
9
9
 
10
- var _bffUtils = require("@modern-js/bff-utils");
10
+ var _bffCore = require("@modern-js/bff-core");
11
11
 
12
12
  var _utils = require("@modern-js/utils");
13
13
 
@@ -15,6 +15,10 @@ var _constants = require("./constants");
15
15
 
16
16
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
17
 
18
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
19
+
20
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
21
+
18
22
  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; }
19
23
 
20
24
  class Storage {
@@ -84,7 +88,15 @@ var _default = () => ({
84
88
 
85
89
  const apiDir = _path.default.resolve(pwd, _utils.API_DIR);
86
90
 
87
- (0, _bffUtils.injectAPIHandlerInfos)(apiDir, prefix);
91
+ const appContext = api.useAppContext();
92
+ const apiRouter = new _bffCore.ApiRouter({
93
+ apiDir,
94
+ prefix
95
+ });
96
+ const apiHandlerInfos = apiRouter.getApiHandlers();
97
+ api.setAppContext(_objectSpread(_objectSpread({}, appContext), {}, {
98
+ apiHandlerInfos
99
+ }));
88
100
  return next(props);
89
101
  }
90
102
 
@@ -0,0 +1,2 @@
1
+ declare const registerModernRuntimePath: (internalDirectory: string) => () => void;
2
+ export { registerModernRuntimePath };
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.5.2",
14
+ "version": "1.6.0-alpha.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,18 +55,18 @@
55
55
  "dependencies": {
56
56
  "@babel/core": "^7.18.0",
57
57
  "@babel/runtime": "^7.18.0",
58
- "@modern-js/babel-compiler": "^1.2.6",
59
- "@modern-js/bff-utils": "^1.2.9",
60
- "@modern-js/create-request": "^1.2.11",
61
- "@modern-js/server-utils": "^1.2.10",
62
- "@modern-js/utils": "^1.7.6"
58
+ "@modern-js/babel-compiler": "^1.2.7-alpha.0",
59
+ "@modern-js/bff-core": "^1.1.0-alpha.0",
60
+ "@modern-js/create-request": "^1.3.0-alpha.0",
61
+ "@modern-js/server-utils": "^1.2.12-alpha.0",
62
+ "@modern-js/utils": "^1.7.9-alpha.0"
63
63
  },
64
64
  "devDependencies": {
65
- "@modern-js/core": "1.11.2",
66
- "@modern-js/plugin-analyze": "1.4.6",
67
- "@modern-js/runtime": "1.3.2",
68
- "@modern-js/server-core": "1.3.5",
69
- "@modern-js/types": "1.5.4",
65
+ "@modern-js/core": "1.12.2-alpha.0",
66
+ "@modern-js/plugin-analyze": "1.4.7-alpha.0",
67
+ "@modern-js/runtime": "1.3.4-alpha.0",
68
+ "@modern-js/server-core": "1.4.0-alpha.0",
69
+ "@modern-js/types": "1.5.5-alpha.0",
70
70
  "@scripts/build": "0.0.0",
71
71
  "@scripts/jest-config": "0.0.0",
72
72
  "@types/babel__core": "^7.1.15",