@modern-js/plugin-bff 1.6.2 → 1.15.1-beta.1

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,24 @@
1
1
  # @modern-js/plugin-bff
2
2
 
3
+ ## 1.15.0
4
+
5
+ ### Patch Changes
6
+
7
+ - c0d8dac: fix: remove package adapter-helpers and bff runtime export
8
+ - Updated dependencies [c0d8dac]
9
+ - Updated dependencies [8658a78]
10
+ - Updated dependencies [05d4a4f]
11
+ - Updated dependencies [7bfaaf9]
12
+ - Updated dependencies [b1f7000]
13
+ - Updated dependencies [ad05af9]
14
+ - Updated dependencies [5d53d1c]
15
+ - Updated dependencies [37cd159]
16
+ - @modern-js/bff-core@1.15.0
17
+ - @modern-js/utils@1.15.0
18
+ - @modern-js/create-request@1.15.0
19
+ - @modern-js/server-utils@1.15.0
20
+ - @modern-js/babel-compiler@1.15.0
21
+
3
22
  ## 1.6.2
4
23
 
5
24
  ### Patch Changes
@@ -1,50 +1,9 @@
1
1
  import path from 'path';
2
- import { compiler } from '@modern-js/babel-compiler';
3
2
  import { fs, API_DIR, PLUGIN_SCHEMAS, normalizeOutputPath, SHARED_DIR, isProd } from '@modern-js/utils';
4
- import { resolveBabelConfig } from '@modern-js/server-utils';
3
+ import { compile } from '@modern-js/server-utils';
5
4
  import { registerModernRuntimePath } from "./helper";
6
5
  const DEFAULT_API_PREFIX = '/api';
7
6
  const TS_CONFIG_FILENAME = 'tsconfig.json';
