Skip to content

Commit

Permalink
fix: handle pure ESM robustly (#428)
Browse files Browse the repository at this point in the history
close #427
  • Loading branch information
JounQin authored Sep 14, 2022
1 parent bebc564 commit ff52be2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/proud-trainers-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-mdx": patch
---

fix: handle pure ESM robustly
8 changes: 7 additions & 1 deletion packages/eslint-mdx/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,14 @@ export const loadModule = async <T>(modulePath: string): Promise<T> => {
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-unsafe-return
return require(modulePath)
} catch (err) {
const code = (err as { code: string }).code
/* istanbul ignore if */
if ((err as { code: string }).code === 'ERR_REQUIRE_ESM') {
if (
code === 'ERR_REQUIRE_ESM' ||
// A pure ESM could have no `exports.require` and then throw the following error,
// related to #427.
code === 'ERR_PACKAGE_PATH_NOT_EXPORTED'
) {
// Load the ESM configuration file using the TypeScript dynamic import workaround.
// Once TypeScript provides support for keeping the dynamic import this workaround can be
// changed to a direct dynamic import.
Expand Down

0 comments on commit ff52be2

Please sign in to comment.