rspress icon indicating copy to clipboard operation
rspress copied to clipboard

[Bug]: "the name `Tabs` is defined multiple" when flattening Mdx files

Open SoonIter opened this issue 1 year ago β€’ 4 comments

Version

System:
    OS: macOS 14.2.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 54.31 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Browsers:
    Chrome: 124.0.6367.119
    Chrome Canary: 126.0.6467.2
    Safari: 17.2.1
  npmPackages:
    @rspress/plugin-container-syntax: 1.19.1 => 1.19.1 
    @rspress/plugin-playground: 1.19.1 => 1.19.1 
    @rspress/plugin-preview: 1.19.1 => 1.19.1 
    rspress: 1.19.1 => 1.19.1

Details

Rspress uses flattenMdxContent to inline one mdx to another mdx file. However, this function currently has some bugs.

https://github.com/web-infra-dev/rspress/blob/5121cac6cdc08cc58ed613347d43d48cf90dcc3d/packages/core/src/node/utils/flattenMdxContent.ts#L59

case 1

// a.mdx
import { Tabs } from '@theme'
// index.mdx
import { Tabs } from β€˜@theme’

import A from './a'

<A />

rspress uses flattenMdxContent to inline "a.mdx" into "index.mdx" for better analysis

  Γ— Module build failed:
  ╰─▢   Γ— Error:
        β”‚   Γ— the name `Tabs` is defined multiple times
        β”‚    ╭─[/Users/appe/Documents/demos/rspress-preview-demo/docs/guide/index.mdx:1:1]
        β”‚  1 β”‚ const frontmatter = {};
        β”‚  2 β”‚ import { useMDXComponents as _provideComponents } from "@mdx-js/react";
        β”‚  3 β”‚ import { Tabs } from '@theme';
        β”‚    Β·          ──┬─
        β”‚    Β·            ╰── previous definition of `Tabs` here
        β”‚  4 β”‚ import { Tabs } from '@theme';
        β”‚    Β·          ──┬─
        β”‚    Β·            ╰── `Tabs` redefined here
        β”‚  5 β”‚ function _createMdxContent(props) {
        β”‚  6 β”‚     const _components = Object.assign({
        β”‚  7 β”‚         h1: "h1",
        β”‚    ╰────
        β”‚
        β”‚

Case 2

// a.mdx
import { Tabs } from '@theme'
// b.mdx
import { Tabs } from '@theme'
// index.mdx
import A from './a'

import B from './b'

<A />

<B />

same error

Possible Solution

We need to refactor this function and find the best solution to combine two mdx fragments into one, just like concatenation module or scope hoisting

https://webpack.js.org/plugins/module-concatenation-plugin/#root

Reproduce link

https://github.com/SoonIter/rspress-mdx-flatten

Reproduce Steps

  1. pnpm install
  2. npm run dev

SoonIter avatar May 09 '24 07:05 SoonIter

we need to rename the top-level variables when merging two mdx files to solve this issue

https://github.com/webpack/webpack/pull/18348/files#diff-820a04017e95cec05fd58ca44cd7f6c9cfa5b698a144471d5a0f523932486c56R1403

SoonIter avatar May 13 '24 05:05 SoonIter

I canβ€˜t reproduce the first case, is it correct?

shulaoda avatar May 13 '24 16:05 shulaoda

I canβ€˜t reproduce the first case, is it correct?

I'm sure

You can checkout https://github.com/SoonIter/rspress-mdx-flatten/tree/case_1 and https://github.com/SoonIter/rspress-mdx-flatten/tree/case_2

SoonIter avatar May 13 '24 16:05 SoonIter

I'm sure

You can checkout https://github.com/SoonIter/rspress-mdx-flatten/tree/case_1 and https://github.com/SoonIter/rspress-mdx-flatten/tree/case_2

I got what you mean. The first case did not directly use the <a />, which caused me to misunderstand.

shulaoda avatar May 13 '24 17:05 shulaoda