@modern-js/plugin-bff 1.5.1 → 1.5.3-beta.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,17 @@
1
1
  # @modern-js/plugin-bff
2
2
 
3
+ ## 1.5.2
4
+
5
+ ### Patch Changes
6
+
7
+ - a1198d509: feat: bump babel 7.18.0
8
+ - c1a4ead09: fix: plugin-bff should compile shared directory
9
+ - Updated dependencies [a1198d509]
10
+ - @modern-js/bff-utils@1.2.9
11
+ - @modern-js/create-request@1.2.11
12
+ - @modern-js/server-utils@1.2.10
13
+ - @modern-js/babel-compiler@1.2.6
14
+
3
15
  ## 1.5.1
4
16
 
5
17
  ### Patch Changes
@@ -1,10 +1,49 @@
1
1
  import path from 'path';
2
2
  import { compiler } from '@modern-js/babel-compiler';
3
- import { fs, API_DIR, PLUGIN_SCHEMAS, normalizeOutputPath } from '@modern-js/utils';
3
+ import { fs, API_DIR, PLUGIN_SCHEMAS, normalizeOutputPath, SHARED_DIR } from '@modern-js/utils';
4
4
  import { resolveBabelConfig } from '@modern-js/server-utils';
5
5
  const DEFAULT_API_PREFIX = '/api';
6
6
  const TS_CONFIG_FILENAME = 'tsconfig.json';
