Skip to content

Commit

Permalink
Merge pull request #454 from hashicorp/ds.v5-enable-imports
Browse files Browse the repository at this point in the history
feat: restore useDynamicImport
  • Loading branch information
dstaley authored Mar 21, 2024
2 parents c38aa37 + 01a7ddd commit bb5aafb
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 19 deletions.
2 changes: 1 addition & 1 deletion __tests__/rsc.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: MPL-2.0
*/

import { compileMDX } from '../rsc'
import { compileMDX } from '../src/rsc'

import { describe, test, expect } from 'vitest'

Expand Down
32 changes: 27 additions & 5 deletions __tests__/serialize.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { paragraphCustomAlerts } from '@hashicorp/remark-plugins'
import * as MDX from '@mdx-js/react'
import { VFile } from 'vfile'

import { MDXRemote } from '../'
import { serialize } from '../serialize'
import { MDXRemote } from '../src'
import { serialize } from '../src/serialize'
import { renderStatic } from './utils'

import { describe, test, expect } from 'vitest'
Expand Down Expand Up @@ -113,15 +113,37 @@ describe('serialize', () => {
expect(result).toMatchInlineSnapshot(`"<p>Hello world</p>"`)
})

test('strips imports & exports', async () => {
const result = await renderStatic(`import foo from 'bar'
test('strips imports & exports by default', async () => {
const source = `import foo from 'bar'
foo **bar**
export const bar = 'bar'`)
export const bar = 'bar'`

const { compiledSource } = await serialize(source)
expect(compiledSource).not.toMatch(/import/)

const result = await renderStatic(source)
expect(result).toMatchInlineSnapshot(`"<p>foo <strong>bar</strong></p>"`)
})

test('keeps imports & exports if useDynamicImport is true', async () => {
const source = `import foo from 'bar'
foo **bar**
export const bar = 'bar'`

const { compiledSource } = await serialize(source, {
mdxOptions: { useDynamicImport: true },
})
expect(compiledSource).toMatch(/await import/)

// We specifically don't attempt to render this source given that it won't
// run correctly since we're emitting a top-level await and executing the
// code in a non-async context.
})

test('fragments', async () => {
const components = {
Test: ({ content }: { content: string }) => <>{content}</>,
Expand Down
12 changes: 1 addition & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ function getCompileOptions(
mdxOptions: SerializeOptions['mdxOptions'] = {},
rsc: boolean = false
): CompileOptions {
const areImportsEnabled = mdxOptions.useDynamicImport ?? false
// don't modify the original object when adding our own plugin
// this allows code to reuse the same options object
const remarkPlugins = [
...(mdxOptions.remarkPlugins || []),
removeImportsExportsPlugin,
...(areImportsEnabled ? [] : [removeImportsExportsPlugin]),
]

return {
Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export interface SerializeOptions {
* These options are passed to the MDX compiler.
* See [the MDX docs.](https://github.com/mdx-js/mdx/blob/master/packages/mdx/index.js).
*/
mdxOptions?: Omit<CompileOptions, 'outputFormat' | 'providerImportSource'>
mdxOptions?: Omit<CompileOptions, 'outputFormat' | 'providerImportSource'> & {
useDynamicImport?: boolean
}
/**
* Indicate whether or not frontmatter should be parsed out of the MDX. Defaults to false
*/
Expand Down

0 comments on commit bb5aafb

Please sign in to comment.