Skip to content

Commit

Permalink
fix: esbuild-register not working (web-infra-dev#1297)
Browse files Browse the repository at this point in the history
  • Loading branch information
yimingjfe committed Jun 30, 2022
1 parent d8541cd commit 53a8476
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/server/bff-core/src/router/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,17 @@ export const isHandler = (input: any): input is Handler<any, any> =>
input && typeof input === 'function';

const enableRegister = (requireFn: (modulePath: string) => HandlerModule) => {
// esbuild-register 做 unRegister 时,不会删除 register 添加的 require.extensions,导致第二次调用时 require.extensions['.ts'] 是 nodejs 默认 loader
// 所以这里根据第一次调用时,require.extensions 有没有,来判断是否需要使用 esbuild-register
let existTsLoader = false;
let firstCall = true;
return (modulePath: string) => {
// eslint-disable-next-line node/no-deprecated-api
if (!require.extensions['.ts']) {
if (firstCall) {
// eslint-disable-next-line node/no-deprecated-api
existTsLoader = Boolean(require.extensions['.ts']);
firstCall = false;
}
if (!existTsLoader) {
const { register } = require('esbuild-register/dist/node');
const { unregister } = register({});
const requiredModule = requireFn(modulePath);
Expand Down

0 comments on commit 53a8476

Please sign in to comment.