@modern-js/plugin-bff 1.1.1 → 1.1.2-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.
@@ -2,9 +2,8 @@ import path from 'path';
2
2
  import fs from 'fs-extra';
3
3
  import { createPlugin, useAppContext, useResolvedConfigContext } from '@modern-js/core';
4
4
  import { compiler } from '@modern-js/babel-compiler';
5
- import { PLUGIN_SCHEMAS, normalizeOutputPath } from '@modern-js/utils';
5
+ import { PLUGIN_SCHEMAS, normalizeOutputPath, API_DIR } from '@modern-js/utils';
6
6
  import { resolveBabelConfig } from '@modern-js/server-utils';
7
- import { API_DIR } from "./constants";
8
7
  const DEFAULT_API_PREFIX = '/api';
9
8
  const TS_CONFIG_FILENAME = 'tsconfig.json';
10
9
  const FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs'];
@@ -41,7 +40,8 @@ export default createPlugin(() => ({
41
40
  apiDir: rootDir,
42
41
  port,
43
42
  fetcher,
44
- target: _config.name
43
+ target: _config.name,
44
+ requestCreator: bff === null || bff === void 0 ? void 0 : bff.requestCreator
45
45
  });
46
46
  }
47
47
  },
@@ -1,2 +1,2 @@
1
- export const API_DIR = 'api';
1
+ export const API_APP_NAME = '_app';
2
2
  export const BUILD_FILES = ['**/*.[tj]sx?', '!**/*.test.jsx?', '!**/*.test.tsx?', '!**/*.spec.jsx?', '!**/*.spec.tsx?', '!__tests__/*.tsx?', '!__tests__/*.jsx?'];
