Skip to content

Commit

Permalink
add mdx providerImportSource option (#4944)
Browse files Browse the repository at this point in the history
### Description

<!--
  ✍️ Write a short summary of your work.
  If necessary, include relevant screenshots.
-->

### Testing Instructions

<!--
  Give a quick description of steps to test your changes.
-->

fix WEB-1012
  • Loading branch information
ForsakenHarmony authored May 15, 2023
1 parent 3dd1dac commit 5ae1c0f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
3 changes: 3 additions & 0 deletions crates/turbopack-mdx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub struct MdxTransformOptions {
pub preserve_jsx: bool,
pub jsx_runtime: Option<String>,
pub jsx_import_source: Option<String>,
pub provider_import_source: Option<String>,
}

impl Default for MdxTransformOptions {
Expand All @@ -49,6 +50,7 @@ impl Default for MdxTransformOptions {
preserve_jsx: false,
jsx_runtime: None,
jsx_import_source: None,
provider_import_source: None,
}
}
}
Expand Down Expand Up @@ -108,6 +110,7 @@ async fn into_ecmascript_module_asset(

let options = Options {
development: transform_options.development,
provider_import_source: transform_options.provider_import_source.clone(),
jsx: transform_options.preserve_jsx, // true means 'preserve' jsx syntax.
jsx_runtime,
jsx_import_source: transform_options
Expand Down
7 changes: 6 additions & 1 deletion crates/turbopack/src/module_options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,19 +414,24 @@ impl ModuleOptionsVc {
),
];

if enable_mdx || enable_mdx_rs {
if enable_mdx || enable_mdx_rs.is_some() {
let (jsx_runtime, jsx_import_source) = if let Some(enable_jsx) = enable_jsx {
let jsx = enable_jsx.await?;
(jsx.runtime.clone(), jsx.import_source.clone())
} else {
(None, None)
};

let mdx_options = enable_mdx_rs
.unwrap_or(MdxTransformModuleOptionsVc::default())
.await?;

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

Expand Down
39 changes: 20 additions & 19 deletions crates/turbopack/src/module_options/module_options_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,47 +128,48 @@ pub struct CustomEcmascriptTransformPlugins {

#[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 MdxTransformModuleOptionsVc {
#[turbo_tasks::function]
pub fn default() -> Self {
Self::cell(Default::default())
}
}

#[turbo_tasks::value(shared)]
#[derive(Default, Clone)]
#[serde(default)]
pub struct ModuleOptionsContext {
#[serde(default)]
pub enable_jsx: Option<JsxTransformOptionsVc>,
#[serde(default)]
pub enable_emotion: Option<EmotionTransformConfigVc>,
#[serde(default)]
pub enable_styled_components: Option<StyledComponentsTransformConfigVc>,
#[serde(default)]
pub enable_styled_jsx: bool,
#[serde(default)]
pub enable_postcss_transform: Option<PostCssTransformOptions>,
#[serde(default)]
pub enable_webpack_loaders: Option<WebpackLoadersOptions>,
#[serde(default)]
pub enable_types: bool,
#[serde(default)]
pub enable_typescript_transform: Option<TypescriptTransformOptionsVc>,
#[serde(default)]
pub decorators: Option<DecoratorsOptionsVc>,
#[serde(default)]
pub enable_mdx: 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.
#[serde(default)]
pub enable_mdx_rs: bool,
#[serde(default)]
pub enable_mdx_rs: Option<MdxTransformModuleOptionsVc>,
pub preset_env_versions: Option<EnvironmentVc>,
#[serde(default)]
pub custom_ecma_transform_plugins: Option<CustomEcmascriptTransformPluginsVc>,
#[serde(default)]
/// Custom rules to be applied after all default rules.
pub custom_rules: Vec<ModuleRule>,
#[serde(default)]
pub execution_context: Option<ExecutionContextVc>,
#[serde(default)]
/// A list of rules to use a different module option context for certain
/// context paths. The first matching is used.
pub rules: Vec<(ContextCondition, ModuleOptionsContextVc)>,
#[serde(default)]
pub placeholder_for_future_extensions: (),
#[serde(default)]
pub enable_tree_shaking: bool,
}

Expand Down

0 comments on commit 5ae1c0f

Please sign in to comment.