Skip to content
Open
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
33 changes: 17 additions & 16 deletions packages/next-mdx/mdx-js-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,22 @@ module.exports = function nextMdxLoader(...args) {
const callback = this.async().bind(this)
const loaderContext = this

getOptions(options, this.context).then((userProvidedMdxOptions) => {
const proxy = new Proxy(loaderContext, {
get(target, prop, receiver) {
if (prop === 'getOptions') {
return () => userProvidedMdxOptions
}

if (prop === 'async') {
return () => callback
}

return Reflect.get(target, prop, receiver)
},
getOptions(options, this.context)
.then((userProvidedMdxOptions) => {
const proxy = new Proxy(loaderContext, {
get(target, prop, receiver) {
if (prop === 'getOptions') {
return () => userProvidedMdxOptions
}
if (prop === 'async') {
return () => callback
}
return Reflect.get(target, prop, receiver)
},
})
mdxLoader.call(proxy, ...args)
})
.catch((error) => {
callback(error)
})

mdxLoader.call(proxy, ...args)
})
}
49 changes: 49 additions & 0 deletions test/e2e/app-dir/mdx/mdx.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,54 @@
import { nextTestSetup } from 'e2e-utils'

describe('mdx without-mdx-rs - invalid plugin error handling', () => {
const { next, isNextStart } = nextTestSetup({
files: __dirname,
dependencies: {
'@next/mdx': 'canary',
'@mdx-js/loader': '^2.2.1',
'@mdx-js/react': '^2.2.1',
},
env: {
WITH_MDX_RS: 'false',
},
skipStart: true,
})

if (!isNextStart) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will still create the isolated dir before skipping for dev. It's preferable to skip based on importing isNextStart from e2e-utils in this case.

import { nextTestSetup, isNextStart } from 'e2e-utils'

Then use it to skip the describe:

;(isNextStart ? describe : describe.skip)('mdx without-mdx-rs - invalid plugin error handling', () => {

it('should skip in dev mode', () => {})
return
}

beforeAll(async () => {
await next.patchFile(
'next.config.ts',
`
import nextMDX from '@next/mdx'
const withMDX = nextMDX({
options: {
remarkPlugins: ['non-existent-remark-plugin'],
},
})
const nextConfig = {
pageExtensions: ['js', 'jsx', 'ts', 'tsx', 'mdx'],
}
export default withMDX(nextConfig)
`
)
try {
await next.start()
} catch {}
})

it('should report a build error when a plugin path fails to resolve', () => {
// Without the .catch() fix, the webpack callback was never called and
// the build would hang or throw an UnhandledPromiseRejection.
// With the fix, the error is forwarded to webpack and appears in cliOutput.
expect(next.cliOutput).toContain('non-existent-remark-plugin')
expect(next.cliOutput).not.toContain('UnhandledPromiseRejection')
})
})

for (const type of ['with-mdx-rs', 'without-mdx-rs']) {
describe(`mdx ${type}`, () => {
const { next } = nextTestSetup({
Expand Down