@@ -9,7 +9,7 @@ async function loader(source) {
9
9
 
10
10
  const draftOptions = getOptions(this);
11
11
  const options = {
12
- prefix: draftOptions.prefix,
12
+ prefix: Array.isArray(draftOptions.prefix) ? draftOptions.prefix[0] : draftOptions.prefix,
13
13
  apiDir: draftOptions.apiDir,
14
14
  target: draftOptions.target,
15
15
  port: Number(draftOptions.port),
@@ -1,18 +1,73 @@
1
1
  import path from 'path';
2
2
  import { createPlugin } from '@modern-js/server-plugin';
3
3
  import { injectAPIHandlerInfos } from '@modern-js/bff-utils';
4
- import { API_DIR } from "./constants";
5
- export default createPlugin(() => ({
6
- prepareApiServer(props, next) {
7
- const {
8
- pwd,
9
- prefix
10
- } = props;
11
- const apiDir = path.resolve(pwd, API_DIR);
12
- injectAPIHandlerInfos(apiDir, prefix);
13
- return next(props);
4
+ import { useAppContext } from '@modern-js/core';
5
+ import { API_DIR, isProd, requireExistModule } from '@modern-js/utils';
6
+ import { API_APP_NAME } from "./constants";
7
+
8
+ class Storage {
9
+ constructor() {
10
+ this.middlewares = [];
11
+ }
12
+
13
+ reset() {
14
+ this.middlewares = [];
15
+ }
16
+
17
+ }
18
+
19
+ const createTransformAPI = storage => ({
20
+ addMiddleware(fn) {
21
+ storage.middlewares.push(fn);
22
+ }
23
+
24
+ });
25
+
26
+ export default createPlugin(() => {
27
+ const {
28
+ appDirectory,
29
+ distDirectory
30
+ } = useAppContext();
31
+ const root = isProd() ? distDirectory : appDirectory;
32
+ const apiPath = path.resolve(root || process.cwd(), API_DIR);
33
+ const apiAppPath = path.resolve(apiPath, API_APP_NAME);
34
+ const storage = new Storage();
35
+ const transformAPI = createTransformAPI(storage);
36
+ const apiMod = requireExistModule(apiAppPath);
37
+
38
+ if (apiMod) {
39
+ apiMod(transformAPI);
14
40
  }
15
41
 
16
- }), {
42
+ return {
43
+ reset() {
44
+ storage.reset();
45
+ const newApiModule = requireExistModule(apiAppPath);
46
+
47
+ if (newApiModule) {
48
+ newApiModule(transformAPI);
49
+ }
50
+ },
51
+
52
+ gather({
53
+ addAPIMiddleware
54
+ }) {
55
+ storage.middlewares.forEach(mid => {
56
+ addAPIMiddleware(mid);
57
+ });
58
+ },
59
+
60
+ prepareApiServer(props, next) {
61
+ const {
62
+ pwd,
63
+ prefix
64
+ } = props;
65
+ const apiDir = path.resolve(pwd, API_DIR);
66
+ injectAPIHandlerInfos(apiDir, prefix);
67
+ return next(props);
68
+ }
69
+
70
+ };
71
+ }, {
17
72
  name: '@modern-js/plugin-bff'
18
73
  });
@@ -17,8 +17,6 @@ var _utils = require("@modern-js/utils");
17
17
 
18
18
  var _serverUtils = require("@modern-js/server-utils");
19
19
 
20
- var _constants = require("./constants");
21
-
22
20
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
21
 
24
22
  const DEFAULT_API_PREFIX = '/api';
@@ -51,7 +49,7 @@ var _default = (0, _core.createPlugin)(() => ({
51
49
  } = bff || {};
52
50
  const prefix = (bff === null || bff === void 0 ? void 0 : bff.prefix) || DEFAULT_API_PREFIX;
53
51
 
54
- const rootDir = _path.default.resolve(appDirectory, _constants.API_DIR);
52
+ const rootDir = _path.default.resolve(appDirectory, _utils.API_DIR);
55
53
 
56
54
  chain.resolve.alias.set('@api', rootDir);
57
55
  const apiRegexp = new RegExp((0, _utils.normalizeOutputPath)(`${appDirectory}${_path.default.sep}api${_path.default.sep}.*(.[tj]s)$`));
@@ -60,12 +58,13 @@ var _default = (0, _core.createPlugin)(() => ({
60
58
  apiDir: rootDir,
61
59
  port,
62
60
  fetcher,
63
- target: _config.name
61
+ target: _config.name,
62
+ requestCreator: bff === null || bff === void 0 ? void 0 : bff.requestCreator
64
63
  });
65
64
  }
66
65
  },
67
66
  source: {
68
- moduleScopes: [`./${_constants.API_DIR}`]
67
+ moduleScopes: [`./${_utils.API_DIR}`]
69
68
  }
70
69
  };
71
70
  },
@@ -109,11 +108,11 @@ var _default = (0, _core.createPlugin)(() => ({
109
108
 
110
109
  const modernConfig = (0, _core.useResolvedConfigContext)();
111
110
 
112
- const rootDir = _path.default.resolve(appDirectory, _constants.API_DIR);
111
+ const rootDir = _path.default.resolve(appDirectory, _utils.API_DIR);
113
112
 
114
- const distDir = _path.default.resolve(distDirectory, _constants.API_DIR);
113
+ const distDir = _path.default.resolve(distDirectory, _utils.API_DIR);
115
114
 
116
- const sourceAbsDir = _path.default.resolve(appDirectory, _constants.API_DIR);
115
+ const sourceAbsDir = _path.default.resolve(appDirectory, _utils.API_DIR);
117
116
 
118
117
  const tsconfigPath = _path.default.resolve(appDirectory, TS_CONFIG_FILENAME);
119
118
 
@@ -3,8 +3,8 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.BUILD_FILES = exports.API_DIR = void 0;
7
- const API_DIR = 'api';
8
- exports.API_DIR = API_DIR;
6
+ exports.BUILD_FILES = exports.API_APP_NAME = void 0;
7
+ const API_APP_NAME = '_app';
8
+ exports.API_APP_NAME = API_APP_NAME;
9
9
  const BUILD_FILES = ['**/*.[tj]sx?', '!**/*.test.jsx?', '!**/*.test.tsx?', '!**/*.spec.jsx?', '!**/*.spec.tsx?', '!__tests__/*.tsx?', '!__tests__/*.jsx?'];
10
10
  exports.BUILD_FILES = BUILD_FILES;
@@ -17,7 +17,7 @@ async function loader(source) {
17
17
 
18
18
  const draftOptions = (0, _loaderUtils.getOptions)(this);
19
19
  const options = {
20
- prefix: draftOptions.prefix,
20
+ prefix: Array.isArray(draftOptions.prefix) ? draftOptions.prefix[0] : draftOptions.prefix,
21
21
  apiDir: draftOptions.apiDir,
22
22
  target: draftOptions.target,
23
23
  port: Number(draftOptions.port),
@@ -11,24 +11,83 @@ var _serverPlugin = require("@modern-js/server-plugin");
11
11
 
12
12
  var _bffUtils = require("@modern-js/bff-utils");
13
13
 
14
+ var _core = require("@modern-js/core");
15
+
16
+ var _utils = require("@modern-js/utils");
17
+
14
18
  var _constants = require("./constants");
15
19
 
16
20
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
21
 
18
- var _default = (0, _serverPlugin.createPlugin)(() => ({
19
- prepareApiServer(props, next) {
20
- const {
21
- pwd,
22
- prefix
23
- } = props;
22
+ class Storage {
23
+ constructor() {
24
+ this.middlewares = [];
25
+ }
26
+
27
+ reset() {
28
+ this.middlewares = [];
29
+ }
24
30
 
25
- const apiDir = _path.default.resolve(pwd, _constants.API_DIR);
31
+ }
26
32
 
27
- (0, _bffUtils.injectAPIHandlerInfos)(apiDir, prefix);
28
- return next(props);
33
+ const createTransformAPI = storage => ({
34
+ addMiddleware(fn) {
35
+ storage.middlewares.push(fn);
29
36
  }
30
37
 
31
- }), {
38
+ });
39
+
40
+ var _default = (0, _serverPlugin.createPlugin)(() => {
41
+ const {
42
+ appDirectory,
43
+ distDirectory
44
+ } = (0, _core.useAppContext)();
45
+ const root = (0, _utils.isProd)() ? distDirectory : appDirectory;
46
+
47
+ const apiPath = _path.default.resolve(root || process.cwd(), _utils.API_DIR);
48
+
49
+ const apiAppPath = _path.default.resolve(apiPath, _constants.API_APP_NAME);
50
+
51
+ const storage = new Storage();
52
+ const transformAPI = createTransformAPI(storage);
53
+ const apiMod = (0, _utils.requireExistModule)(apiAppPath);
54
+
55
+ if (apiMod) {
56
+ apiMod(transformAPI);
57
+ }
58
+
59
+ return {
60
+ reset() {
61
+ storage.reset();
62
+ const newApiModule = (0, _utils.requireExistModule)(apiAppPath);
63
+
64
+ if (newApiModule) {
65
+ newApiModule(transformAPI);
66
+ }
67
+ },
68
+
69
+ gather({
70
+ addAPIMiddleware
71
+ }) {
72
+ storage.middlewares.forEach(mid => {
73
+ addAPIMiddleware(mid);
74
+ });
75
+ },
76
+
77
+ prepareApiServer(props, next) {
78
+ const {
79
+ pwd,
80
+ prefix
81
+ } = props;
82
+
83
+ const apiDir = _path.default.resolve(pwd, _utils.API_DIR);
84
+
85
+ (0, _bffUtils.injectAPIHandlerInfos)(apiDir, prefix);
86
+ return next(props);
87
+ }
88
+
89
+ };
90
+ }, {
32
91
  name: '@modern-js/plugin-bff'
33
92
  });
34
93
 
@@ -2,7 +2,7 @@ declare module '@modern-js/core' {
2
2
  interface UserConfig {
3
3
  bff: {
4
4
  prefix?: string;
5
- requestCreater?: string;
5
+ requestCreator?: string;
6
6
  fetcher?: string;
7
7
  proxy: Record<string, any>;
8
8
  };
@@ -1,2 +1,2 @@
1
- export declare const API_DIR = "api";
1
+ export declare const API_APP_NAME = "_app";
2
2
  export declare const BUILD_FILES: string[];
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.1.1",
14
+ "version": "1.1.2-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",
@@ -41,27 +41,35 @@
41
41
  ]
42
42
  }
43
43
  },
44
+ "scripts": {
45
+ "prepare": "pnpm build",
46
+ "prepublishOnly": "pnpm build -- --platform",
47
+ "new": "modern new",
48
+ "build": "modern build",
49
+ "dev": "modern build --watch",
50
+ "test": "modern test"
51
+ },
44
52
  "dependencies": {
45
53
  "@babel/core": "^7.15.5",
46
54
  "@babel/runtime": "^7",
47
- "@modern-js/babel-chain": "^1.1.1",
48
- "@modern-js/babel-compiler": "^1.1.2",
49
- "@modern-js/babel-preset-lib": "^1.1.1",
50
- "@modern-js/create-request": "^1.1.1",
51
- "@modern-js/server-utils": "^1.1.1",
52
- "@modern-js/utils": "^1.1.2",
55
+ "@modern-js/babel-chain": "workspace:^1.1.1",
56
+ "@modern-js/babel-compiler": "workspace:^1.1.2",
57
+ "@modern-js/babel-preset-lib": "workspace:^1.1.1",
58
+ "@modern-js/create-request": "workspace:^1.1.1",
59
+ "@modern-js/server-utils": "workspace:^1.1.1",
60
+ "@modern-js/utils": "workspace:^1.1.2",
53
61
  "fs-extra": "^10.0.0",
54
62
  "loader-utils": "^2.0.0"
55
63
  },
56
64
  "devDependencies": {
57
- "@modern-js/bff-utils": "^1.1.1",
58
- "@modern-js/core": "^1.1.2",
59
- "@modern-js/module-tools": "^1.1.0",
60
- "@modern-js/plugin-analyze": "^1.1.1",
61
- "@modern-js/plugin-testing": "^1.1.0",
62
- "@modern-js/runtime": "^1.1.1",
63
- "@modern-js/server-plugin": "^1.1.1",
64
- "@modern-js/types": "^1.1.2",
65
+ "@modern-js/bff-utils": "workspace:^1.1.1",
66
+ "@modern-js/core": "workspace:^1.1.2",
67
+ "@modern-js/module-tools": "^1.1.1",
68
+ "@modern-js/plugin-analyze": "workspace:^1.1.1",
69
+ "@modern-js/plugin-testing": "^1.1.1",
70
+ "@modern-js/runtime": "workspace:^1.1.1",
71
+ "@modern-js/server-plugin": "workspace:^1.1.1",
72
+ "@modern-js/types": "workspace:^1.1.2",
65
73
  "@types/babel__core": "^7.1.15",
66
74
  "@types/fs-extra": "^9.0.13",
67
75
  "@types/jest": "^26",
@@ -74,9 +82,9 @@
74
82
  "webpack-chain": "^6.5.1"
75
83
  },
76
84
  "peerDependencies": {
77
- "@modern-js/bff-utils": "^1.1.1",
78
- "@modern-js/core": "^1.1.2",
79
- "@modern-js/server-plugin": "^1.1.1"
85
+ "@modern-js/bff-utils": "workspace:^1.1.1",
86
+ "@modern-js/core": "workspace:^1.1.2",
87
+ "@modern-js/server-plugin": "workspace:^1.1.1"
80
88
  },
81
89
  "modernConfig": {
82
90
  "output": {
@@ -87,11 +95,5 @@
87
95
  "publishConfig": {
88
96
  "registry": "https://registry.npmjs.org/",
89
97
  "access": "public"
90
- },
91
- "scripts": {
92
- "new": "modern new",
93
- "build": "modern build",
94
- "dev": "modern build --watch",
95
- "test": "modern test"
96
98
  }
97
- }
99
+ }
package/src/cli.ts CHANGED
@@ -6,19 +6,18 @@ import {
6
6
  useResolvedConfigContext,
7
7
  } from '@modern-js/core';
8
8
  import { compiler } from '@modern-js/babel-compiler';
9
- import { PLUGIN_SCHEMAS, normalizeOutputPath } from '@modern-js/utils';
9
+ import { PLUGIN_SCHEMAS, normalizeOutputPath, API_DIR } from '@modern-js/utils';
10
10
  import { resolveBabelConfig } from '@modern-js/server-utils';
11
11
 
12
12
  import type { Configuration } from 'webpack';
13
13
  import type Chain from 'webpack-chain';
14
14
  import type { ServerRoute } from '@modern-js/types';
15
- import { API_DIR } from './constants';
16
15
 
17
16
  declare module '@modern-js/core' {
18
17
  interface UserConfig {
19
18
  bff: {
20
19
  prefix?: string;
21
- requestCreater?: string;
20
+ requestCreator?: string;
22
21
  fetcher?: string;
23
22
  proxy: Record<string, any>;
24
23
  };
@@ -69,6 +68,7 @@ export default createPlugin(
69
68
  port,
70
69
  fetcher,
71
70
  target: _config.name,
71
+ requestCreator: bff?.requestCreator,
72
72
  });
73
73
  },
74
74
  },
package/src/constants.ts CHANGED
@@ -1,4 +1,4 @@
1
- export const API_DIR = 'api';
1
+ export const API_APP_NAME = '_app';
2
2
 
3
3
  export const BUILD_FILES = [
4
4
  '**/*.[tj]sx?',
package/src/loader.ts CHANGED
@@ -20,7 +20,9 @@ async function loader(this: LoaderContext<APILoaderOptions>, source: string) {
20
20
  const draftOptions = getOptions(this as any);
21
21
 
22
22
  const options: GenClientOptions = {
23
- prefix: draftOptions.prefix as string,
23
+ prefix: (Array.isArray(draftOptions.prefix)
24
+ ? draftOptions.prefix[0]
25
+ : draftOptions.prefix) as string,
24
26
  apiDir: draftOptions.apiDir as string,
25
27
  target: draftOptions.target as string,
26
28
  port: Number(draftOptions.port),
package/src/server.ts CHANGED
@@ -1,18 +1,64 @@
1
1
  import path from 'path';
2
2
  import { createPlugin } from '@modern-js/server-plugin';
3
3
  import { injectAPIHandlerInfos } from '@modern-js/bff-utils';
4
- import { API_DIR } from './constants';
4
+ import { useAppContext } from '@modern-js/core';
5
+ import { API_DIR, isProd, requireExistModule } from '@modern-js/utils';
6
+ import { API_APP_NAME } from './constants';
7
+
8
+ type SF = (args: any) => void;
9
+ class Storage {
10
+ public middlewares: SF[] = [];
11
+
12
+ reset() {
13
+ this.middlewares = [];
14
+ }
15
+ }
16
+
17
+ const createTransformAPI = (storage: Storage) => ({
18
+ addMiddleware(fn: SF) {
19
+ storage.middlewares.push(fn);
20
+ },
21
+ });
5
22
 
6
23
  export default createPlugin(
7
- () => ({
8
- prepareApiServer(props, next) {
9
- const { pwd, prefix } = props;
10
- const apiDir = path.resolve(pwd, API_DIR);
24
+ () => {
25
+ const { appDirectory, distDirectory } = useAppContext();
26
+
27
+ const root = isProd() ? distDirectory : appDirectory;
28
+
29
+ const apiPath = path.resolve(root || process.cwd(), API_DIR);
30
+ const apiAppPath = path.resolve(apiPath, API_APP_NAME);
31
+
32
+ const storage = new Storage();
33
+ const transformAPI = createTransformAPI(storage);
34
+
35
+ const apiMod = requireExistModule(apiAppPath);
36
+ if (apiMod) {
37
+ apiMod(transformAPI);
38
+ }
39
+
40
+ return {
41
+ reset() {
42
+ storage.reset();
43
+ const newApiModule = requireExistModule(apiAppPath);
44
+ if (newApiModule) {
45
+ newApiModule(transformAPI);
46
+ }
47
+ },
48
+ gather({ addAPIMiddleware }) {
49
+ storage.middlewares.forEach(mid => {
50
+ addAPIMiddleware(mid);
51
+ });
52
+ },
53
+ prepareApiServer(props, next) {
54
+ const { pwd, prefix } = props;
55
+ const apiDir = path.resolve(pwd, API_DIR);
11
56
 
12
- injectAPIHandlerInfos(apiDir, prefix);
57
+ injectAPIHandlerInfos(apiDir, prefix);
13
58
 
14
- return next(props);
15
- },
16
- }),
59
+ return next(props);
60
+ },
61
+ };
62
+ },
17
63
  { name: '@modern-js/plugin-bff' },
18
64
  ) as any;
@@ -25,12 +25,18 @@ Array [
25
25
  "type": "string",
26
26
  },
27
27
  "prefix": Object {
28
- "type": "string",
28
+ "items": Object {
29
+ "type": "string",
30
+ },
31
+ "type": Array [
32
+ "string",
33
+ "array",
34
+ ],
29
35
  },
30
36
  "proxy": Object {
31
37
  "type": "object",
32
38
  },
33
- "requestCreater": Object {
39
+ "requestCreator": Object {
34
40
  "type": "string",
35
41
  },
36
42
  },
@@ -1 +0,0 @@
1
- export * from '/github/workspace/packages/cli/plugin-runtime'