8
- const FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs']; // TODO: 封装服务端编译函数
9
-
10
- const compile = async (appDirectory, modernConfig, compileOptions) => {
11
- const {
12
- patterns
13
- } = compileOptions;
14
- const results = await Promise.all(patterns.map(async pattern => {
15
- const {
16
- from,
17
- to,
18
- tsconfigPath
19
- } = pattern;
20
- const babelConfig = resolveBabelConfig(appDirectory, modernConfig, {
21
- tsconfigPath: tsconfigPath ? tsconfigPath : '',
22
- syntax: 'es6+',
23
- type: 'commonjs'
24
- });
25
-
26
- if (await fs.pathExists(from)) {
27
- const basename = path.basename(from);
28
- const targetDir = path.join(to, basename);
29
- await fs.copy(from, targetDir, {
30
- filter: src => !['.ts', '.js'].includes(path.extname(src)) && src !== tsconfigPath
31
- });
32
- }
33
-
34
- return compiler({
35
- rootDir: appDirectory,
36
- distDir: to,
37
- sourceDir: from,
38
- extensions: FILE_EXTENSIONS
39
- }, babelConfig);
40
- }));
41
- results.forEach(result => {
42
- if (result.code === 1) {
43
- throw new Error(result.message);
44
- }
45
- });
46
- };
47
-
48
7
  export default (() => ({
49
8
  name: '@modern-js/plugin-bff',
50
9
  setup: api => {
@@ -145,27 +104,21 @@ export default (() => ({
145
104
  const apiDir = path.resolve(appDirectory, API_DIR);
146
105
  const sharedDir = path.resolve(appDirectory, SHARED_DIR);
147
106
  const tsconfigPath = path.resolve(appDirectory, TS_CONFIG_FILENAME);
148
- const patterns = [];
107
+ const sourceDirs = [];
149
108
 
150
109
  if (fs.existsSync(apiDir)) {
151
- patterns.push({
152
- from: apiDir,
153
- to: distDir,
154
- tsconfigPath
155
- });
110
+ sourceDirs.push(apiDir);
156
111
  }
157
112
 
158
113
  if (fs.existsSync(sharedDir)) {
159
- patterns.push({
160
- from: sharedDir,
161
- to: distDir,
162
- tsconfigPath
163
- });
114
+ sourceDirs.push(sharedDir);
164
115
  }
165
116
 
166
- if (patterns.length > 0) {
117
+ if (sourceDirs.length > 0) {
167
118
  await compile(appDirectory, modernConfig, {
168
- patterns
119
+ sourceDirs,
120
+ distDir,
121
+ tsconfigPath
169
122
  });
170
123
  }
171
124
  }
@@ -7,8 +7,6 @@ exports.default = void 0;
7
7
 
8
8
  var _path = _interopRequireDefault(require("path"));
9
9
 
10
- var _babelCompiler = require("@modern-js/babel-compiler");
11
-
12
10
  var _utils = require("@modern-js/utils");
13
11
 
14
12
  var _serverUtils = require("@modern-js/server-utils");
@@ -19,47 +17,6 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
19
17
 
20
18
  const DEFAULT_API_PREFIX = '/api';
21
19
  const TS_CONFIG_FILENAME = 'tsconfig.json';
22
- const FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs']; // TODO: 封装服务端编译函数
23
-
24
- const compile = async (appDirectory, modernConfig, compileOptions) => {
25
- const {
26
- patterns
27
- } = compileOptions;
28
- const results = await Promise.all(patterns.map(async pattern => {
29
- const {
30
- from,
31
- to,
32
- tsconfigPath
33
- } = pattern;
34
- const babelConfig = (0, _serverUtils.resolveBabelConfig)(appDirectory, modernConfig, {
35
- tsconfigPath: tsconfigPath ? tsconfigPath : '',
36
- syntax: 'es6+',
37
- type: 'commonjs'
38
- });
39
-
40
- if (await _utils.fs.pathExists(from)) {
41
- const basename = _path.default.basename(from);
42
-
43
- const targetDir = _path.default.join(to, basename);
44
-
45
- await _utils.fs.copy(from, targetDir, {
46
- filter: src => !['.ts', '.js'].includes(_path.default.extname(src)) && src !== tsconfigPath
47
- });
48
- }
49
-
50
- return (0, _babelCompiler.compiler)({
51
- rootDir: appDirectory,
52
- distDir: to,
53
- sourceDir: from,
54
- extensions: FILE_EXTENSIONS
55
- }, babelConfig);
56
- }));
57
- results.forEach(result => {
58
- if (result.code === 1) {
59
- throw new Error(result.message);
60
- }
61
- });
62
- };
63
20
 
64
21
  var _default = () => ({
65
22
  name: '@modern-js/plugin-bff',
@@ -168,27 +125,21 @@ var _default = () => ({
168
125
 
169
126
  const tsconfigPath = _path.default.resolve(appDirectory, TS_CONFIG_FILENAME);
170
127
 
171
- const patterns = [];
128
+ const sourceDirs = [];
172
129
 
173
130
  if (_utils.fs.existsSync(apiDir)) {
174
- patterns.push({
175
- from: apiDir,
176
- to: distDir,
177
- tsconfigPath
178
- });
131
+ sourceDirs.push(apiDir);
179
132
  }
180
133
 
181
134
  if (_utils.fs.existsSync(sharedDir)) {
182
- patterns.push({
183
- from: sharedDir,
184
- to: distDir,
185
- tsconfigPath
186
- });
135
+ sourceDirs.push(sharedDir);
187
136
  }
188
137
 
189
- if (patterns.length > 0) {
190
- await compile(appDirectory, modernConfig, {
191
- patterns
138
+ if (sourceDirs.length > 0) {
139
+ await (0, _serverUtils.compile)(appDirectory, modernConfig, {
140
+ sourceDirs,
141
+ distDir,
142
+ tsconfigPath
192
143
  });
193
144
  }
194
145
  }
@@ -0,0 +1,160 @@
1
+ import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
2
+ import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
3
+ import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
4
+ import path from 'path';
5
+ import { fs, API_DIR, PLUGIN_SCHEMAS, normalizeOutputPath, SHARED_DIR, isProd } from '@modern-js/utils';
6
+ import { compile } from '@modern-js/server-utils';
7
+ import { registerModernRuntimePath } from "./helper";
8
+ var DEFAULT_API_PREFIX = '/api';
9
+ var TS_CONFIG_FILENAME = 'tsconfig.json';
10
+ export default (function () {
11
+ return {
12
+ name: '@modern-js/plugin-bff',
13
+ setup: function setup(api) {
14
+ var unRegisterResolveRuntimePath = null;
15
+ return {
16
+ validateSchema: function validateSchema() {
17
+ return PLUGIN_SCHEMAS['@modern-js/plugin-bff'];
18
+ },
19
+ config: function config() {
20
+ return {
21
+ tools: {
22
+ webpackChain: function webpackChain(chain, _ref) {
23
+ var name = _ref.name,
24
+ CHAIN_ID = _ref.CHAIN_ID;
25
+
26
+ var _api$useAppContext = api.useAppContext(),
27
+ appDirectory = _api$useAppContext.appDirectory,
28
+ port = _api$useAppContext.port;
29
+
30
+ var modernConfig = api.useResolvedConfigContext();
31
+
32
+ var _ref2 = modernConfig || {},
33
+ bff = _ref2.bff;
34
+
35
+ var _ref3 = bff || {},
36
+ fetcher = _ref3.fetcher;
37
+
38
+ var prefix = (bff === null || bff === void 0 ? void 0 : bff.prefix) || DEFAULT_API_PREFIX;
39
+ var rootDir = path.resolve(appDirectory, API_DIR);
40
+ chain.resolve.alias.set('@api', rootDir);
41
+ var apiRegexp = new RegExp(normalizeOutputPath("".concat(appDirectory).concat(path.sep, "api").concat(path.sep, ".*(.[tj]s)$")));
42
+ 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({
43
+ prefix: prefix,
44
+ apiDir: rootDir,
45
+ port: port,
46
+ fetcher: fetcher,
47
+ target: name,
48
+ requestCreator: bff === null || bff === void 0 ? void 0 : bff.requestCreator
49
+ });
50
+ }
51
+ },
52
+ source: {
53
+ moduleScopes: ["./".concat(API_DIR), /create-request/]
54
+ }
55
+ };
56
+ },
57
+ modifyServerRoutes: function modifyServerRoutes(_ref4) {
58
+ var routes = _ref4.routes;
59
+ var modernConfig = api.useResolvedConfigContext();
60
+
61
+ var _ref5 = modernConfig || {},
62
+ bff = _ref5.bff;
63
+
64
+ var prefix = (bff === null || bff === void 0 ? void 0 : bff.prefix) || '/api';
65
+ var prefixList = [];
66
+
67
+ if (Array.isArray(prefix)) {
68
+ prefixList.push.apply(prefixList, _toConsumableArray(prefix));
69
+ } else {
70
+ prefixList.push(prefix);
71
+ }
72
+
73
+ var apiServerRoutes = prefixList.map(function (pre) {
74
+ return {
75
+ urlPath: pre,
76
+ isApi: true,
77
+ entryPath: '',
78
+ isSPA: false,
79
+ isSSR: false // FIXME: })) as IAppContext[`serverRoutes`];
80
+
81
+ };
82
+ });
83
+ return {
84
+ routes: routes.concat(apiServerRoutes)
85
+ };
86
+ },
87
+ beforeBuild: function beforeBuild() {
88
+ return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
89
+ var _api$useAppContext2, internalDirectory;
90
+
91
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
92
+ while (1) {
93
+ switch (_context.prev = _context.next) {
94
+ case 0:
95
+ // help esbuild-register resolve @modern-js/server/runtime
96
+ if (isProd()) {
97
+ _api$useAppContext2 = api.useAppContext(), internalDirectory = _api$useAppContext2.internalDirectory;
98
+ unRegisterResolveRuntimePath = registerModernRuntimePath(internalDirectory);
99
+ }
100
+
101
+ case 1:
102
+ case "end":
103
+ return _context.stop();
104
+ }
105
+ }
106
+ }, _callee);
107
+ }))();
108
+ },
109
+ afterBuild: function afterBuild() {
110
+ return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
111
+ var _api$useAppContext3, appDirectory, distDirectory, modernConfig, distDir, apiDir, sharedDir, tsconfigPath, sourceDirs;
112
+
113
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
114
+ while (1) {
115
+ switch (_context2.prev = _context2.next) {
116
+ case 0:
117
+ if (unRegisterResolveRuntimePath) {
118
+ unRegisterResolveRuntimePath();
119
+ }
120
+
121
+ _api$useAppContext3 = api.useAppContext(), appDirectory = _api$useAppContext3.appDirectory, distDirectory = _api$useAppContext3.distDirectory;
122
+ modernConfig = api.useResolvedConfigContext();
123
+ distDir = path.resolve(distDirectory);
124
+ apiDir = path.resolve(appDirectory, API_DIR);
125
+ sharedDir = path.resolve(appDirectory, SHARED_DIR);
126
+ tsconfigPath = path.resolve(appDirectory, TS_CONFIG_FILENAME);
127
+ sourceDirs = [];
128
+
129
+ if (fs.existsSync(apiDir)) {
130
+ sourceDirs.push(apiDir);
131
+ }
132
+
133
+ if (fs.existsSync(sharedDir)) {
134
+ sourceDirs.push(sharedDir);
135
+ }
136
+
137
+ if (!(sourceDirs.length > 0)) {
138
+ _context2.next = 13;
139
+ break;
140
+ }
141
+
142
+ _context2.next = 13;
143
+ return compile(appDirectory, modernConfig, {
144
+ sourceDirs: sourceDirs,
145
+ distDir: distDir,
146
+ tsconfigPath: tsconfigPath
147
+ });
148
+
149
+ case 13:
150
+ case "end":
151
+ return _context2.stop();
152
+ }
153
+ }
154
+ }, _callee2);
155
+ }))();
156
+ }
157
+ };
158
+ }
159
+ };
160
+ });
@@ -0,0 +1,2 @@
1
+ export var API_APP_NAME = '_app';
2
+ export var BUILD_FILES = ['**/*.[tj]sx?', '!**/*.test.jsx?', '!**/*.test.tsx?', '!**/*.spec.jsx?', '!**/*.spec.tsx?', '!__tests__/*.tsx?', '!__tests__/*.jsx?'];
@@ -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,63 @@
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
+
5
+ function loader(_x) {
6
+ return _loader.apply(this, arguments);
7
+ }
8
+
9
+ function _loader() {
10
+ _loader = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(source) {
11
+ var callback, draftOptions, options, result;
12
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
13
+ while (1) {
14
+ switch (_context.prev = _context.next) {
15
+ case 0:
16
+ // eslint-disable-next-line @babel/no-invalid-this
17
+ this.cacheable(); // eslint-disable-next-line @babel/no-invalid-this
18
+
19
+ callback = this.async(); // eslint-disable-next-line @babel/no-invalid-this
20
+
21
+ draftOptions = this.getOptions();
22
+ options = {
23
+ prefix: Array.isArray(draftOptions.prefix) ? draftOptions.prefix[0] : draftOptions.prefix,
24
+ apiDir: draftOptions.apiDir,
25
+ target: draftOptions.target,
26
+ port: Number(draftOptions.port),
27
+ source: source,
28
+ // eslint-disable-next-line @babel/no-invalid-this
29
+ resourcePath: this.resourcePath
30
+ };
31
+
32
+ if (draftOptions.fetcher) {
33
+ options.fetcher = draftOptions.fetcher;
34
+ }
35
+
36
+ if (draftOptions.requestCreator) {
37
+ options.requestCreator = draftOptions.requestCreator;
38
+ }
39
+
40
+ options.requireResolve = require.resolve;
41
+ _context.next = 9;
42
+ return generateClient(options);
43
+
44
+ case 9:
45
+ result = _context.sent;
46
+
47
+ if (result.isOk) {
48
+ callback(undefined, result.value);
49
+ } else {
50
+ callback(undefined, "throw new Error('".concat(result.value, "')"));
51
+ }
52
+
53
+ case 11:
54
+ case "end":
55
+ return _context.stop();
56
+ }
57
+ }
58
+ }, _callee, this);
59
+ }));
60
+ return _loader.apply(this, arguments);
61
+ }
62
+
63
+ 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/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.6.2",
14
+ "version": "1.15.1-beta.1",
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": "^1.2.6",
59
- "@modern-js/bff-core": "^1.1.2",
60
- "@modern-js/create-request": "^1.3.0",
61
- "@modern-js/server-utils": "^1.2.11",
62
- "@modern-js/utils": "^1.7.12"
58
+ "@modern-js/babel-compiler": "1.15.0",
59
+ "@modern-js/bff-core": "1.15.0",
60
+ "@modern-js/create-request": "1.15.0",
61
+ "@modern-js/server-utils": "1.15.1-beta.1",
62
+ "@modern-js/utils": "1.15.0"
63
63
  },
64
64
  "devDependencies": {
65
- "@modern-js/core": "1.13.0",
66
- "@modern-js/plugin-analyze": "1.5.0",
67
- "@modern-js/runtime": "1.3.5",
68
- "@modern-js/server-core": "1.4.1",
69
- "@modern-js/types": "1.6.0",
70
- "@scripts/build": "0.0.0",
71
- "@scripts/jest-config": "0.0.0",
65
+ "@modern-js/core": "1.15.0",
66
+ "@modern-js/bff-runtime": "1.15.0",
67
+ "@modern-js/runtime": "1.15.0",
68
+ "@modern-js/server-core": "1.15.0",
69
+ "@modern-js/types": "1.15.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,7 +82,8 @@
82
82
  "sideEffects": false,
83
83
  "publishConfig": {
84
84
  "registry": "https://registry.npmjs.org/",
85
- "access": "public"
85
+ "access": "public",
86
+ "types": "./dist/types/index.d.ts"
86
87
  },
87
88
  "wireit": {
88
89
  "build": {