Skip to content

Commit

Permalink
fix: workaround for --experimental-vm-modules not enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Dec 24, 2023
1 parent e52a432 commit 139cb9e
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions packages/eslint-plugin-mdx/src/configs/recommended.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createRequire } from 'node:module'
import path from 'node:path'

import type { ESLint, Linter } from 'eslint'

Expand All @@ -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 =
Expand Down

0 comments on commit 139cb9e

Please sign in to comment.