Skip to content

Fix casing of babel option in getting-started.server.mdx#2148

Merged
wooorm merged 1 commit intomdx-js:mainfrom
kochie:patch-1
Oct 7, 2022
Merged

Fix casing of babel option in getting-started.server.mdx#2148
wooorm merged 1 commit intomdx-js:mainfrom
kochie:patch-1

Conversation

@kochie
Copy link
Copy Markdown
Contributor

@kochie kochie commented Oct 7, 2022

Fixes a small typo in the babel config where the sourceFilename is actually sourceFileName in the latest babel version.

@vercel
Copy link
Copy Markdown

vercel Bot commented Oct 7, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
mdx ✅ Ready (Inspect) Visit Preview Oct 7, 2022 at 8:01AM (UTC)

@codecov-commenter
Copy link
Copy Markdown

Codecov Report

Base: 100.00% // Head: 100.00% // No change to project coverage 👍

Coverage data is based on head (6ba3a1e) compared to base (29e93e5).
Patch has no changes to coverable lines.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #2148   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           22        22           
  Lines         2044      2044           
=========================================
  Hits          2044      2044           

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@wooorm
Copy link
Copy Markdown
Member

wooorm commented Oct 7, 2022

Can you confirm that this gave a warning, or didn’t work at all?

I remember running into babel using two casings of I believe this word. I think I raised an issue with them and that it’s been solved.
But wanted to verify!

@kochie
Copy link
Copy Markdown
Contributor Author

kochie commented Oct 7, 2022

Yeah I just wrote a transformer for jest and needed to change the case to get it working. No warning just a silent failure because the value was undefined.

// import { compile, compileSync } from '@mdx-js/mdx'
import babel from '@babel/core'

import path from 'path'
import parser from '@babel/parser'
import estreeToBabel from 'estree-to-babel'
import { compileSync } from '@mdx-js/mdx'

export function babelPluginSyntaxMdx() {
  // Tell Babel to use a different parser.
  return { parserOverride: babelParserWithMdx }
}

// A Babel parser that parses MDX files with `@mdx-js/mdx` and passes any
// other things through to the normal Babel parser.
function babelParserWithMdx(value, options) {
  console.log(
    options
    // /\.mdx?$/.test(path.extname(options.sourceFilename))
  )
  if (
    options.sourceFileName &&
    /\.mdx?$/.test(path.extname(options.sourceFileName))
  ) {
    // Babel does not support async parsers, unfortunately.
    return compileSync(
      { value, path: options.sourceFileName },
      // Tell `@mdx-js/mdx` to return a Babel tree instead of serialized JS.
      {
        recmaPlugins: [recmaBabel] /* jsxImportSource: …, otherOptions… */,
        // outputFormat: 'function-body',
      }
    ).result
  }

  return parser.parse(value, options)
}

// A “recma” plugin is a unified plugin that runs on the estree (used by
// `@mdx-js/mdx` and much of the JS ecosystem but not Babel).
// This plugin defines `'estree-to-babel'` as the compiler, which means that
// the resulting Babel tree is given back by `compileSync`.
function recmaBabel() {
  Object.assign(this, { Compiler: estreeToBabel })
}

export default {
  process(sourceText, sourcePath, options) {

    const babelRes = babel.transform(sourceText, {
      filename: sourcePath,

      presets: ['@babel/preset-env'],
      plugins: [babelPluginSyntaxMdx],
    })

    console.log(babelRes.code)

    return babelRes
  },
}

I should point out I'm using jest with esm modules enabled, not sure if that would cause any difference.
[email protected]
@mdx-js/[email protected]
@babel/[email protected]

@wooorm wooorm changed the title Fix typo in getting-started.server.mdx Fix casing of babel option in getting-started.server.mdx Oct 7, 2022
@wooorm wooorm merged commit c3ccee4 into mdx-js:main Oct 7, 2022
@wooorm wooorm added 📚 area/docs This affects documentation 💪 phase/solved Post is done labels Oct 7, 2022
@wooorm
Copy link
Copy Markdown
Member

wooorm commented Oct 7, 2022

Cool, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

📚 area/docs This affects documentation 💪 phase/solved Post is done

Development

Successfully merging this pull request may close these issues.

3 participants