Skip to content

Commit

Permalink
feat: add InputOptions#platform (#751)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyf0 committed Apr 4, 2024
1 parent 5e25f31 commit 97ea4b6
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 3 deletions.
1 change: 1 addition & 0 deletions crates/rolldown/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub use crate::{
output_options::{OutputFormat, OutputOptions, SourceMapType},
types::input_item::InputItem,
types::output_option::AddonOutputOption,
types::platform::Platform,
},
types::rolldown_output::RolldownOutput,
};
2 changes: 2 additions & 0 deletions crates/rolldown/src/options/input_options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use rolldown_error::BuildError;
use self::resolve_options::ResolveOptions;

use super::types::input_item::InputItem;
use super::types::platform::Platform;

pub mod resolve_options;

Expand Down Expand Up @@ -62,4 +63,5 @@ pub struct InputOptions {
pub external: Option<External>,
pub treeshake: Option<bool>,
pub resolve: Option<ResolveOptions>,
pub platform: Option<Platform>,
}
3 changes: 2 additions & 1 deletion crates/rolldown/src/options/normalized_input_options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use derivative::Derivative;

use crate::External;

use super::types::input_item::InputItem;
use super::types::{input_item::InputItem, platform::Platform};

pub type SharedNormalizedInputOptions = Arc<NormalizedInputOptions>;

Expand All @@ -18,4 +18,5 @@ pub struct NormalizedInputOptions {
pub cwd: PathBuf,
pub external: External,
pub treeshake: bool,
pub platform: Platform,
}
1 change: 1 addition & 0 deletions crates/rolldown/src/options/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod input_item;
pub mod output_option;
pub mod platform;
20 changes: 20 additions & 0 deletions crates/rolldown/src/options/types/platform.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#[derive(Debug)]
pub enum Platform {
/// Represents the Node.js platform.
Node,
Browser,
Neutral,
}

impl TryFrom<&str> for Platform {
type Error = String;

fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"node" => Ok(Self::Node),
"browser" => Ok(Self::Browser),
"neutral" => Ok(Self::Neutral),
_ => Err(format!("Unknown platform: {value:?}")),
}
}
}
2 changes: 2 additions & 0 deletions crates/rolldown/src/utils/normalize_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use rolldown_resolver::EnforceExtension;
use crate::options::{
normalized_input_options::NormalizedInputOptions,
normalized_output_options::NormalizedOutputOptions, output_options::SourceMapType,
types::platform::Platform,
};

#[allow(clippy::struct_field_names)]
Expand Down Expand Up @@ -68,6 +69,7 @@ pub fn normalize_options(
.unwrap_or_else(|| std::env::current_dir().expect("Failed to get current dir")),
external: raw_input.external.unwrap_or_default(),
treeshake: raw_input.treeshake.unwrap_or(true),
platform: raw_input.platform.unwrap_or(Platform::Browser),
};

// Normalize output options
Expand Down
1 change: 1 addition & 0 deletions crates/rolldown/tests/common/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ impl Fixture {
modules: value.modules,
symlinks: value.symlinks,
}),
..Default::default()
},
OutputOptions {
entry_file_names: "[name].mjs".to_string().into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ pub struct BindingInputOptions {
// strictDeprecations?: boolean;
// pub treeshake: Option<bool>,
// watch?: WatcherOptions | false;
#[napi(ts_type = "'node' | 'browser' | 'neutral'")]
pub platform: Option<String>,

// extra
pub cwd: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
options::plugin::JsPlugin,
types::{binding_rendered_chunk::RenderedChunk, js_callback::MaybeAsyncJsCallbackExt},
};
use rolldown::{AddonOutputOption, InputOptions, OutputOptions};
use rolldown::{AddonOutputOption, InputOptions, OutputOptions, Platform};
use rolldown_error::BuildError;
use rolldown_plugin::BoxPlugin;

Expand Down Expand Up @@ -54,6 +54,12 @@ pub fn normalize_binding_options(
external: external.into(),
treeshake: true.into(),
resolve: input_options.resolve.map(Into::into),
platform: input_options
.platform
.as_deref()
.map(Platform::try_from)
.transpose()
.map_err(|err| napi::Error::new(napi::Status::GenericFailure, err))?,
};

// Deal with output options
Expand Down
1 change: 1 addition & 0 deletions crates/rolldown_binding_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub fn bundle(file_list: Vec<FileItem>) -> Vec<AssetItem> {
external: Some(External::ArrayString(vec![])),
treeshake: Some(false),
resolve: None,
..Default::default()
})
.build();

Expand Down
1 change: 1 addition & 0 deletions packages/rolldown/src/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export interface BindingInputOptions {
input: Array<BindingInputItem>
plugins: Array<BindingPluginOptions>
resolve?: BindingResolveOptions
platform?: 'node' | 'browser' | 'neutral'
cwd: string
}

Expand Down
1 change: 1 addition & 0 deletions packages/rolldown/src/options/input-options-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function createInputOptionsAdapter(
cwd: inputOptions.cwd ?? process.cwd(),
external: inputOptions.external ? options.external : undefined,
resolve: options.resolve,
platform: options.platform,
}
}

Expand Down
5 changes: 4 additions & 1 deletion packages/rolldown/src/options/input-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
InputOptions as RollupInputOptions,
} from '../rollup-types'
import { ensureArray, normalizePluginOption } from '../utils'
import { BindingResolveOptions } from '../binding'
import { BindingInputOptions, BindingResolveOptions } from '../binding'
import { Plugin } from '../plugin'

// TODO export compat plugin type
Expand All @@ -13,6 +13,7 @@ export interface InputOptions {
external?: RollupInputOptions['external']
resolve?: RolldownResolveOptions
cwd?: string
platform?: BindingInputOptions['platform']
}

export type RolldownResolveOptions = Omit<BindingResolveOptions, 'alias'> & {
Expand All @@ -21,6 +22,7 @@ export type RolldownResolveOptions = Omit<BindingResolveOptions, 'alias'> & {

export type RolldownNormalizedInputOptions = NormalizedInputOptions & {
resolve?: BindingResolveOptions
platform?: BindingInputOptions['platform']
}

export async function normalizeInputOptions(
Expand All @@ -32,6 +34,7 @@ export async function normalizeInputOptions(
plugins: await normalizePluginOption(config.plugins),
external: getIdMatcher(config.external),
resolve: getResolve(config.resolve),
platform: config.platform,
}
}

Expand Down

1 comment on commit 97ea4b6

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 97ea4b6 Previous: 5e25f31 Ratio
threejs10x 380.34 ms / ops 360.40 ms / ops 1.06
threejs10x-sourcemap 430.97 ms / ops 459.35 ms / ops 0.94

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.