7
- const FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs'];
7
+ const FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs']; // TODO: 封装服务端编译函数
8
+
9
+ const compile = async (appDirectory, modernConfig, compileOptions) => {
10
+ const {
11
+ patterns
12
+ } = compileOptions;
13
+ const results = await Promise.all(patterns.map(async pattern => {
14
+ const {
15
+ from,
16
+ to,
17
+ tsconfigPath
18
+ } = pattern;
19
+ const babelConfig = resolveBabelConfig(appDirectory, modernConfig, {
20
+ tsconfigPath: tsconfigPath ? tsconfigPath : '',
21
+ syntax: 'es6+',
22
+ type: 'commonjs'
23
+ });
24
+
25
+ if (await fs.pathExists(from)) {
26
+ const basename = path.basename(from);
27
+ const targetDir = path.join(to, basename);
28
+ await fs.copy(from, targetDir, {
29
+ filter: src => !['.ts', '.js'].includes(path.extname(src)) && src !== tsconfigPath
30
+ });
31
+ }
32
+
33
+ return compiler({
34
+ rootDir: appDirectory,
35
+ distDir: to,
36
+ sourceDir: from,
37
+ extensions: FILE_EXTENSIONS
38
+ }, babelConfig);
39
+ }));
40
+ results.forEach(result => {
41
+ if (result.code === 1) {
42
+ throw new Error(result.message);
43
+ }
44
+ });
45
+ };
46
+
8
47
  export default (() => ({
9
48
  name: '@modern-js/plugin-bff',
10
49
  setup: api => ({
@@ -85,33 +124,32 @@ export default (() => ({
85
124
  distDirectory
86
125
  } = api.useAppContext();
87
126
  const modernConfig = api.useResolvedConfigContext();
88
- const rootDir = path.resolve(appDirectory, API_DIR);
89
- const distDir = path.resolve(distDirectory, API_DIR);
90
- const sourceAbsDir = path.resolve(appDirectory, API_DIR);
127
+ const distDir = path.resolve(distDirectory);
128
+ const apiDir = path.resolve(appDirectory, API_DIR);
129
+ const sharedDir = path.resolve(appDirectory, SHARED_DIR);
91
130
  const tsconfigPath = path.resolve(appDirectory, TS_CONFIG_FILENAME);
92
- const babelConfig = resolveBabelConfig(appDirectory, modernConfig, {
93
- tsconfigPath,
94
- syntax: 'es6+',
95
- type: 'commonjs'
96
- });
97
- const result = await compiler({
98
- rootDir,
99
- distDir,
100
- sourceDir: sourceAbsDir,
101
- extensions: FILE_EXTENSIONS,
102
- ignore: [`**/__tests__/**`, '*.d.ts', '*.test.ts']
103
- }, babelConfig);
131
+ const patterns = [];
104
132
 
105
- if (await fs.pathExists(rootDir)) {
106
- await fs.copy(rootDir, distDir, {
107
- filter: src => !['.ts', '.js'].includes(path.extname(src)) && src !== tsconfigPath
133
+ if (fs.existsSync(apiDir)) {
134
+ patterns.push({
135
+ from: apiDir,
136
+ to: distDir,
137
+ tsconfigPath
108
138
  });
109
139
  }
110
140
 
111
- if (result.code === 1) {
112
- var _result$messageDetail;
141
+ if (fs.existsSync(sharedDir)) {
142
+ patterns.push({
143
+ from: sharedDir,
144
+ to: distDir,
145
+ tsconfigPath
146
+ });
147
+ }
113
148
 
114
- throw new Error((result === null || result === void 0 ? void 0 : (_result$messageDetail = result.messageDetails) === null || _result$messageDetail === void 0 ? void 0 : _result$messageDetail[0].content) || result.message);
149
+ if (patterns.length > 0) {
150
+ await compile(appDirectory, modernConfig, {
151
+ patterns
152
+ });
115
153
  }
116
154
  }
117
155
 
@@ -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
 
@@ -17,7 +17,47 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
17
17
 
18
18
  const DEFAULT_API_PREFIX = '/api';
19
19
  const TS_CONFIG_FILENAME = 'tsconfig.json';
20
- const FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs'];
20
+ const FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs']; // TODO: 封装服务端编译函数
21
+
22
+ const compile = async (appDirectory, modernConfig, compileOptions) => {
23
+ const {
24
+ patterns
25
+ } = compileOptions;
26
+ const results = await Promise.all(patterns.map(async pattern => {
27
+ const {
28
+ from,
29
+ to,
30
+ tsconfigPath
31
+ } = pattern;
32
+ const babelConfig = (0, _serverUtils.resolveBabelConfig)(appDirectory, modernConfig, {
33
+ tsconfigPath: tsconfigPath ? tsconfigPath : '',
34
+ syntax: 'es6+',
35
+ type: 'commonjs'
36
+ });
37
+
38
+ if (await _utils.fs.pathExists(from)) {
39
+ const basename = _path.default.basename(from);
40
+
41
+ const targetDir = _path.default.join(to, basename);
42
+
43
+ await _utils.fs.copy(from, targetDir, {
44
+ filter: src => !['.ts', '.js'].includes(_path.default.extname(src)) && src !== tsconfigPath
45
+ });
46
+ }
47
+
48
+ return (0, _babelCompiler.compiler)({
49
+ rootDir: appDirectory,
50
+ distDir: to,
51
+ sourceDir: from,
52
+ extensions: FILE_EXTENSIONS
53
+ }, babelConfig);
54
+ }));
55
+ results.forEach(result => {
56
+ if (result.code === 1) {
57
+ throw new Error(result.message);
58
+ }
59
+ });
60
+ };
21
61
 
22
62
  var _default = () => ({
23
63
  name: '@modern-js/plugin-bff',
@@ -102,37 +142,36 @@ var _default = () => ({
102
142
  } = api.useAppContext();
103
143
  const modernConfig = api.useResolvedConfigContext();
104
144
 
105
- const rootDir = _path.default.resolve(appDirectory, _utils.API_DIR);
145
+ const distDir = _path.default.resolve(distDirectory);
106
146
 
107
- const distDir = _path.default.resolve(distDirectory, _utils.API_DIR);
147
+ const apiDir = _path.default.resolve(appDirectory, _utils.API_DIR);
108
148
 
109
- const sourceAbsDir = _path.default.resolve(appDirectory, _utils.API_DIR);
149
+ const sharedDir = _path.default.resolve(appDirectory, _utils.SHARED_DIR);
110
150
 
111
151
  const tsconfigPath = _path.default.resolve(appDirectory, TS_CONFIG_FILENAME);
112
152
 
113
- const babelConfig = (0, _serverUtils.resolveBabelConfig)(appDirectory, modernConfig, {
114
- tsconfigPath,
115
- syntax: 'es6+',
116
- type: 'commonjs'
117
- });
118
- const result = await (0, _babelCompiler.compiler)({
119
- rootDir,
120
- distDir,
121
- sourceDir: sourceAbsDir,
122
- extensions: FILE_EXTENSIONS,
123
- ignore: [`**/__tests__/**`, '*.d.ts', '*.test.ts']
124
- }, babelConfig);
125
-
126
- if (await _utils.fs.pathExists(rootDir)) {
127
- await _utils.fs.copy(rootDir, distDir, {
128
- filter: src => !['.ts', '.js'].includes(_path.default.extname(src)) && src !== tsconfigPath
153
+ const patterns = [];
154
+
155
+ if (_utils.fs.existsSync(apiDir)) {
156
+ patterns.push({
157
+ from: apiDir,
158
+ to: distDir,
159
+ tsconfigPath
129
160
  });
130
161
  }
131
162
 
132
- if (result.code === 1) {
133
- var _result$messageDetail;
163
+ if (_utils.fs.existsSync(sharedDir)) {
164
+ patterns.push({
165
+ from: sharedDir,
166
+ to: distDir,
167
+ tsconfigPath
168
+ });
169
+ }
134
170
 
135
- throw new Error((result === null || result === void 0 ? void 0 : (_result$messageDetail = result.messageDetails) === null || _result$messageDetail === void 0 ? void 0 : _result$messageDetail[0].content) || result.message);
171
+ if (patterns.length > 0) {
172
+ await compile(appDirectory, modernConfig, {
173
+ patterns
174
+ });
136
175
  }
137
176
  }
138
177
 
@@ -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
 
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.5.1",
14
+ "version": "1.5.3-beta.0",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -56,15 +56,15 @@
56
56
  "@babel/core": "^7.17.0",
57
57
  "@babel/runtime": "^7",
58
58
  "@modern-js/babel-compiler": "^1.2.5",
59
- "@modern-js/bff-utils": "^1.2.8",
59
+ "@modern-js/bff-core": "^1.0.0",
60
60
  "@modern-js/create-request": "^1.2.10",
61
61
  "@modern-js/server-utils": "^1.2.9",
62
62
  "@modern-js/utils": "^1.7.6"
63
63
  },
64
64
  "devDependencies": {
65
- "@modern-js/core": "1.11.1",
66
- "@modern-js/plugin-analyze": "1.4.5",
67
- "@modern-js/runtime": "1.3.0",
65
+ "@modern-js/core": "1.11.2",
66
+ "@modern-js/plugin-analyze": "1.4.6",
67
+ "@modern-js/runtime": "1.3.2",
68
68
  "@modern-js/server-core": "1.3.5",
69
69
  "@modern-js/types": "1.5.4",
70
70
  "@scripts/build": "0.0.0",
@@ -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": {