@@ -46,6 +46,10 @@ import type {
46
46
} from './types'
47
47
48
48
let acorn : typeof import ( 'acorn' )
49
+ let acornJsx : {
50
+ default : typeof import ( 'acorn-jsx' )
51
+ }
52
+ let acornParser : typeof import ( 'acorn' ) . Parser
49
53
50
54
let tokTypes : typeof _tokTypes
51
55
let jsxTokTypes : Record < string , TokenType >
@@ -60,6 +64,7 @@ const explorer = cosmiconfig('remark', {
60
64
export const processorCache = new Map < string , FrozenProcessor > ( )
61
65
62
66
const getRemarkMdxOptions = ( tokens : Token [ ] ) : Options => ( {
67
+ acorn : acornParser ,
63
68
acornOptions : {
64
69
ecmaVersion : 'latest' ,
65
70
sourceType : 'module' ,
@@ -181,6 +186,20 @@ runAsWorker(
181
186
WorkerOptions ) : Promise < WorkerResult > => {
182
187
sharedTokens . length = 0
183
188
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
+
184
203
const processor = await getRemarkProcessor (
185
204
physicalFilename ,
186
205
isMdx ,
@@ -205,27 +224,13 @@ runAsWorker(
205
224
}
206
225
}
207
226
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
-
218
227
if ( ! tokTypes ) {
219
228
tokTypes = acorn . tokTypes
220
229
}
221
230
222
231
if ( ! jsxTokTypes ) {
223
232
// 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 (
229
234
{
230
235
allowNamespacedObjects : true ,
231
236
} ,
0 commit comments