Skip to content

Commit 30cd412

Browse files
committed
fix(parse): prevent undefined error due to bundler optimization
1 parent 738defe commit 30cd412

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/runtime/parser/index.ts

+15-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { unified } from 'unified'
22
import remarkParse from 'remark-parse'
33
import remark2rehype from 'remark-rehype'
44
import { parseFrontMatter } from 'remark-mdc'
5-
import type { Options as VFileOptions } from 'vfile'
5+
import type { VFile, Options as VFileOptions } from 'vfile'
66
import { defu } from 'defu'
77
import type { MdcConfig, MDCData, MDCElement, MDCParseOptions, MDCParserResult, MDCRoot, Toc } from '@nuxtjs/mdc'
88
import { nodeTextContent } from '../utils/node'
@@ -104,9 +104,20 @@ export const createMarkdownParser = async (inlineOptions: MDCParseOptions = {})
104104
const { content, data: frontmatter } = await parseFrontMatter(md)
105105

106106
// Start processing stream
107-
const processedFile = await processor.process({ cwd: typeof process.cwd === 'function' ? process.cwd() : '/tmp', ...fileOptions, value: content, data: frontmatter })
108-
109-
const result = processedFile.result as { body: MDCRoot, excerpt: MDCRoot | undefined }
107+
const cwd = typeof process !== 'undefined' && typeof process.cwd === 'function' ? process.cwd() : '/tmp'
108+
const processedFile: VFile | undefined = await new Promise((resolve, reject) => {
109+
// There is an issue with bundler optimizer which causes undefined error
110+
// When using processor.process as a promise. Use callback instead to avoid this issue
111+
processor.process({ cwd, ...fileOptions, value: content, data: frontmatter }, (err, file) => {
112+
if (err) {
113+
reject(err)
114+
} else {
115+
resolve(file)
116+
}
117+
})
118+
})
119+
120+
const result = processedFile?.result as { body: MDCRoot, excerpt: MDCRoot | undefined }
110121

111122
// Update data with processor data
112123
const data = Object.assign(

0 commit comments

Comments
 (0)