Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ impl From<BindingHookJsResolveIdOptions> for Arc<CustomField> {
#[napi(object)]
pub struct BindingHookJsResolveIdOutput {
pub id: String,
#[napi(ts_type = "boolean | 'absolute' | 'relative'")]
pub external: Option<BindingResolvedExternal>,
#[napi(ts_type = "boolean | 'no-treeshake'")]
pub side_effects: BindingJsSideEffects,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,6 @@ impl From<PluginContext> for BindingPluginContext {
#[napi(object)]
pub struct BindingPluginContextResolvedId {
pub id: String,
#[napi(ts_type = "boolean | 'absolute' | 'relative'")]
pub external: BindingResolvedExternal,
}
6 changes: 4 additions & 2 deletions crates/rolldown_binding/src/options/plugin/js_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ impl Plugin for JsPlugin {
)
.instrument(debug_span!("resolve_id_hook", plugin_name = self.name))
.await?
.map(Into::into),
.map(TryInto::try_into)
.transpose()?,
)
}

Expand All @@ -146,7 +147,8 @@ impl Plugin for JsPlugin {
)
.instrument(debug_span!("resolve_dynamic_import_hook", plugin_name = self.name))
.await?
.map(Into::into),
.map(TryInto::try_into)
.transpose()?,
),
_ => Ok(None),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ pub struct BindingHookResolveIdOutput {
pub side_effects: Option<BindingHookSideEffects>,
}

