@@ -46,6 +46,10 @@ import type {
4646} from './types'
4747
4848let acorn : typeof import ( 'acorn' )
49+ let acornJsx : {
50+ default : typeof import ( 'acorn-jsx' )
51+ }
52+ let acornParser : typeof import ( 'acorn' ) . Parser
4953
5054let tokTypes : typeof _tokTypes
5155let jsxTokTypes : Record < string , TokenType >
@@ -60,6 +64,7 @@ const explorer = cosmiconfig('remark', {
6064export const processorCache = new Map < string , FrozenProcessor > ( )
6165
6266const getRemarkMdxOptions = ( tokens : Token [ ] ) : Options => ( {
67+ acorn : acornParser ,
6368 acornOptions : {
6469 ecmaVersion : 'latest' ,
6570 sourceType : 'module' ,
@@ -181,6 +186,20 @@ runAsWorker(
181186 WorkerOptions ) : Promise < WorkerResult > => {
182187 sharedTokens . length = 0
183188
189+ /**
190+ * unified plugins are using ESM version of acorn,
191+ * so we have to use the same version as well,
192+ * otherwise the TokenType will be different class
193+ * @see also https://github.com/acornjs/acorn-jsx/issues/133
194+ */
195+ if ( ! acorn ) {
196+ acorn = await loadEsmModule < typeof import ( 'acorn' ) > ( 'acorn' )
197+ acornJsx = await loadEsmModule < { default : typeof import ( 'acorn-jsx' ) } > (
198+ 'acorn-jsx' ,
199+ )
200+ acornParser = acorn . Parser . extend ( acornJsx . default ( ) )
201+ }
202+
184203 const processor = await getRemarkProcessor (
185204 physicalFilename ,
186205 isMdx ,
@@ -205,27 +224,13 @@ runAsWorker(
205224 }
206225 }
207226
208- /**
209- * unified plugins are using ESM version of acorn,
210- * so we have to use the same version as well,
211- * otherwise the TokenType will be different class
212- * @see also https://github.com/acornjs/acorn-jsx/issues/133
213- */
214- if ( ! acorn ) {
215- acorn = await loadEsmModule < typeof import ( 'acorn' ) > ( 'acorn' )
216- }
217-
218227 if ( ! tokTypes ) {
219228 tokTypes = acorn . tokTypes
220229 }
221230
222231 if ( ! jsxTokTypes ) {
223232 // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
224- jsxTokTypes = (
225- await loadEsmModule < { default : typeof import ( 'acorn-jsx' ) } > (
226- 'acorn-jsx' ,
227- )
228- ) . default (
233+ jsxTokTypes = acornJsx . default (
229234 {
230235 allowNamespacedObjects : true ,
231236 } ,
0 commit comments