Skip to content

Commit

Permalink
refactor(rust/plugin): merge fields of HookResolveIdExtraOptions to…
Browse files Browse the repository at this point in the history
… `HookResolveIdArgs` (rolldown#1872)

<!-- Thank you for contributing! -->

### Description

- We only need a extra struct for rollup compat, no need to do it in the
pure rust side.

<!-- Please insert your description here and provide especially info
about the "what" this PR is solving -->
  • Loading branch information
hyf0 authored Aug 6, 2024
1 parent ad78e63 commit 0d5d1d4
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 78 deletions.
14 changes: 4 additions & 10 deletions crates/rolldown/src/stages/scan_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use futures::future::join_all;
use rolldown_common::{EntryPoint, ImportKind, ModuleTable, ResolvedId};
use rolldown_error::{BuildDiagnostic, DiagnosableResult};
use rolldown_fs::OsFileSystem;
use rolldown_plugin::{HookResolveIdExtraOptions, SharedPluginDriver};
use rolldown_plugin::SharedPluginDriver;
use rolldown_resolver::ResolveError;

use crate::{
Expand Down Expand Up @@ -106,15 +106,9 @@ impl ScanStage {
specifier: &'a str,
}
let args = Args { specifier: &input_item.import };
let resolved = resolve_id(
resolver,
plugin_driver,
args.specifier,
None,
HookResolveIdExtraOptions { is_entry: true, kind: ImportKind::Import },
None,
)
.await;
let resolved =
resolve_id(resolver, plugin_driver, args.specifier, None, true, ImportKind::Import, None)
.await;

resolved
.map(|info| (args, info.map(|info| ((input_item.name.clone().map(ArcStr::from)), info))))
Expand Down
23 changes: 8 additions & 15 deletions crates/rolldown/src/types/module_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use std::sync::Arc;
use futures::future::join_all;
use oxc::index::IndexVec;
use rolldown_common::{
side_effects::HookSideEffects, ImportRecordIdx, Module, ModuleDefFormat, ModuleIdx, ModuleType,
RawImportRecord, ResolvedId, StrOrBytes,
side_effects::HookSideEffects, ImportKind, ImportRecordIdx, Module, ModuleDefFormat, ModuleIdx,
ModuleType, RawImportRecord, ResolvedId, StrOrBytes,
};
use rolldown_ecmascript::EcmaAst;
use rolldown_error::{BuildDiagnostic, DiagnosableResult};
use rolldown_plugin::{HookResolveIdExtraOptions, SharedPluginDriver};
use rolldown_plugin::SharedPluginDriver;
use rolldown_resolver::ResolveError;
use rolldown_sourcemap::SourceMap;

Expand All @@ -34,7 +34,7 @@ impl<'a> CreateModuleContext<'a> {
plugin_driver: &SharedPluginDriver,
importer: &str,
specifier: &str,
options: HookResolveIdExtraOptions,
kind: ImportKind,
) -> anyhow::Result<Result<ResolvedId, ResolveError>> {
// Check external with unresolved path
if let Some(is_external) = bundle_options.external.as_ref() {
Expand Down Expand Up @@ -63,7 +63,7 @@ impl<'a> CreateModuleContext<'a> {
}

let resolved_id =
resolve_id::resolve_id(resolver, plugin_driver, specifier, Some(importer), options, None)
resolve_id::resolve_id(resolver, plugin_driver, specifier, Some(importer), false, kind, None)
.await?;

match resolved_id {
Expand Down Expand Up @@ -93,16 +93,9 @@ impl<'a> CreateModuleContext<'a> {
let importer = &self.resolved_id.id;
let kind = item.kind;
async move {
Self::resolve_id(
&bundle_options,
&resolver,
&plugin_driver,
importer,
&specifier,
HookResolveIdExtraOptions { is_entry: false, kind },
)
.await
.map(|id| (specifier, idx, id))
Self::resolve_id(&bundle_options, &resolver, &plugin_driver, importer, &specifier, kind)
.await
.map(|id| (specifier, idx, id))
}
});

Expand Down
12 changes: 8 additions & 4 deletions crates/rolldown_binding/src/options/plugin/js_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ use crate::types::{
use rolldown_plugin::{Plugin, __inner::SharedPluginable};
use std::{borrow::Cow, ops::Deref, sync::Arc};

use super::{binding_transform_context::BindingTransformPluginContext, BindingPluginOptions};
use super::{
binding_transform_context::BindingTransformPluginContext,
types::binding_hook_resolve_id_extra_options::BindingHookResolveIdExtraOptions,
BindingPluginOptions,
};

#[derive(Debug)]
pub struct JsPlugin {
Expand Down Expand Up @@ -59,7 +63,7 @@ impl Plugin for JsPlugin {
Arc::clone(ctx).into(),
args.specifier.to_string(),
args.importer.map(str::to_string),
args.options.clone().into(),
BindingHookResolveIdExtraOptions { is_entry: args.is_entry, kind: args.kind.to_string() },
))
.await?
.map(Into::into),
Expand All @@ -72,13 +76,13 @@ impl Plugin for JsPlugin {
async fn resolve_dynamic_import(
&self,
ctx: &rolldown_plugin::SharedPluginContext,
args: &rolldown_plugin::HookResolveDynamicImportArgs<'_>,
args: &rolldown_plugin::HookResolveIdArgs<'_>,
) -> rolldown_plugin::HookResolveIdReturn {
if let Some(cb) = &self.resolve_dynamic_import {
Ok(
cb.await_call((
Arc::clone(ctx).into(),
args.source.to_string(),
args.specifier.to_string(),
args.importer.map(str::to_string),
))
.await?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,3 @@ pub struct BindingHookResolveIdExtraOptions {
#[napi(ts_type = "'import' | 'dynamic-import' | 'require-call'")]
pub kind: String,
}

impl From<rolldown_plugin::HookResolveIdExtraOptions> for BindingHookResolveIdExtraOptions {
fn from(value: rolldown_plugin::HookResolveIdExtraOptions) -> Self {
Self { is_entry: value.is_entry, kind: value.kind.to_string() }
}
}
2 changes: 0 additions & 2 deletions crates/rolldown_plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ pub use crate::{
types::hook_render_chunk_args::HookRenderChunkArgs,
types::hook_render_chunk_output::HookRenderChunkOutput,
types::hook_render_error::HookRenderErrorArgs,
types::hook_resolve_dynamic_import_args::HookResolveDynamicImportArgs,
types::hook_resolve_id_args::HookResolveIdArgs,
types::hook_resolve_id_extra_options::HookResolveIdExtraOptions,
types::hook_resolve_id_output::HookResolveIdOutput,
types::hook_transform_args::HookTransformArgs,
types::hook_transform_ast_args::HookTransformAstArgs,
Expand Down
5 changes: 2 additions & 3 deletions crates/rolldown_plugin/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use crate::{
hook_transform_output::HookTransformOutput,
},
HookBuildEndArgs, HookInjectionArgs, HookLoadArgs, HookLoadOutput, HookRenderChunkArgs,
HookRenderChunkOutput, HookResolveDynamicImportArgs, HookResolveIdArgs, HookResolveIdOutput,
HookTransformArgs,
HookRenderChunkOutput, HookResolveIdArgs, HookResolveIdOutput, HookTransformArgs,
};
use anyhow::Result;
use rolldown_common::{ModuleInfo, Output, RollupRenderedChunk};
Expand Down Expand Up @@ -52,7 +51,7 @@ pub trait Plugin: Any + Debug + Send + Sync + 'static {
fn resolve_dynamic_import(
&self,
_ctx: &SharedPluginContext,
_args: &HookResolveDynamicImportArgs<'_>,
_args: &HookResolveIdArgs<'_>,
) -> impl std::future::Future<Output = HookResolveIdReturn> + Send {
async { Ok(None) }
}
Expand Down
5 changes: 3 additions & 2 deletions crates/rolldown_plugin/src/plugin_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
plugin_context_resolve_options::PluginContextResolveOptions, plugin_idx::PluginIdx,
},
utils::resolve_id_with_plugins::resolve_id_with_plugins,
HookResolveIdExtraOptions, PluginDriver,
PluginDriver,
};

pub type SharedPluginContext = std::sync::Arc<PluginContext>;
Expand Down Expand Up @@ -58,7 +58,8 @@ impl PluginContext {
&plugin_driver,
specifier,
importer,
HookResolveIdExtraOptions { is_entry: false, kind: normalized_extra_options.import_kind },
false,
normalized_extra_options.import_kind,
if normalized_extra_options.skip_self {
let mut skipped_resolve_calls = Vec::with_capacity(self.skipped_resolve_calls.len() + 1);
skipped_resolve_calls.extend(self.skipped_resolve_calls.clone());
Expand Down
9 changes: 4 additions & 5 deletions crates/rolldown_plugin/src/plugin_driver/build_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ use crate::{
hook_resolve_id_skipped::HookResolveIdSkipped, hook_transform_ast_args::HookTransformAstArgs,
plugin_idx::PluginIdx,
},
HookBuildEndArgs, HookLoadArgs, HookLoadReturn, HookNoopReturn, HookResolveDynamicImportArgs,
HookResolveIdArgs, HookResolveIdReturn, HookTransformArgs, PluginContext, PluginDriver,
TransformPluginContext,
HookBuildEndArgs, HookLoadArgs, HookLoadReturn, HookNoopReturn, HookResolveIdArgs,
HookResolveIdReturn, HookTransformArgs, PluginContext, PluginDriver, TransformPluginContext,
};
use anyhow::Result;
use rolldown_common::{side_effects::HookSideEffects, ModuleInfo};
Expand Down Expand Up @@ -101,11 +100,11 @@ impl PluginDriver {
// Only for rollup compatibility
pub async fn resolve_dynamic_import(
&self,
args: &HookResolveDynamicImportArgs<'_>,
args: &HookResolveIdArgs<'_>,
skipped_resolve_calls: Option<&Vec<Arc<HookResolveIdSkipped>>>,
) -> HookResolveIdReturn {
let skipped_plugins =
Self::get_resolve_call_skipped_plugins(args.source, args.importer, skipped_resolve_calls);
Self::get_resolve_call_skipped_plugins(args.specifier, args.importer, skipped_resolve_calls);
for (plugin_idx, plugin, ctx) in self.iter_enumerated_plugin_with_context() {
if skipped_plugins.iter().any(|p| *p == plugin_idx) {
continue;
Expand Down
6 changes: 3 additions & 3 deletions crates/rolldown_plugin/src/pluginable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
transform_plugin_context::TransformPluginContext,
types::{hook_render_error::HookRenderErrorArgs, hook_transform_ast_args::HookTransformAstArgs},
HookBuildEndArgs, HookInjectionArgs, HookInjectionOutputReturn, HookLoadArgs,
HookRenderChunkArgs, HookResolveDynamicImportArgs, HookResolveIdArgs, HookTransformArgs, Plugin,
HookRenderChunkArgs, HookResolveIdArgs, HookTransformArgs, Plugin,
};
use rolldown_common::{ModuleInfo, Output, RollupRenderedChunk};

Expand Down Expand Up @@ -47,7 +47,7 @@ pub trait Pluginable: Any + Debug + Send + Sync + 'static {
async fn call_resolve_dynamic_import(
&self,
_ctx: &SharedPluginContext,
_args: &HookResolveDynamicImportArgs,
_args: &HookResolveIdArgs,
) -> HookResolveIdReturn;

async fn call_load(&self, _ctx: &SharedPluginContext, _args: &HookLoadArgs) -> HookLoadReturn;
Expand Down Expand Up @@ -158,7 +158,7 @@ impl<T: Plugin> Pluginable for T {
async fn call_resolve_dynamic_import(
&self,
ctx: &SharedPluginContext,
args: &HookResolveDynamicImportArgs,
args: &HookResolveIdArgs,
) -> HookResolveIdReturn {
Plugin::resolve_dynamic_import(self, ctx, args).await
}
Expand Down

This file was deleted.

7 changes: 5 additions & 2 deletions crates/rolldown_plugin/src/types/hook_resolve_id_args.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use super::hook_resolve_id_extra_options::HookResolveIdExtraOptions;
use rolldown_common::ImportKind;

#[derive(Debug)]
pub struct HookResolveIdArgs<'a> {
pub importer: Option<&'a str>,
pub specifier: &'a str,
pub options: HookResolveIdExtraOptions,
pub is_entry: bool,
// Rollup doesn't have a `kind` field, but rolldown supports cjs, css by default. So we need this
// field to determine the import kind.
pub kind: ImportKind,
}

This file was deleted.

2 changes: 0 additions & 2 deletions crates/rolldown_plugin/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ pub mod hook_load_output;
pub mod hook_render_chunk_args;
pub mod hook_render_chunk_output;
pub mod hook_render_error;
pub mod hook_resolve_dynamic_import_args;
pub mod hook_resolve_id_args;
pub mod hook_resolve_id_extra_options;
pub mod hook_resolve_id_output;
pub mod hook_resolve_id_skipped;
pub mod hook_transform_args;
Expand Down
17 changes: 9 additions & 8 deletions crates/rolldown_plugin/src/utils/resolve_id_with_plugins.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
types::hook_resolve_id_skipped::HookResolveIdSkipped, HookResolveDynamicImportArgs,
HookResolveIdArgs, HookResolveIdExtraOptions, PluginDriver,
types::hook_resolve_id_skipped::HookResolveIdSkipped, HookResolveIdArgs, PluginDriver,
};
use rolldown_common::{ImportKind, ModuleDefFormat, ResolvedId};
use rolldown_resolver::{ResolveError, Resolver};
Expand All @@ -19,17 +18,18 @@ pub async fn resolve_id_with_plugins(
plugin_driver: &PluginDriver,
request: &str,
importer: Option<&str>,
options: HookResolveIdExtraOptions,
is_entry: bool,
import_kind: ImportKind,
skipped_resolve_calls: Option<Vec<Arc<HookResolveIdSkipped>>>,
) -> anyhow::Result<Result<ResolvedId, ResolveError>> {
let import_kind = options.kind;
if matches!(import_kind, ImportKind::DynamicImport) {
if let Some(r) = plugin_driver
.resolve_dynamic_import(
&HookResolveDynamicImportArgs {
&HookResolveIdArgs {
importer: importer.map(std::convert::AsRef::as_ref),
source: request,
options: &options,
specifier: request,
is_entry,
kind: import_kind,
},
skipped_resolve_calls.as_ref(),
)
Expand All @@ -51,7 +51,8 @@ pub async fn resolve_id_with_plugins(
&HookResolveIdArgs {
importer: importer.map(std::convert::AsRef::as_ref),
specifier: request,
options,
is_entry,
kind: import_kind,
},
skipped_resolve_calls.as_ref(),
)
Expand Down

0 comments on commit 0d5d1d4

Please sign in to comment.