From 139cb9eb168aae17ceebf9fc18823d5880a7d181 Mon Sep 17 00:00:00 2001 From: JounQin Date: Sun, 24 Dec 2023 16:51:18 +0800 Subject: [PATCH] fix: workaround for `--experimental-vm-modules` not enabled --- .../src/configs/recommended.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/eslint-plugin-mdx/src/configs/recommended.ts b/packages/eslint-plugin-mdx/src/configs/recommended.ts index a667843..9242fcc 100644 --- a/packages/eslint-plugin-mdx/src/configs/recommended.ts +++ b/packages/eslint-plugin-mdx/src/configs/recommended.ts @@ -1,4 +1,5 @@ import { createRequire } from 'node:module' +import path from 'node:path' import type { ESLint, Linter } from 'eslint' @@ -22,9 +23,19 @@ export const recommended: Linter.Config = { /* istanbul ignore next */ // hack to bypass syntax error for commonjs -const getImportMetaUrl = () => - // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func - new Function('return import.meta.url')() as string +const getImportMetaUrl = () => { + try { + // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func + return new Function('return import.meta.url')() as string + } catch { + // if `--experimental-vm-modules` is not enabled, the following error would be thrown: + // `SyntaxError: Cannot use 'import.meta' outside a module`, + // then we fallback to `process.cwd()` resolution which could fail actually, + // but we're only trying to resolve `prettier` and `eslint-plugin-prettier` dependencies, + // it would be fine enough + return path.resolve(process.cwd(), '__test__.js') + } +} /* istanbul ignore next */ const cjsRequire =