Skip to content

Commit

Permalink
Revert "feat(mdx): support custom parse construct options" (#8008)
Browse files Browse the repository at this point in the history
Reverts #8005

Closes PACK-2980
  • Loading branch information
kwonoj committed Apr 19, 2024
1 parent 9bb156a commit e3e52e7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 36 deletions.
38 changes: 8 additions & 30 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, MdxParseOptions, Options};
use mdxjs::{compile, Options};
use turbo_tasks::{Value, ValueDefault, Vc};
use turbo_tasks_fs::{rope::Rope, File, FileContent, FileSystemPath};
use turbopack_core::{
Expand Down Expand Up @@ -31,42 +31,26 @@ 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. This is thin, near straightforward subset of mdxjs::Options to
/// enable turbo tasks.
/// into mdxjs.
#[turbo_tasks::value(shared)]
#[derive(PartialOrd, Ord, Hash, Debug, Clone)]
#[serde(rename_all = "camelCase", default)]
pub struct MdxTransformOptions {
pub development: Option<bool>,
pub jsx: Option<bool>,
pub development: bool,
pub preserve_jsx: 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: Some(true),
jsx: Some(false),
development: true,
preserve_jsx: false,
jsx_runtime: None,
jsx_import_source: None,
provider_import_source: None,
mdx_type: Some(MdxParseConstructs::Commonmark),
}
}
}
Expand Down Expand Up @@ -128,16 +112,10 @@ 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 {
parse: parse_options,
development: transform_options.development.unwrap_or(false),
development: transform_options.development,
provider_import_source: transform_options.provider_import_source.clone(),
jsx: transform_options.jsx.unwrap_or(false), // true means 'preserve' jsx syntax.
jsx: transform_options.preserve_jsx, // true means 'preserve' jsx syntax.
jsx_runtime,
jsx_import_source: transform_options
.jsx_import_source
Expand Down
11 changes: 7 additions & 4 deletions crates/turbopack/src/module_options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ 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 @@ -490,14 +491,16 @@ impl ModuleOptions {
(None, None)
};

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

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

Expand Down
21 changes: 19 additions & 2 deletions crates/turbopack/src/module_options/module_options_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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 @@ -101,6 +100,24 @@ 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 @@ -122,7 +139,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<MdxTransformOptions>>,
pub enable_mdx_rs: Option<Vc<MdxTransformModuleOptions>>,
pub preset_env_versions: Option<Vc<Environment>>,
/// Custom rules to be applied after all default rules.
pub custom_rules: Vec<ModuleRule>,
Expand Down

0 comments on commit e3e52e7

Please sign in to comment.