From ff52be232611275307681a53c69ed40c3a419da1 Mon Sep 17 00:00:00 2001 From: JounQin Date: Thu, 15 Sep 2022 06:58:10 +0800 Subject: [PATCH] fix: handle pure ESM robustly (#428) close #427 --- .changeset/proud-trainers-sort.md | 5 +++++ packages/eslint-mdx/src/helpers.ts | 8 +++++++- 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 .changeset/proud-trainers-sort.md diff --git a/.changeset/proud-trainers-sort.md b/.changeset/proud-trainers-sort.md new file mode 100644 index 00000000..bb7eb01a --- /dev/null +++ b/.changeset/proud-trainers-sort.md @@ -0,0 +1,5 @@ +--- +"eslint-mdx": patch +--- + +fix: handle pure ESM robustly diff --git a/packages/eslint-mdx/src/helpers.ts b/packages/eslint-mdx/src/helpers.ts index e3f6387d..60f39c41 100644 --- a/packages/eslint-mdx/src/helpers.ts +++ b/packages/eslint-mdx/src/helpers.ts @@ -90,8 +90,14 @@ export const loadModule = async (modulePath: string): Promise => { // 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.