diff --git a/.changeset/fix-esm-dirname.md b/.changeset/fix-esm-dirname.md new file mode 100644 index 0000000..dd0f354 --- /dev/null +++ b/.changeset/fix-esm-dirname.md @@ -0,0 +1,11 @@ +--- +'rspress-plugin-google-analytics': patch +'rspress-plugin-back-to-top': patch +'rspress-plugin-file-tree': patch +'rspress-plugin-live2d': patch +'rspress-plugin-mermaid': patch +'rspress-plugin-reading-time': patch +'rspress-plugin-vercel-analytics': patch +--- + +fix: replace `__dirname` with ESM-compatible `import.meta.url` to fix `ReferenceError: __dirname is not defined` when loading plugins as ESM diff --git a/packages/rspress-plugin-back-to-top/src/index.ts b/packages/rspress-plugin-back-to-top/src/index.ts index aba38e4..38c4e37 100644 --- a/packages/rspress-plugin-back-to-top/src/index.ts +++ b/packages/rspress-plugin-back-to-top/src/index.ts @@ -1,10 +1,14 @@ import path from 'node:path'; +import { fileURLToPath } from 'node:url'; import { PresetConfigMutator } from 'rspress-plugin-devkit'; import type { RspressPlugin } from '@rspress/core'; import type { Back2TopProps } from './components/Back2Top'; +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + export const componentsPath = path.join(__dirname, './components'); export interface RspressPluginBack2TopOptions extends Back2TopProps {} diff --git a/packages/rspress-plugin-file-tree/src/index.ts b/packages/rspress-plugin-file-tree/src/index.ts index b09375c..b328d40 100644 --- a/packages/rspress-plugin-file-tree/src/index.ts +++ b/packages/rspress-plugin-file-tree/src/index.ts @@ -1,4 +1,5 @@ import path from 'node:path'; +import { fileURLToPath } from 'node:url'; import { RemarkCodeBlockToGlobalComponentPluginFactory } from 'rspress-plugin-devkit'; @@ -6,6 +7,9 @@ import { parseTreeContent } from './components/tree-parser/tree-parser'; import type { RspressPlugin } from '@rspress/core'; +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + interface RspressPluginFileTreeOptions { initialExpandDepth?: number; } diff --git a/packages/rspress-plugin-google-analytics/src/index.ts b/packages/rspress-plugin-google-analytics/src/index.ts index 8f78cc9..1ebee92 100644 --- a/packages/rspress-plugin-google-analytics/src/index.ts +++ b/packages/rspress-plugin-google-analytics/src/index.ts @@ -2,6 +2,10 @@ import { ensureArray, type MaybeArray } from 'rspress-plugin-devkit'; import type { RspressPlugin } from '@rspress/core'; import path from 'path'; +import { fileURLToPath } from 'url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); interface RspressPluginSiteMapOptions { id: MaybeArray; diff --git a/packages/rspress-plugin-live2d/src/index.ts b/packages/rspress-plugin-live2d/src/index.ts index 7eae291..c167dba 100644 --- a/packages/rspress-plugin-live2d/src/index.ts +++ b/packages/rspress-plugin-live2d/src/index.ts @@ -1,8 +1,12 @@ import path from 'node:path'; +import { fileURLToPath } from 'node:url'; import type { RspressPlugin } from '@rspress/core'; import type { Live2DWidgetProps } from './typings'; +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + export interface RspressPluginLive2dOptions extends Live2DWidgetProps {} export default function rspressPluginLive2d( diff --git a/packages/rspress-plugin-mermaid/src/index.ts b/packages/rspress-plugin-mermaid/src/index.ts index 8d24c79..3bed174 100644 --- a/packages/rspress-plugin-mermaid/src/index.ts +++ b/packages/rspress-plugin-mermaid/src/index.ts @@ -1,4 +1,5 @@ import path from 'node:path'; +import { fileURLToPath } from 'node:url'; import { PresetConfigMutator, @@ -9,6 +10,9 @@ import type { RspressPlugin } from '@rspress/core'; import type { MermaidConfig } from 'mermaid'; import type { MermaidRendererProps } from './typings'; +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + interface RspressPluginMermaidOptions { mermaidConfig?: MermaidConfig; } diff --git a/packages/rspress-plugin-reading-time/src/index.ts b/packages/rspress-plugin-reading-time/src/index.ts index a47eb80..1932b9d 100644 --- a/packages/rspress-plugin-reading-time/src/index.ts +++ b/packages/rspress-plugin-reading-time/src/index.ts @@ -1,4 +1,5 @@ import path from 'node:path'; +import { fileURLToPath } from 'node:url'; import readingTime, { type ReadTimeResults } from 'reading-time'; @@ -10,6 +11,9 @@ import { import type { RspressPlugin } from '@rspress/core'; +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + export interface RspressPluginReadingTimeOptions extends WithDefaultLocale { getReadingTime?: (content: string) => ReadTimeResults; } diff --git a/packages/rspress-plugin-vercel-analytics/src/index.ts b/packages/rspress-plugin-vercel-analytics/src/index.ts index 8b7cb38..70bedb9 100644 --- a/packages/rspress-plugin-vercel-analytics/src/index.ts +++ b/packages/rspress-plugin-vercel-analytics/src/index.ts @@ -1,8 +1,12 @@ import path from 'node:path'; +import { fileURLToPath } from 'node:url'; import type { RspressPlugin } from '@rspress/core'; import type { VercelAnalyticsProps } from './typings'; +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + interface RspressPluginVercelAnalyticsOptions extends VercelAnalyticsProps {} export default function rspressPluginVercelAnalytics(