impl From<BindingHookResolveIdOutput> for rolldown_plugin::HookResolveIdOutput {
fn from(value: BindingHookResolveIdOutput) -> Self {
Self {
impl TryFrom<BindingHookResolveIdOutput> for rolldown_plugin::HookResolveIdOutput {
type Error = napi::Error;

fn try_from(value: BindingHookResolveIdOutput) -> Result<Self, Self::Error> {
Ok(Self {
id: value.id.into(),
external: value.external.map(Into::into),
external: value.external.map(TryInto::try_into).transpose()?,
normalize_external_id: value.normalize_external_id,
side_effects: value.side_effects.map(Into::into),
}
})
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
use napi::Either;
use rolldown_common::ResolvedExternal;

#[napi_derive::napi]
#[derive(Debug)]
pub enum BindingResolvedExternal {
Bool(bool),
Absolute,
Relative,
}
#[napi_derive::napi(transparent)]
pub struct BindingResolvedExternal(Either<bool, String>);

impl TryFrom<BindingResolvedExternal> for ResolvedExternal {
type Error = napi::Error;

impl From<BindingResolvedExternal> for ResolvedExternal {
fn from(value: BindingResolvedExternal) -> Self {
match value {
BindingResolvedExternal::Bool(b) => ResolvedExternal::Bool(b),
BindingResolvedExternal::Absolute => ResolvedExternal::Absolute,
BindingResolvedExternal::Relative => ResolvedExternal::Relative,
}
fn try_from(value: BindingResolvedExternal) -> Result<Self, Self::Error> {
Ok(match value.0 {
Either::A(b) => Self::Bool(b),
Either::B(s) => match s.as_str() {
"absolute" => Self::Absolute,
"relative" => Self::Relative,
_ => {
return Err(napi::Error::new(
napi::Status::InvalidArg,
format!("Invalid string option: {s}"),
));
}
},
})
}
}

impl From<ResolvedExternal> for BindingResolvedExternal {
fn from(value: ResolvedExternal) -> Self {
match value {
ResolvedExternal::Bool(b) => BindingResolvedExternal::Bool(b),
ResolvedExternal::Absolute => BindingResolvedExternal::Absolute,
ResolvedExternal::Relative => BindingResolvedExternal::Relative,
}
Self(match value {
ResolvedExternal::Bool(b) => Either::A(b),
ResolvedExternal::Absolute => Either::B("absolute".to_string()),
ResolvedExternal::Relative => Either::B("relative".to_string()),
})
}
}
8 changes: 3 additions & 5 deletions packages/rolldown/src/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ export interface BindingHookJsResolveIdOptions {

export interface BindingHookJsResolveIdOutput {
id: string
external?: BindingResolvedExternal
external?: boolean | 'absolute' | 'relative'
sideEffects: boolean | 'no-treeshake'
}

Expand Down Expand Up @@ -573,7 +573,7 @@ export interface BindingOutputOptions {

export interface BindingPluginContextResolvedId {
id: string
external: BindingResolvedExternal
external: boolean | 'absolute' | 'relative'
}

export interface BindingPluginContextResolveOptions {
Expand Down Expand Up @@ -671,9 +671,7 @@ export interface BindingReportPluginConfig {
}

export type BindingResolvedExternal =
| { type: 'Bool', field0: boolean }
| { type: 'Absolute' }
| { type: 'Relative' }
boolean | string

export interface BindingResolveOptions {
alias?: Array<AliasItem>
Expand Down
9 changes: 4 additions & 5 deletions packages/rolldown/src/plugin/bindingify-build-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { SYMBOL_FOR_RESOLVE_CALLER_THAT_SKIP_SELF } from '../constants/plugin-co
import { NormalizedInputOptionsImpl } from '../options/normalized-input-options';
import { bindingifySourcemap, ExistingRawSourceMap } from '../types/sourcemap';
import { normalizeErrors } from '../utils/error';
import { bindingResolvedExternal } from '../utils/resolved-external';
import { transformModuleInfo } from '../utils/transform-module-info';
import { bindingifySideEffects } from '../utils/transform-side-effects';
import {
Expand Down Expand Up @@ -141,7 +140,7 @@ export function bindingifyResolveId(
if (ret === false) {
return {
id: specifier,
external: bindingResolvedExternal(true),
external: true,
normalizeExternalId: true,
};
}
Expand All @@ -161,7 +160,7 @@ export function bindingifyResolveId(

return {
id: ret.id,
external: bindingResolvedExternal(ret.external),
external: ret.external,
normalizeExternalId: false,
sideEffects: bindingifySideEffects(exist.moduleSideEffects),
};
Expand Down Expand Up @@ -201,7 +200,7 @@ export function bindingifyResolveDynamicImport(
if (ret === false) {
return {
id: specifier,
external: bindingResolvedExternal(true),
external: true,
};
}
if (typeof ret === 'string') {
Expand All @@ -212,7 +211,7 @@ export function bindingifyResolveDynamicImport(

const result: BindingHookResolveIdOutput = {
id: ret.id,
external: bindingResolvedExternal(ret.external),
external: ret.external,
};

if (ret.moduleSideEffects !== null) {
Expand Down
9 changes: 6 additions & 3 deletions packages/rolldown/src/plugin/plugin-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import type { LogHandler, LogLevelOption } from '../types/misc';
import { ModuleInfo } from '../types/module-info';
import { PartialNull } from '../types/utils';
import { AssetSource, bindingAssetSource } from '../utils/asset-source';
import { unimplemented } from '../utils/misc';
import { transformResolvedExternal } from '../utils/resolved-external';
import { unimplemented, unreachable } from '../utils/misc';
import { bindingifySideEffects } from '../utils/transform-side-effects';
import type {
CustomPluginOptions,
Expand Down Expand Up @@ -177,7 +176,11 @@ export class PluginContextImpl extends MinimalPluginContextImpl {
const info = this.data.getModuleOption(res.id) || ({} as ModuleOptions);
return {
...res,
external: transformResolvedExternal(res.external),
external: res.external === 'relative'
? unreachable(
`The PluginContext resolve result external couldn't be 'relative'`,
)
: res.external,
...info,
};
}
Expand Down
34 changes: 0 additions & 34 deletions packages/rolldown/src/utils/resolved-external.ts

This file was deleted.

Loading