Skip to content

Commit

Permalink
Add experimental support for imports in evaluate
Browse files Browse the repository at this point in the history
Closes GH-18.

Reviewed-by: Christian Murphy <[email protected]>
  • Loading branch information
wooorm authored Mar 3, 2021
1 parent e87ab66 commit 43429a3
Show file tree
Hide file tree
Showing 8 changed files with 298 additions and 126 deletions.
14 changes: 9 additions & 5 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ import {remarkMdx} from './remark-mdx.js'
* @property {PluggableList} [recmaPlugins] List of recma (esast, JavaScript) plugins
* @property {PluggableList} [remarkPlugins] List of remark (mdast, markdown) plugins
* @property {PluggableList} [rehypePlugins] List of rehype (hast, HTML) plugins
* @property {boolean} [_async=false] Semihidden option
* @property {string} [_baseUrl] Semihidden option
* @property {boolean} [_contain=false] Semihidden option
*
* @typedef {Omit<RecmaDocumentOptions & RecmaStringifyOptions & RecmaJsxRewriteOptions & BaseProcessorOptions, "contain"> } ProcessorOptions
* @typedef {Omit<RecmaDocumentOptions & RecmaStringifyOptions & RecmaJsxRewriteOptions, "_async" | "_contain" | "_baseUrl">} PluginOptions
* @typedef {BaseProcessorOptions & PluginOptions} ProcessorOptions
*/

/**
Expand Down Expand Up @@ -62,7 +65,8 @@ export function compileSync(file, options) {
*/
export function createProcessor(options = {}) {
var {
_contain: contain,
_async,
_contain,
jsx,
providerImportSource,
recmaPlugins,
Expand Down Expand Up @@ -91,11 +95,11 @@ export function createProcessor(options = {}) {
.use(rehypeMarkAndUnravel)
.use(rehypePlugins)
.use(rehypeRecma)
.use(recmaDocument, {...otherOptions, contain})
.use(recmaDocument, {...otherOptions, _async, _contain})
// @ts-ignore recma transformer uses an esast node rather than a unist node
.use(recmaJsxRewrite, {providerImportSource, contain})
.use(recmaJsxRewrite, {providerImportSource, _contain})
// @ts-ignore recma transformer uses an esast node rather than a unist node
.use(jsx ? undefined : recmaJsxBuild, {contain})
.use(jsx ? undefined : recmaJsxBuild, {_contain})
// @ts-ignore recma compiler is seen as a transformer
.use(recmaStringify, {SourceMapGenerator})
.use(recmaPlugins)
Expand Down
28 changes: 20 additions & 8 deletions lib/evaluate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import {compile, compileSync} from './core.js'

/* c8 ignore next 1 */
var AsyncFunction = Object.getPrototypeOf(async function () {}).constructor

/**
* @typedef {import("vfile").VFileCompatible} VFileCompatible
* @typedef {import('./core.js').BaseProcessorOptions} BaseProcessorOptions
Expand All @@ -10,12 +13,16 @@ import {compile, compileSync} from './core.js'
* @property {*} jsxs Function to generate an element with dynamic children
* @property {*} [useMDXComponents] Function to get `MDXComponents` from context
*
* @typedef {Omit<BaseProcessorOptions, "jsx" | "_contain"> } ProcessorOptions
* @typedef {ProcessorOptions & RunnerOptions} ProcessorAndRunnerOptions
* @typedef {Omit<BaseProcessorOptions, "jsx" | "_async" | "_contain" | "_baseUrl"> } ProcessorOptions
*
* @typedef ExtraOptions
* @property {string} baseUrl URL to resolve imports from (typically: pass `import.meta.url`)
*
* @typedef {ProcessorOptions & RunnerOptions & ExtraOptions} ProcessorAndRunnerOptions
*
* @typedef {{ [name: string]: any }} ComponentMap
* @typedef {{ [props: string]: any, components?: ComponentMap }} MDXContentProps
* @typedef {{ [exports: string]: unknown, default: (props: MDXContentProps) => any }} ExportMap
* @typedef {{[name: string]: any}} ComponentMap
* @typedef {{[props: string]: any, components?: ComponentMap}} MDXContentProps
* @typedef {{[exports: string]: unknown, default: (props: MDXContentProps) => any}} ExportMap
*/

/**
Expand All @@ -27,10 +34,13 @@ import {compile, compileSync} from './core.js'
*/
export async function evaluate(file, options) {
var config = split(options)
var _async = Boolean(config.compile._baseUrl)
// What is this?
// V8 on Node 12 can’t handle `eval` for collecting coverage, apparently…
/* c8 ignore next 2 */
return new Function(String(await compile(file, config.compile)))(config.run)
/* c8 ignore next 4 */
return new AsyncFunction(
String(await compile(file, {...config.compile, _async}))
)(config.run)
}

/**
Expand Down Expand Up @@ -58,7 +68,8 @@ function split(options) {
recmaPlugins,
rehypePlugins,
remarkPlugins,
useMDXComponents
useMDXComponents,
baseUrl
} = options || {}

if (!Fragment) throw new Error('Expected `Fragment` given to `evaluate`')
Expand All @@ -68,6 +79,7 @@ function split(options) {
return {
compile: {
_contain: true,
_baseUrl: baseUrl,
providerImportSource: useMDXComponents ? '#' : undefined,
recmaPlugins,
rehypePlugins,
Expand Down
Loading

0 comments on commit 43429a3

Please sign in to comment.