@modern-js/plugin-bff 1.3.1 → 1.3.4
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 +45 -0
- package/dist/js/modern/cli.js +1 -0
- package/dist/js/modern/index.js +1 -0
- package/dist/js/modern/server.js +1 -2
- package/dist/js/modern/types.js +1 -0
- package/dist/js/node/cli.js +2 -0
- package/dist/js/node/index.js +2 -0
- package/dist/js/node/server.js +3 -5
- package/dist/js/node/types.js +3 -0
- package/dist/types/cli.d.ts +1 -10
- package/dist/types/index.d.ts +1 -0
- package/dist/types/types.d.ts +11 -0
- package/package.json +17 -18
- package/tests/server.test.ts +1 -1
- package/src/cli.ts +0 -146
- package/src/constants.ts +0 -11
- package/src/index.ts +0 -1
- package/src/loader.ts +0 -53
- package/src/server.ts +0 -64
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,50 @@
|
|
|
1
1
|
# @modern-js/plugin-bff
|
|
2
2
|
|
|
3
|
+
## 1.3.4
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 8f24a514: fix: allow reference typings to index file
|
|
8
|
+
- Updated dependencies [deeaa602]
|
|
9
|
+
- Updated dependencies [54786e58]
|
|
10
|
+
- @modern-js/utils@1.3.2
|
|
11
|
+
- @modern-js/core@1.4.3
|
|
12
|
+
|
|
13
|
+
## 1.3.3
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- 272cab15: refactor server plugin manager
|
|
18
|
+
- Updated dependencies [d9cc5ea9]
|
|
19
|
+
- Updated dependencies [bd819a8d]
|
|
20
|
+
- Updated dependencies [ec4dbffb]
|
|
21
|
+
- Updated dependencies [d099e5c5]
|
|
22
|
+
- Updated dependencies [bada2879]
|
|
23
|
+
- Updated dependencies [24f616ca]
|
|
24
|
+
- Updated dependencies [bd819a8d]
|
|
25
|
+
- Updated dependencies [272cab15]
|
|
26
|
+
- @modern-js/core@1.4.0
|
|
27
|
+
- @modern-js/utils@1.3.0
|
|
28
|
+
- @modern-js/server-core@1.2.2
|
|
29
|
+
|
|
30
|
+
## 1.3.2
|
|
31
|
+
|
|
32
|
+
### Patch Changes
|
|
33
|
+
|
|
34
|
+
- 83166714: change .npmignore
|
|
35
|
+
- Updated dependencies [83166714]
|
|
36
|
+
- Updated dependencies [c3de9882]
|
|
37
|
+
- Updated dependencies [33ff48af]
|
|
38
|
+
- @modern-js/babel-preset-lib@1.2.1
|
|
39
|
+
- @modern-js/core@1.3.2
|
|
40
|
+
- @modern-js/bff-utils@1.2.2
|
|
41
|
+
- @modern-js/create-request@1.2.1
|
|
42
|
+
- @modern-js/server-plugin@1.2.1
|
|
43
|
+
- @modern-js/server-utils@1.2.1
|
|
44
|
+
- @modern-js/babel-chain@1.2.1
|
|
45
|
+
- @modern-js/babel-compiler@1.2.1
|
|
46
|
+
- @modern-js/utils@1.2.2
|
|
47
|
+
|
|
3
48
|
## 1.3.1
|
|
4
49
|
|
|
5
50
|
### Patch Changes
|
package/dist/js/modern/cli.js
CHANGED
package/dist/js/modern/index.js
CHANGED
package/dist/js/modern/server.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
|
-
import { createPlugin } from '@modern-js/server-
|
|
2
|
+
import { createPlugin, useAppContext } from '@modern-js/server-core';
|
|
3
3
|
import { injectAPIHandlerInfos } from '@modern-js/bff-utils';
|
|
4
|
-
import { useAppContext } from '@modern-js/core';
|
|
5
4
|
import { API_DIR, isProd, requireExistModule } from '@modern-js/utils';
|
|
6
5
|
import { API_APP_NAME } from "./constants";
|
|
7
6
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import '@modern-js/core';
|
package/dist/js/node/cli.js
CHANGED
package/dist/js/node/index.js
CHANGED
package/dist/js/node/server.js
CHANGED
|
@@ -7,12 +7,10 @@ exports.default = void 0;
|
|
|
7
7
|
|
|
8
8
|
var _path = _interopRequireDefault(require("path"));
|
|
9
9
|
|
|
10
|
-
var
|
|
10
|
+
var _serverCore = require("@modern-js/server-core");
|
|
11
11
|
|
|
12
12
|
var _bffUtils = require("@modern-js/bff-utils");
|
|
13
13
|
|
|
14
|
-
var _core = require("@modern-js/core");
|
|
15
|
-
|
|
16
14
|
var _utils = require("@modern-js/utils");
|
|
17
15
|
|
|
18
16
|
var _constants = require("./constants");
|
|
@@ -37,11 +35,11 @@ const createTransformAPI = storage => ({
|
|
|
37
35
|
|
|
38
36
|
});
|
|
39
37
|
|
|
40
|
-
var _default = (0,
|
|
38
|
+
var _default = (0, _serverCore.createPlugin)(() => {
|
|
41
39
|
const {
|
|
42
40
|
appDirectory,
|
|
43
41
|
distDirectory
|
|
44
|
-
} = (0,
|
|
42
|
+
} = (0, _serverCore.useAppContext)();
|
|
45
43
|
const root = (0, _utils.isProd)() ? distDirectory : appDirectory;
|
|
46
44
|
|
|
47
45
|
const apiPath = _path.default.resolve(root || process.cwd(), _utils.API_DIR);
|
package/dist/types/cli.d.ts
CHANGED
package/dist/types/index.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.3.
|
|
14
|
+
"version": "1.3.4",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/index.d.ts",
|
|
17
17
|
"main": "./dist/js/node/index.js",
|
|
@@ -51,23 +51,23 @@
|
|
|
51
51
|
"dependencies": {
|
|
52
52
|
"@babel/core": "7.16.7",
|
|
53
53
|
"@babel/runtime": "^7",
|
|
54
|
-
"@modern-js/babel-chain": "^1.2.
|
|
55
|
-
"@modern-js/babel-compiler": "^1.2.
|
|
56
|
-
"@modern-js/babel-preset-lib": "^1.2.
|
|
57
|
-
"@modern-js/create-request": "^1.2.
|
|
58
|
-
"@modern-js/server-utils": "^1.2.
|
|
59
|
-
"@modern-js/utils": "^1.2
|
|
54
|
+
"@modern-js/babel-chain": "^1.2.1",
|
|
55
|
+
"@modern-js/babel-compiler": "^1.2.1",
|
|
56
|
+
"@modern-js/babel-preset-lib": "^1.2.1",
|
|
57
|
+
"@modern-js/create-request": "^1.2.1",
|
|
58
|
+
"@modern-js/server-utils": "^1.2.1",
|
|
59
|
+
"@modern-js/utils": "^1.3.2",
|
|
60
60
|
"fs-extra": "^10.0.0",
|
|
61
61
|
"loader-utils": "^2.0.0"
|
|
62
62
|
},
|
|
63
63
|
"devDependencies": {
|
|
64
|
-
"@modern-js/bff-utils": "^1.2.
|
|
65
|
-
"@modern-js/core": "^1.3
|
|
64
|
+
"@modern-js/bff-utils": "^1.2.2",
|
|
65
|
+
"@modern-js/core": "^1.4.3",
|
|
66
66
|
"@scripts/build": "0.0.0",
|
|
67
|
-
"@modern-js/plugin-analyze": "^1.
|
|
68
|
-
"@modern-js/runtime": "^1.2.
|
|
69
|
-
"@modern-js/server-
|
|
70
|
-
"@modern-js/types": "^1.
|
|
67
|
+
"@modern-js/plugin-analyze": "^1.3.3",
|
|
68
|
+
"@modern-js/runtime": "^1.2.2",
|
|
69
|
+
"@modern-js/server-core": "^1.2.2",
|
|
70
|
+
"@modern-js/types": "^1.3.3",
|
|
71
71
|
"@types/babel__core": "^7.1.15",
|
|
72
72
|
"@types/fs-extra": "^9.0.13",
|
|
73
73
|
"@types/jest": "^26",
|
|
@@ -82,9 +82,9 @@
|
|
|
82
82
|
"@scripts/jest-config": "0.0.0"
|
|
83
83
|
},
|
|
84
84
|
"peerDependencies": {
|
|
85
|
-
"@modern-js/bff-utils": "^1.2.
|
|
86
|
-
"@modern-js/core": "^1.3
|
|
87
|
-
"@modern-js/server-
|
|
85
|
+
"@modern-js/bff-utils": "^1.2.2",
|
|
86
|
+
"@modern-js/core": "^1.4.3",
|
|
87
|
+
"@modern-js/server-core": "^1.2.2"
|
|
88
88
|
},
|
|
89
89
|
"modernConfig": {
|
|
90
90
|
"output": {
|
|
@@ -94,8 +94,7 @@
|
|
|
94
94
|
"sideEffects": false,
|
|
95
95
|
"publishConfig": {
|
|
96
96
|
"registry": "https://registry.npmjs.org/",
|
|
97
|
-
"access": "public"
|
|
98
|
-
"types": "./dist/types/index.d.ts"
|
|
97
|
+
"access": "public"
|
|
99
98
|
},
|
|
100
99
|
"scripts": {
|
|
101
100
|
"new": "modern new",
|
package/tests/server.test.ts
CHANGED
package/src/cli.ts
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import fs from 'fs-extra';
|
|
3
|
-
import {
|
|
4
|
-
createPlugin,
|
|
5
|
-
useAppContext,
|
|
6
|
-
useResolvedConfigContext,
|
|
7
|
-
} from '@modern-js/core';
|
|
8
|
-
import { compiler } from '@modern-js/babel-compiler';
|
|
9
|
-
import { PLUGIN_SCHEMAS, normalizeOutputPath, API_DIR } from '@modern-js/utils';
|
|
10
|
-
import { resolveBabelConfig } from '@modern-js/server-utils';
|
|
11
|
-
|
|
12
|
-
import type { Configuration } from 'webpack';
|
|
13
|
-
import type Chain from 'webpack-chain';
|
|
14
|
-
import type { ServerRoute } from '@modern-js/types';
|
|
15
|
-
|
|
16
|
-
declare module '@modern-js/core' {
|
|
17
|
-
interface UserConfig {
|
|
18
|
-
bff?: {
|
|
19
|
-
prefix?: string;
|
|
20
|
-
requestCreator?: string;
|
|
21
|
-
fetcher?: string;
|
|
22
|
-
proxy: Record<string, any>;
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const DEFAULT_API_PREFIX = '/api';
|
|
28
|
-
const TS_CONFIG_FILENAME = 'tsconfig.json';
|
|
29
|
-
const FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs'];
|
|
30
|
-
|
|
31
|
-
export default createPlugin(
|
|
32
|
-
() => ({
|
|
33
|
-
validateSchema() {
|
|
34
|
-
return PLUGIN_SCHEMAS['@modern-js/plugin-bff'];
|
|
35
|
-
},
|
|
36
|
-
config() {
|
|
37
|
-
return {
|
|
38
|
-
tools: {
|
|
39
|
-
webpack: (_config: Configuration, { chain }: { chain: Chain }) => {
|
|
40
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
41
|
-
const { appDirectory, port } = useAppContext();
|
|
42
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
43
|
-
const modernConfig = useResolvedConfigContext();
|
|
44
|
-
|
|
45
|
-
const { bff } = modernConfig || {};
|
|
46
|
-
const { fetcher } = bff || {};
|
|
47
|
-
const prefix = bff?.prefix || DEFAULT_API_PREFIX;
|
|
48
|
-
|
|
49
|
-
const rootDir = path.resolve(appDirectory, API_DIR);
|
|
50
|
-
|
|
51
|
-
chain.resolve.alias.set('@api', rootDir);
|
|
52
|
-
|
|
53
|
-
const apiRegexp = new RegExp(
|
|
54
|
-
normalizeOutputPath(
|
|
55
|
-
`${appDirectory}${path.sep}api${path.sep}.*(.[tj]s)$`,
|
|
56
|
-
),
|
|
57
|
-
);
|
|
58
|
-
chain.module
|
|
59
|
-
.rule('loaders')
|
|
60
|
-
.oneOf('bff-client')
|
|
61
|
-
.before('fallback')
|
|
62
|
-
.test(apiRegexp)
|
|
63
|
-
.use('custom-loader')
|
|
64
|
-
.loader(require.resolve('./loader').replace(/\\/g, '/'))
|
|
65
|
-
.options({
|
|
66
|
-
prefix,
|
|
67
|
-
apiDir: rootDir,
|
|
68
|
-
port,
|
|
69
|
-
fetcher,
|
|
70
|
-
target: _config.name,
|
|
71
|
-
requestCreator: bff?.requestCreator,
|
|
72
|
-
});
|
|
73
|
-
},
|
|
74
|
-
},
|
|
75
|
-
source: {
|
|
76
|
-
moduleScopes: [`./${API_DIR}`, /create-request/],
|
|
77
|
-
},
|
|
78
|
-
};
|
|
79
|
-
},
|
|
80
|
-
modifyServerRoutes({ routes }: any) {
|
|
81
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
82
|
-
const modernConfig = useResolvedConfigContext();
|
|
83
|
-
|
|
84
|
-
const { bff } = modernConfig || {};
|
|
85
|
-
const prefix = bff?.prefix || '/api';
|
|
86
|
-
|
|
87
|
-
const prefixList: string[] = [];
|
|
88
|
-
|
|
89
|
-
if (Array.isArray(prefix)) {
|
|
90
|
-
prefixList.push(...prefix);
|
|
91
|
-
} else {
|
|
92
|
-
prefixList.push(prefix);
|
|
93
|
-
}
|
|
94
|
-
const apiServerRoutes = prefixList.map(pre => ({
|
|
95
|
-
urlPath: pre,
|
|
96
|
-
isApi: true,
|
|
97
|
-
entryPath: '',
|
|
98
|
-
isSPA: false,
|
|
99
|
-
isSSR: false,
|
|
100
|
-
// FIXME: })) as IAppContext[`serverRoutes`];
|
|
101
|
-
})) as ServerRoute[];
|
|
102
|
-
|
|
103
|
-
return { routes: routes.concat(apiServerRoutes) };
|
|
104
|
-
},
|
|
105
|
-
async afterBuild() {
|
|
106
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
107
|
-
const { appDirectory, distDirectory } = useAppContext();
|
|
108
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
109
|
-
const modernConfig = useResolvedConfigContext();
|
|
110
|
-
|
|
111
|
-
const rootDir = path.resolve(appDirectory, API_DIR);
|
|
112
|
-
const distDir = path.resolve(distDirectory, API_DIR);
|
|
113
|
-
|
|
114
|
-
const sourceAbsDir = path.resolve(appDirectory, API_DIR);
|
|
115
|
-
const tsconfigPath = path.resolve(appDirectory, TS_CONFIG_FILENAME);
|
|
116
|
-
const babelConfig = resolveBabelConfig(appDirectory, modernConfig, {
|
|
117
|
-
tsconfigPath,
|
|
118
|
-
syntax: 'es6+',
|
|
119
|
-
type: 'commonjs',
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
const result = await compiler(
|
|
123
|
-
{
|
|
124
|
-
rootDir,
|
|
125
|
-
distDir,
|
|
126
|
-
sourceDir: sourceAbsDir,
|
|
127
|
-
extensions: FILE_EXTENSIONS,
|
|
128
|
-
ignore: [`**/__tests__/**`, '**/typings/**', '*.d.ts'],
|
|
129
|
-
},
|
|
130
|
-
babelConfig,
|
|
131
|
-
);
|
|
132
|
-
|
|
133
|
-
if (await fs.pathExists(rootDir)) {
|
|
134
|
-
await fs.copy(rootDir, distDir, {
|
|
135
|
-
filter: src =>
|
|
136
|
-
!['.ts', '.js'].includes(path.extname(src)) && src !== tsconfigPath,
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
if (result.code === 1) {
|
|
141
|
-
throw new Error(result.message);
|
|
142
|
-
}
|
|
143
|
-
},
|
|
144
|
-
}),
|
|
145
|
-
{ name: '@modern-js/plugin-bff' },
|
|
146
|
-
) as any;
|
package/src/constants.ts
DELETED
package/src/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './constants';
|
package/src/loader.ts
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { getOptions } from 'loader-utils';
|
|
2
|
-
import { generateClient, GenClientOptions } from '@modern-js/bff-utils';
|
|
3
|
-
import { LoaderContext } from 'webpack';
|
|
4
|
-
|
|
5
|
-
export type APILoaderOptions = {
|
|
6
|
-
prefix: string;
|
|
7
|
-
apiDir: string;
|
|
8
|
-
port: number;
|
|
9
|
-
fetcher?: string;
|
|
10
|
-
requestCreator?: string;
|
|
11
|
-
target: string;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
async function loader(this: LoaderContext<APILoaderOptions>, source: string) {
|
|
15
|
-
// eslint-disable-next-line @babel/no-invalid-this
|
|
16
|
-
this.cacheable();
|
|
17
|
-
// eslint-disable-next-line @babel/no-invalid-this
|
|
18
|
-
const callback = this.async();
|
|
19
|
-
// eslint-disable-next-line @babel/no-invalid-this
|
|
20
|
-
const draftOptions = getOptions(this as any);
|
|
21
|
-
|
|
22
|
-
const options: GenClientOptions = {
|
|
23
|
-
prefix: (Array.isArray(draftOptions.prefix)
|
|
24
|
-
? draftOptions.prefix[0]
|
|
25
|
-
: draftOptions.prefix) as string,
|
|
26
|
-
apiDir: draftOptions.apiDir as string,
|
|
27
|
-
target: draftOptions.target as string,
|
|
28
|
-
port: Number(draftOptions.port),
|
|
29
|
-
source,
|
|
30
|
-
// eslint-disable-next-line @babel/no-invalid-this
|
|
31
|
-
resourcePath: this.resourcePath,
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
if (draftOptions.fetcher) {
|
|
35
|
-
options.fetcher = draftOptions.fetcher as string;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if (draftOptions.requestCreator) {
|
|
39
|
-
options.requestCreator = draftOptions.requestCreator as string;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
options.requireResolve = require.resolve;
|
|
43
|
-
|
|
44
|
-
const result = await generateClient(options);
|
|
45
|
-
|
|
46
|
-
if (result.isOk) {
|
|
47
|
-
callback(undefined, result.value);
|
|
48
|
-
} else {
|
|
49
|
-
callback(undefined, `throw new Error('${result.value}')`);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export default loader;
|
package/src/server.ts
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import { createPlugin } from '@modern-js/server-plugin';
|
|
3
|
-
import { injectAPIHandlerInfos } from '@modern-js/bff-utils';
|
|
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
|
-
});
|
|
22
|
-
|
|
23
|
-
export default createPlugin(
|
|
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 && typeof apiMod === 'function') {
|
|
37
|
-
apiMod(transformAPI);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return {
|
|
41
|
-
reset() {
|
|
42
|
-
storage.reset();
|
|
43
|
-
const newApiModule = requireExistModule(apiAppPath);
|
|
44
|
-
if (newApiModule && typeof newApiModule === 'function') {
|
|
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);
|
|
56
|
-
|
|
57
|
-
injectAPIHandlerInfos(apiDir, prefix);
|
|
58
|
-
|
|
59
|
-
return next(props);
|
|
60
|
-
},
|
|
61
|
-
};
|
|
62
|
-
},
|
|
63
|
-
{ name: '@modern-js/plugin-bff' },
|
|
64
|
-
) as any;
|