Skip to content

Commit

Permalink
feat(node): expose the moduleTypes option (#1451)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyf0 authored Jun 27, 2024
1 parent fcfd4a1 commit 39f16e2
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// cSpell:disable

use std::collections::HashMap;

use crate::types::{binding_log::BindingLog, binding_log_level::BindingLogLevel};
use derivative::Derivative;
use napi::threadsafe_function::ThreadsafeFunction;
Expand Down Expand Up @@ -68,6 +70,8 @@ pub struct BindingInputOptions {
pub cwd: String,
// pub builtins: BuiltinsOptions,
pub treeshake: Option<treeshake::BindingTreeshake>,

pub module_types: Option<HashMap<String, String>>,
}

pub type BindingOnLog = Option<ThreadsafeFunction<(String, BindingLog), (), false>>;
19 changes: 16 additions & 3 deletions crates/rolldown_binding/src/utils/normalize_binding_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use crate::{
worker_manager::WorkerManager,
};
use napi::Either;
use rolldown::{AddonOutputOption, BundlerOptions, IsExternal, OutputFormat, Platform};
use rolldown::{AddonOutputOption, BundlerOptions, IsExternal, ModuleType, OutputFormat, Platform};
use rolldown_plugin::BoxPlugin;
use std::path::PathBuf;
#[cfg(not(target_family = "wasm"))]
use std::sync::Arc;
use std::{collections::HashMap, path::PathBuf, str::FromStr};

#[cfg_attr(target_family = "wasm", allow(unused))]
pub struct NormalizeBindingOptionsReturn {
Expand Down Expand Up @@ -80,6 +80,19 @@ pub fn normalize_binding_options(
}))
});

let mut module_types = None;
if let Some(raw) = input_options.module_types {
let mut tmp = HashMap::with_capacity(raw.len());
for (k, v) in raw {
tmp.insert(
k,
ModuleType::from_str(&v)
.map_err(|err| napi::Error::new(napi::Status::GenericFailure, err))?,
);
}
module_types = Some(tmp);
}

let bundler_options = BundlerOptions {
input: Some(input_options.input.into_iter().map(Into::into).collect()),
cwd: cwd.into(),
Expand Down Expand Up @@ -110,7 +123,7 @@ pub fn normalize_binding_options(
"cjs" => OutputFormat::Cjs,
_ => panic!("Invalid format: {format_str}"),
}),
module_types: None,
module_types,
};

#[cfg(not(target_family = "wasm"))]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::str::FromStr;

#[cfg(feature = "deserialize_bundler_options")]
use schemars::JsonSchema;
#[cfg(feature = "deserialize_bundler_options")]
Expand All @@ -20,3 +22,22 @@ pub enum ModuleType {
Binary,
Empty,
}

impl FromStr for ModuleType {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"js" => Ok(Self::Js),
"jsx" => Ok(Self::Jsx),
"ts" => Ok(Self::Ts),
"tsx" => Ok(Self::Tsx),
"json" => Ok(Self::Json),
"text" => Ok(Self::Text),
"base64" => Ok(Self::Base64),
"binary" => Ok(Self::Binary),
"empty" => Ok(Self::Empty),
_ => Err(format!("Unknown module type: {s}")),
}
}
}
1 change: 1 addition & 0 deletions packages/rolldown/src/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export interface BindingInputOptions {
onLog: (logLevel: 'debug' | 'warn' | 'info', log: BindingLog) => void
cwd: string
treeshake?: BindingTreeshake
moduleTypes?: Record<string, string>
}

export interface BindingJsonSourcemap {
Expand Down
1 change: 1 addition & 0 deletions packages/rolldown/src/options/bindingify-input-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export function bindingifyInputOptions(
options.onLog(level, { code: log.code, message: log.message })
},
treeshake: options.treeshake,
moduleTypes: options.moduleTypes,
}
}

Expand Down
15 changes: 15 additions & 0 deletions packages/rolldown/src/options/input-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ const inputOptionsSchema = z.strictObject({
),
)
.optional(),
moduleTypes: z
.record(
z
.literal('js')
.or(z.literal('jsx'))
.or(z.literal('ts'))
.or(z.literal('tsx'))
.or(z.literal('empty'))
.or(z.literal('json'))
.or(z.literal('text'))
.or(z.literal('base64'))
.or(z.literal('binary'))
.or(z.literal('empty')),
)
.optional(),
})

export type InputOption = z.infer<typeof inputOptionSchema>
Expand Down

0 comments on commit 39f16e2

Please sign in to comment.