Skip to content

Commit

Permalink
fix(utils): ESM mode needs .js extension so add it if not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Nov 28, 2022
1 parent 996a0e6 commit eba73c6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/good-hats-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-mesh/utils': patch
---

Better ESM support
25 changes: 17 additions & 8 deletions packages/utils/src/defaultImportFn.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
export async function defaultImportFn(path: string) {
let module = await import(/* @vite-ignore */ path).catch(e => {
if (e.code === 'ERR_REQUIRE_ESM') {
// eslint-disable-next-line no-new-func
return new Function(`return import(${JSON.stringify(path)})`)();
}
throw e;
});
import { path as pathModule } from '@graphql-mesh/cross-helpers';

export async function defaultImportFn(path: string): Promise<any> {
let module = await import(/* @vite-ignore */ path)
.catch(e => {
if (e.code === 'ERR_REQUIRE_ESM') {
// eslint-disable-next-line no-new-func
return new Function(`return import(${JSON.stringify(path)})`)();
}
throw e;
})
.catch(e => {
if (pathModule.isAbsolute(path) && !path.endsWith('.js') && !path.endsWith('.ts')) {
return defaultImportFn(`${path}.ts`);
}
throw e;
});
if (module.default != null) {
module = module.default;
}
Expand Down

0 comments on commit eba73c6

Please sign in to comment.