Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mdx): support custom parse construct options #8009

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions crates/turbopack-mdx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![feature(arbitrary_self_types)]

use anyhow::{anyhow, Context, Result};
use mdxjs::{compile, Options};
use mdxjs::{compile, MdxParseOptions, Options};
use turbo_tasks::{Value, ValueDefault, Vc};
use turbo_tasks_fs::{rope::Rope, File, FileContent, FileSystemPath};
use turbopack_core::{
Expand Down Expand Up @@ -31,26 +31,42 @@ fn modifier() -> Vc<String> {
Vc::cell("mdx".to_string())
}

#[turbo_tasks::value(shared)]
#[derive(PartialOrd, Ord, Hash, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub enum MdxParseConstructs {
Commonmark,
Gfm,
}

/// Subset of mdxjs::Options to allow to inherit turbopack's jsx-related configs
/// into mdxjs.
/// into mdxjs. This is thin, near straightforward subset of mdxjs::Options to
/// enable turbo tasks.
#[turbo_tasks::value(shared)]
#[derive(PartialOrd, Ord, Hash, Debug, Clone)]
#[serde(rename_all = "camelCase", default)]
pub struct MdxTransformOptions {
pub development: bool,
pub preserve_jsx: bool,
pub development: Option<bool>,
pub jsx: Option<bool>,
pub jsx_runtime: Option<String>,
pub jsx_import_source: Option<String>,
/// The path to a module providing Components to mdx modules.
/// The provider must export a useMDXComponents, which is called to access
/// an object of components.
pub provider_import_source: Option<String>,
/// Determines how to parse mdx contents.
pub mdx_type: Option<MdxParseConstructs>,
}

impl Default for MdxTransformOptions {
fn default() -> Self {
Self {
development: true,
preserve_jsx: false,
development: Some(true),
jsx: Some(false),
jsx_runtime: None,
jsx_import_source: None,
provider_import_source: None,
mdx_type: Some(MdxParseConstructs::Commonmark),
}
}
}
Expand Down Expand Up @@ -112,10 +128,16 @@ async fn into_ecmascript_module_asset(
None
};

let parse_options = match transform_options.mdx_type {
Some(MdxParseConstructs::Gfm) => MdxParseOptions::gfm(),
_ => MdxParseOptions::default(),
};

let options = Options {
development: transform_options.development,
parse: parse_options,
development: transform_options.development.unwrap_or(false),
provider_import_source: transform_options.provider_import_source.clone(),
jsx: transform_options.preserve_jsx, // true means 'preserve' jsx syntax.
jsx: transform_options.jsx.unwrap_or(false), // true means 'preserve' jsx syntax.
jsx_runtime,
jsx_import_source: transform_options
.jsx_import_source
Expand Down
11 changes: 4 additions & 7 deletions crates/turbopack/src/module_options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use turbopack_core::{
};
use turbopack_css::CssModuleAssetType;
use turbopack_ecmascript::{EcmascriptInputTransform, EcmascriptOptions, SpecifiedModuleType};
use turbopack_mdx::MdxTransformOptions;
use turbopack_node::transforms::{postcss::PostCssTransform, webpack::WebpackLoaders};
use turbopack_wasm::source::WebAssemblySourceType;

Expand Down Expand Up @@ -491,16 +490,14 @@ impl ModuleOptions {
(None, None)
};

let mdx_options = enable_mdx_rs
.unwrap_or(MdxTransformModuleOptions::default())
.await?;
let mdx_options = &*enable_mdx_rs.unwrap_or(Default::default()).await?;

let mdx_transform_options = (MdxTransformOptions {
development: true,
preserve_jsx: false,
development: Some(true),
jsx: Some(false),
jsx_runtime,
jsx_import_source,
provider_import_source: mdx_options.provider_import_source.clone(),
..(mdx_options.clone())
})
.cell();

Expand Down
21 changes: 2 additions & 19 deletions crates/turbopack/src/module_options/module_options_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use turbopack_core::{
condition::ContextCondition, environment::Environment, resolve::options::ImportMapping,
};
use turbopack_ecmascript::{references::esm::UrlRewriteBehavior, TreeShakingMode};
pub use turbopack_mdx::MdxTransformOptions;
use turbopack_node::{
execution_context::ExecutionContext,
transforms::{postcss::PostCssTransformOptions, webpack::WebpackLoaderItems},
Expand Down Expand Up @@ -100,24 +101,6 @@ pub struct JsxTransformOptions {
pub runtime: Option<String>,
}

#[turbo_tasks::value(shared)]
#[derive(Default, Clone)]
#[serde(default)]
pub struct MdxTransformModuleOptions {
/// The path to a module providing Components to mdx modules.
/// The provider must export a useMDXComponents, which is called to access
/// an object of components.
pub provider_import_source: Option<String>,
}

#[turbo_tasks::value_impl]
impl MdxTransformModuleOptions {
#[turbo_tasks::function]
pub fn default() -> Vc<Self> {
Self::cell(Default::default())
}
}

#[turbo_tasks::value(shared)]
#[derive(Default, Clone)]
#[serde(default)]
Expand All @@ -139,7 +122,7 @@ pub struct ModuleOptionsContext {
pub enable_raw_css: bool,
// [Note]: currently mdx, and mdx_rs have different configuration entrypoint from next.config.js,
// however we might want to unify them in the future.
pub enable_mdx_rs: Option<Vc<MdxTransformModuleOptions>>,
pub enable_mdx_rs: Option<Vc<MdxTransformOptions>>,
pub preset_env_versions: Option<Vc<Environment>>,
/// Custom rules to be applied after all default rules.
pub custom_rules: Vec<ModuleRule>,
Expand Down
Loading