From 47e6b180f42374d9d0b7b0547594eb71fa81690e Mon Sep 17 00:00:00 2001 From: OJ Kwon <1210596+kwonoj@users.noreply.github.com> Date: Tue, 15 Aug 2023 11:19:16 -0700 Subject: [PATCH] fix(next-swc): coerce mdxrs default options (#54068) ### 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 --- packages/next/src/build/swc/index.ts | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/packages/next/src/build/swc/index.ts b/packages/next/src/build/swc/index.ts index 772ffe2005eb..8583300348d7 100644 --- a/packages/next/src/build/swc/index.ts +++ b/packages/next/src/build/swc/index.ts @@ -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 @@ -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 @@ -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)) }