wooorm / xdm

Just a *really* good MDX compiler. No runtime. With esbuild, Rollup, and webpack plugins
http://wooorm.com/xdm/
MIT License
595 stars 18 forks source link

Use `let` for components in output #102

Closed ElMassimo closed 2 years ago

ElMassimo commented 2 years ago

Description 📖

This pull request changes the variable declaration for _components and destructured component identifiers (which must be passed via props), to use let instead of const.

This is part 1 of the discussed changes to facilitate individual component resolution in Vue (or other frameworks).

Why? 🤔

By using let instead of const, it enables re-assignment of the destructured components:

Before

        const _components = Object.assign({
            h1: "h1",
        }, _provideComponents(), props.components)
          , {MyComponent} = _components;
        if (!MyComponent)
            _missingMdxReference("MyComponent", true)

After

        let _components = Object.assign({
            h1: "h1",
        }, _provideComponents(), props.components)
          , {MyComponent} = _components;
        if (!MyComponent)
            _missingMdxReference("MyComponent", true)

which would allow a recma plugin to walk through _missingMdxReference calls and replace the call with an assignment of the call expression:

        if (!MyComponent)
            MyComponent = _missingMdxReference("MyComponent", true)

Considerations

Replacing const with let shouldn't have any practical downsides.

I though about adding the assignment in this pull request, but it increases the output size and it doesn't serve a purpose without additional transformations, so that is definitely a downside.