Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove runtime renderer, still support MDXProvider #1425

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions packages/loader/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
const {getOptions} = require('loader-utils')
const mdx = require('@mdx-js/mdx')

const DEFAULT_RENDERER = `
import React from 'react'
import {mdx} from '@mdx-js/react'
`
const DEFAULT_RENDERER = 'import React from "react"'

module.exports = async function (content) {
const callback = this.async()
Expand Down
30 changes: 8 additions & 22 deletions packages/loader/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ const webpack = require('webpack')
const MemoryFs = require('memory-fs')
const React = require('react')
const {renderToString} = require('react-dom/server')
const _extends = require('@babel/runtime/helpers/extends')
const _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties')
const {mdx} = require('../../react')
const {useMDXComponents} = require('../../react')

const transform = (filePath, options) => {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -57,30 +55,18 @@ const transform = (filePath, options) => {
}

const run = value => {
// Webpack 5 (replace everything in this function with):
// const val = 'return ' + value.replace(/__webpack_require__\(0\)/, 'return $&')
//
// // eslint-disable-next-line no-new-func
// return new Function(val)().default
// Replace import/exports w/ parameters and return value.
const val = value
.replace(
/import _objectWithoutProperties from "@babel\/runtime\/helpers\/objectWithoutProperties";/,
''
)
.replace(/import _extends from "@babel\/runtime\/helpers\/extends";/, '')
.replace(/import React from 'react';/, '')
.replace(/import \{ mdx } from '@mdx-js\/react';/, '')
.replace(/import "core-js\/.+";/g, '')
.replace(/import React from "react";/, '')
.replace(/import \{.+?\} from "@mdx-js\/react";/g, '')
.replace(/export default/, 'return')

// eslint-disable-next-line no-new-func
return new Function(
'mdx',
'React',
'_extends',
'_objectWithoutProperties',
val
)(mdx, React, _extends, _objectWithoutProperties)
return new Function('React', '__provideComponents', val)(
React,
useMDXComponents
)
}

describe('@mdx-js/loader', () => {
Expand Down
7 changes: 6 additions & 1 deletion packages/mdx/.babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
"useBuiltIns": "usage"
}
],
"@babel/react"
[
"@babel/react",
{
"throwIfNamespace": false
}
]
]
}
14 changes: 12 additions & 2 deletions packages/mdx/estree-to-js.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const astring = require('astring')
const stringifyEntities = require('stringify-entities/light')

module.exports = estreeToJs

Expand All @@ -20,7 +21,7 @@ const customGenerator = Object.assign({}, astring.baseGenerator, {
})

function estreeToJs(estree) {
return astring.generate(estree, {generator: customGenerator})
return astring.generate(estree, {generator: customGenerator, comments: true})
}

// `attr="something"`
Expand All @@ -30,7 +31,16 @@ function JSXAttribute(node, state) {

if (node.value != null) {
state.write('=')
this[node.value.type](node.value, state)

// Encode double quotes in attribute values.
if (node.value.type === 'Literal' && typeof node.value.value === 'string') {
state.write(
'"' + stringifyEntities(node.value.value, {subset: ['"']}) + '"',
node
)
} else {
this[node.value.type](node.value, state)
}
}
}

Expand Down
12 changes: 4 additions & 8 deletions packages/mdx/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ const minifyWhitespace = require('rehype-minify-whitespace')
const mdxAstToMdxHast = require('./mdx-ast-to-mdx-hast')
const mdxHastToJsx = require('./mdx-hast-to-jsx')

const pragma = `/* @jsxRuntime classic */
/* @jsx mdx */
/* @jsxFrag mdx.Fragment */`

function createMdxAstCompiler(options = {}) {
return unified()
.use(remarkParse)
Expand Down Expand Up @@ -37,13 +33,13 @@ function createConfig(mdx, options) {
}

function sync(mdx, options = {}) {
const file = createCompiler(options).processSync(createConfig(mdx, options))
return pragma + '\n' + String(file)
return String(createCompiler(options).processSync(createConfig(mdx, options)))
}

async function compile(mdx, options = {}) {
const file = await createCompiler(options).process(createConfig(mdx, options))
return pragma + '\n' + String(file)
return String(
await createCompiler(options).process(createConfig(mdx, options))
)
}

module.exports = compile
Expand Down
Loading