Skip to content

Commit

Permalink
fix(next-swc): coerce mdxrs default options (#54068)
Browse files Browse the repository at this point in the history
### What?

wasm-bindgen's serde deserialization is more strict to not automatically coerce non-optionable default values for the mdx configurations. Since these are required options anyway, consolidate to construct default options.


Closes WEB-1384
  • Loading branch information
kwonoj committed Aug 15, 2023
1 parent 36602e7 commit 47e6b18
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions packages/next/src/build/swc/index.ts
Expand Up @@ -1135,9 +1135,9 @@ async function loadWasm(importPath = '', isCustomTurbopack: boolean) {
},
mdx: {
compile: (src: string, options: any) =>
bindings.mdxCompile(src, options),
bindings.mdxCompile(src, getMdxOptions(options)),
compileSync: (src: string, options: any) =>
bindings.mdxCompileSync(src, options),
bindings.mdxCompileSync(src, getMdxOptions(options)),
},
}
return wasmBindings
Expand Down Expand Up @@ -1409,9 +1409,9 @@ function loadNative(isCustomTurbopack = false, importPath?: string) {
},
mdx: {
compile: (src: string, options: any) =>
bindings.mdxCompile(src, toBuffer(options ?? {})),
bindings.mdxCompile(src, toBuffer(getMdxOptions(options))),
compileSync: (src: string, options: any) =>
bindings.mdxCompileSync(src, toBuffer(options ?? {})),
bindings.mdxCompileSync(src, toBuffer(getMdxOptions(options))),
},
}
return nativeBindings
Expand All @@ -1420,6 +1420,22 @@ function loadNative(isCustomTurbopack = false, importPath?: string) {
throw attempts
}

/// Build a mdx options object contains default values that
/// can be parsed with serde_wasm_bindgen.
function getMdxOptions(options: any = {}) {
const ret = {
...options,
development: options.development ?? false,
jsx: options.jsx ?? false,
parse: options.parse ?? {
gfmStrikethroughSingleTilde: true,
mathTextSingleDollar: true,
},
}

return ret
}

function toBuffer(t: any) {
return Buffer.from(JSON.stringify(t))
}
Expand Down

0 comments on commit 47e6b18

Please sign in to comment.