@modern-js/bff-core 1.0.1-beta.7 → 1.0.1-beta.8

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.
@@ -39,9 +39,18 @@ const clearRouteName = routeName => {
39
39
  export const isHandler = input => input && typeof input === 'function';
40
40
 
41
41
  const enableRegister = requireFn => {
42
+ // esbuild-register 做 unRegister 时,不会删除 register 添加的 require.extensions,导致第二次调用时 require.extensions['.ts'] 是 nodejs 默认 loader
43
+ // 所以这里根据第一次调用时,require.extensions 有没有,来判断是否需要使用 esbuild-register
44
+ let existTsLoader = false;
45
+ let firstCall = true;
42
46
  return modulePath => {
43
- // eslint-disable-next-line node/no-deprecated-api
44
- if (!require.extensions['.ts']) {
47
+ if (firstCall) {
48
+ // eslint-disable-next-line node/no-deprecated-api
49
+ existTsLoader = Boolean(require.extensions['.ts']);
50
+ firstCall = false;
51
+ }
52
+
53
+ if (!existTsLoader) {
45
54
  const {
46
55
  register
47
56
  } = require('esbuild-register/dist/node');
@@ -1,4 +1,7 @@
1
1
  import util from 'util';
2
+ import fs from 'fs';
3
+ import Module from 'module';
4
+ import path from 'path';
2
5
  import { createDebugger } from '@modern-js/utils';
3
6
  export const HANDLER_WITH_META = 'HANDLER_WITH_META';
4
7
  export const debug = createDebugger('bff'); // export const pick = <T extends Record<string, unknown>, K extends keyof T>(
@@ -60,4 +63,74 @@ export const validateFunction = (maybeFunc, name) => {
60
63
  };
61
64
  export const isWithMetaHandler = handler => {
62
65
  return typeof handler === 'function' && handler[HANDLER_WITH_META];
66
+ };
67
+
68
+ const sortByLongestPrefix = arr => {
69
+ return arr.concat().sort((a, b) => b.length - a.length);
70
+ };
71
+
72
+ export const createMatchPath = paths => {
73
+ const sortedKeys = sortByLongestPrefix(Object.keys(paths));
74
+ const sortedPaths = {};
75
+ sortedKeys.forEach(key => {
76
+ sortedPaths[key] = paths[key];
77
+ });
78
+ return request => {
79
+ const found = Object.keys(sortedPaths).find(key => {
80
+ return request.startsWith(key);
81
+ });
82
+
83
+ if (found) {
84
+ let foundPaths = sortedPaths[found];
85
+
86
+ if (!Array.isArray(foundPaths)) {
87
+ foundPaths = [foundPaths];
88
+ }
89
+
90
+ foundPaths = foundPaths.filter(foundPath => path.isAbsolute(foundPath));
91
+
92
+ for (const p of foundPaths) {
93
+ const foundPath = request.replace(found, p);
94
+
95
+ if (fs.existsSync(foundPath)) {
96
+ return foundPath;
97
+ }
98
+ }
99
+
100
+ return request.replace(found, foundPaths[0]);
101
+ }
102
+
103
+ return null;
104
+ };
105
+ }; // paths 中的值只能是绝对路径
106
+
107
+ export const registerPaths = paths => {
108
+ const originalResolveFilename = Module._resolveFilename; // eslint-disable-next-line node/no-unsupported-features/node-builtins
109
+
110
+ const {
111
+ builtinModules
112
+ } = Module;
113
+ const matchPath = createMatchPath(paths);
114
+
115
+ Module._resolveFilename = function (request, _parent) {
116
+ const isCoreModule = builtinModules.includes(request);
117
+
118
+ if (!isCoreModule) {
119
+ const matched = matchPath(request);
120
+
121
+ if (matched) {
122
+ // eslint-disable-next-line prefer-rest-params
123
+ const modifiedArguments = [matched, ...[].slice.call(arguments, 1)]; // Passes all arguments. Even those that is not specified above.
124
+
125
+ return originalResolveFilename.apply(this, modifiedArguments);
126
+ }
127
+ } // eslint-disable-next-line prefer-rest-params
128
+
129
+
130
+ return originalResolveFilename.apply(this, arguments);
131
+ };
132
+
133
+ return () => {
134
+ Module._resolveFilename = originalResolveFilename;
135
+ };
63
136
  };
@@ -58,9 +58,18 @@ const isHandler = input => input && typeof input === 'function';
58
58
  exports.isHandler = isHandler;
59
59
 
60
60
  const enableRegister = requireFn => {
61
+ // esbuild-register 做 unRegister 时,不会删除 register 添加的 require.extensions,导致第二次调用时 require.extensions['.ts'] 是 nodejs 默认 loader
62
+ // 所以这里根据第一次调用时,require.extensions 有没有,来判断是否需要使用 esbuild-register
63
+ let existTsLoader = false;
64
+ let firstCall = true;
61
65
  return modulePath => {
62
- // eslint-disable-next-line node/no-deprecated-api
63
- if (!require.extensions['.ts']) {
66
+ if (firstCall) {
67
+ // eslint-disable-next-line node/no-deprecated-api
68
+ existTsLoader = Boolean(require.extensions['.ts']);
69
+ firstCall = false;
70
+ }
71
+
72
+ if (!existTsLoader) {
64
73
  const {
65
74
  register
66
75
  } = require('esbuild-register/dist/node');
@@ -3,10 +3,16 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.validateFunction = exports.isWithMetaHandler = exports.getTypeErrorMessage = exports.debug = exports.HANDLER_WITH_META = exports.ERR_INVALID_ARG_TYPE = void 0;
6
+ exports.validateFunction = exports.registerPaths = exports.isWithMetaHandler = exports.getTypeErrorMessage = exports.debug = exports.createMatchPath = exports.HANDLER_WITH_META = exports.ERR_INVALID_ARG_TYPE = void 0;
7
7
 
8
8
  var _util = _interopRequireDefault(require("util"));
9
9
 
10
+ var _fs = _interopRequireDefault(require("fs"));
11
+
12
+ var _module = _interopRequireDefault(require("module"));
13
+
14
+ var _path = _interopRequireDefault(require("path"));
15
+
10
16
  var _utils = require("@modern-js/utils");
11
17
 
12
18
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -86,4 +92,79 @@ const isWithMetaHandler = handler => {
86
92
  return typeof handler === 'function' && handler[HANDLER_WITH_META];
87
93
  };
88
94
 
89
- exports.isWithMetaHandler = isWithMetaHandler;
95
+ exports.isWithMetaHandler = isWithMetaHandler;
96
+
97
+ const sortByLongestPrefix = arr => {
98
+ return arr.concat().sort((a, b) => b.length - a.length);
99
+ };
100
+
101
+ const createMatchPath = paths => {
102
+ const sortedKeys = sortByLongestPrefix(Object.keys(paths));
103
+ const sortedPaths = {};
104
+ sortedKeys.forEach(key => {
105
+ sortedPaths[key] = paths[key];
106
+ });
107
+ return request => {
108
+ const found = Object.keys(sortedPaths).find(key => {
109
+ return request.startsWith(key);
110
+ });
111
+
112
+ if (found) {
113
+ let foundPaths = sortedPaths[found];
114
+
115
+ if (!Array.isArray(foundPaths)) {
116
+ foundPaths = [foundPaths];
117
+ }
118
+
119
+ foundPaths = foundPaths.filter(foundPath => _path.default.isAbsolute(foundPath));
120
+
121
+ for (const p of foundPaths) {
122
+ const foundPath = request.replace(found, p);
123
+
124
+ if (_fs.default.existsSync(foundPath)) {
125
+ return foundPath;
126
+ }
127
+ }
128
+
129
+ return request.replace(found, foundPaths[0]);
130
+ }
131
+
132
+ return null;
133
+ };
134
+ }; // paths 中的值只能是绝对路径
135
+
136
+
137
+ exports.createMatchPath = createMatchPath;
138
+
139
+ const registerPaths = paths => {
140
+ const originalResolveFilename = _module.default._resolveFilename; // eslint-disable-next-line node/no-unsupported-features/node-builtins
141
+
142
+ const {
143
+ builtinModules
144
+ } = _module.default;
145
+ const matchPath = createMatchPath(paths);
146
+
147
+ _module.default._resolveFilename = function (request, _parent) {
148
+ const isCoreModule = builtinModules.includes(request);
149
+
150
+ if (!isCoreModule) {
151
+ const matched = matchPath(request);
152
+
153
+ if (matched) {
154
+ // eslint-disable-next-line prefer-rest-params
155
+ const modifiedArguments = [matched, ...[].slice.call(arguments, 1)]; // Passes all arguments. Even those that is not specified above.
156
+
157
+ return originalResolveFilename.apply(this, modifiedArguments);
158
+ }
159
+ } // eslint-disable-next-line prefer-rest-params
160
+
161
+
162
+ return originalResolveFilename.apply(this, arguments);
163
+ };
164
+
165
+ return () => {
166
+ _module.default._resolveFilename = originalResolveFilename;
167
+ };
168
+ };
169
+
170
+ exports.registerPaths = registerPaths;
@@ -5,4 +5,10 @@ export declare class ERR_INVALID_ARG_TYPE extends Error {
5
5
  constructor(funcName: string, expectedType: string, actual: unknown);
6
6
  }
7
7
  export declare const validateFunction: (maybeFunc: unknown, name: string) => boolean;
8
- export declare const isWithMetaHandler: (handler: any) => any;
8
+ export declare const isWithMetaHandler: (handler: any) => any;
9
+ interface Paths {
10
+ [key: string]: string[] | string;
11
+ }
12
+ export declare const createMatchPath: (paths: Paths) => (request: string) => string | null;
13
+ export declare const registerPaths: (paths: Paths) => () => void;
14
+ export {};
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.0.1-beta.7",
14
+ "version": "1.0.1-beta.8",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",