@modern-js/plugin-bff 1.3.1 → 1.3.2

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,23 @@
1
1
  # @modern-js/plugin-bff
2
2
 
3
+ ## 1.3.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 83166714: change .npmignore
8
+ - Updated dependencies [83166714]
9
+ - Updated dependencies [c3de9882]
10
+ - Updated dependencies [33ff48af]
11
+ - @modern-js/babel-preset-lib@1.2.1
12
+ - @modern-js/core@1.3.2
13
+ - @modern-js/bff-utils@1.2.2
14
+ - @modern-js/create-request@1.2.1
15
+ - @modern-js/server-plugin@1.2.1
16
+ - @modern-js/server-utils@1.2.1
17
+ - @modern-js/babel-chain@1.2.1
18
+ - @modern-js/babel-compiler@1.2.1
19
+ - @modern-js/utils@1.2.2
20
+
3
21
  ## 1.3.1
4
22
 
5
23
  ### Patch Changes
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.3.1",
14
+ "version": "1.3.2",
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.0",
55
- "@modern-js/babel-compiler": "^1.2.0",
56
- "@modern-js/babel-preset-lib": "^1.2.0",
57
- "@modern-js/create-request": "^1.2.0",
58
- "@modern-js/server-utils": "^1.2.0",
59
- "@modern-js/utils": "^1.2.1",
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.2.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.1",
65
- "@modern-js/core": "^1.3.1",
64
+ "@modern-js/bff-utils": "^1.2.2",
65
+ "@modern-js/core": "^1.3.2",
66
66
  "@scripts/build": "0.0.0",
67
- "@modern-js/plugin-analyze": "^1.2.0",
68
- "@modern-js/runtime": "^1.2.0",
69
- "@modern-js/server-plugin": "^1.2.0",
70
- "@modern-js/types": "^1.2.0",
67
+ "@modern-js/plugin-analyze": "^1.2.1",
68
+ "@modern-js/runtime": "^1.2.1",
69
+ "@modern-js/server-plugin": "^1.2.1",
70
+ "@modern-js/types": "^1.2.1",
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.1",
86
- "@modern-js/core": "^1.3.1",
87
- "@modern-js/server-plugin": "^1.2.0"
85
+ "@modern-js/bff-utils": "^1.2.2",
86
+ "@modern-js/core": "^1.3.2",
87
+ "@modern-js/server-plugin": "^1.2.1"
88
88
  },
89
89
  "modernConfig": {
90
90
  "output": {
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
@@ -1,11 +0,0 @@
1
- export const API_APP_NAME = '_app';
2
-
3
- export const BUILD_FILES = [
4
- '**/*.[tj]sx?',
5
- '!**/*.test.jsx?',
6
- '!**/*.test.tsx?',
7
- '!**/*.spec.jsx?',
8
- '!**/*.spec.tsx?',
9
- '!__tests__/*.tsx?',
10
- '!__tests__/*.jsx?',
11
- ];
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;