Skip to content

Commit

Permalink
feat: add PluginDriver resolve_id (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin authored Nov 9, 2023
1 parent 42cdd61 commit 1f0b4f2
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 25 deletions.
22 changes: 13 additions & 9 deletions crates/rolldown/src/bundler/module_loader/module_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,21 +201,25 @@ impl<'a, T: FileSystemExt + 'static + Default> ModuleLoader<'a, T> {
#[allow(clippy::collection_is_never_read)]
async fn resolve_entries(&mut self) -> BatchedResult<Vec<(Option<String>, ResolvedRequestInfo)>> {
let resolver = &self.resolver;
let plugin_driver = &self.plugin_driver;

let resolved_ids =
futures::future::join_all(self.input_options.input.iter().map(|input_item| async move {
let specifier = &input_item.import;
let resolve_id = resolve_id(resolver, specifier, None, false).await?;

let Some(info) = resolve_id else {
return Err(BuildError::unresolved_entry(specifier));
};
match resolve_id(resolver, plugin_driver, specifier, None, false).await {
Ok(r) => {
let Some(info) = r else {
return Err(BuildError::unresolved_entry(specifier));
};

if info.is_external {
return Err(BuildError::entry_cannot_be_external(info.path.as_str()));
}

if info.is_external {
return Err(BuildError::entry_cannot_be_external(info.path.as_str()));
Ok((input_item.name.clone(), info))
}
Err(e) => Err(e),
}

Ok((input_item.name.clone(), info))
}))
.await;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{
bundler::{
module::normal_module_builder::NormalModuleBuilder,
module_loader::NormalModuleTaskResult,
plugin_driver::SharedPluginDriver,
utils::{
ast_scope::AstScope,
ast_symbol::AstSymbol,
Expand Down Expand Up @@ -171,16 +172,17 @@ impl<'task, T: FileSystemExt + Default + 'static> NormalModuleTask<'task, T> {
#[allow(clippy::option_if_let_else)]
pub(crate) async fn resolve_id<F: FileSystemExt + Default>(
resolver: &Resolver<F>,
plugin_driver: &SharedPluginDriver,
importer: &ResourceId,
specifier: &str,
) -> Result<ResolvedRequestInfo, BuildError> {
) -> BatchedResult<ResolvedRequestInfo> {
// let is_marked_as_external = is_external(specifier, Some(importer.id()), false).await?;

// if is_marked_as_external {
// return Ok(ModuleId::new(specifier, true));
// }

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

match resolved_id {
Some(info) => Ok(info),
Expand Down Expand Up @@ -208,11 +210,12 @@ impl<'task, T: FileSystemExt + Default + 'static> NormalModuleTask<'task, T> {
let specifier = item.module_request.clone();
// FIXME(hyf0): should not use `Arc<Resolver>` here
let resolver = Arc::clone(self.ctx.resolver);
let plugin_driver = Arc::clone(self.ctx.plugin_driver);
let importer = self.path.clone();
// let is_external = self.is_external.clone();
// let on_warn = self.input_options.on_warn.clone();
tokio::spawn(async move {
Self::resolve_id(&resolver, &importer, &specifier).await.map(|id| (idx, id))
Self::resolve_id(&resolver, &plugin_driver, &importer, &specifier).await.map(|id| (idx, id))
})
});

Expand Down
2 changes: 1 addition & 1 deletion crates/rolldown/src/bundler/plugin_driver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl PluginDriver {
Self { plugins }
}

pub async fn _resolve_id(&self, args: &HookResolveIdArgs<'_>) -> HookResolveIdReturn {
pub async fn resolve_id(&self, args: &HookResolveIdArgs<'_>) -> HookResolveIdReturn {
for plugin in &self.plugins {
if let Some(r) = plugin.resolve_id(&mut PluginContext::new(), args).await? {
return Ok(Some(r));
Expand Down
18 changes: 17 additions & 1 deletion crates/rolldown/src/bundler/utils/resolve_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use rolldown_error::BuildError;
use rolldown_fs::FileSystemExt;
use rolldown_resolver::Resolver;

use crate::{bundler::plugin_driver::SharedPluginDriver, HookResolveIdArgs};

pub struct ResolvedRequestInfo {
pub path: RawPath,
pub module_type: ModuleType,
Expand All @@ -12,11 +14,25 @@ pub struct ResolvedRequestInfo {
#[allow(clippy::unused_async)]
pub async fn resolve_id<T: FileSystemExt + Default>(
resolver: &Resolver<T>,
plugin_driver: &SharedPluginDriver,
request: &str,
importer: Option<&ResourceId>,
_preserve_symlinks: bool,
) -> Result<Option<ResolvedRequestInfo>, BuildError> {
// TODO: resolve with plugins
// Run plugin resolve_id first, if it is None use internal resolver as fallback
if let Some(r) = plugin_driver
.resolve_id(&HookResolveIdArgs {
importer: importer.map(std::convert::AsRef::as_ref),
source: request,
})
.await?
{
return Ok(Some(ResolvedRequestInfo {
path: r.id.into(),
module_type: ModuleType::Unknown,
is_external: matches!(r.external, Some(true)),
}));
}

// external modules (non-entry modules that start with neither '.' or '/')
// are skipped at this stage.
Expand Down
4 changes: 1 addition & 3 deletions crates/rolldown/src/plugin/args.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use rolldown_common::RawPath;

#[derive(Debug)]
pub struct HookResolveIdArgs<'a> {
pub importer: Option<&'a RawPath>,
pub importer: Option<&'a str>,
pub source: &'a str,
}

Expand Down
8 changes: 4 additions & 4 deletions crates/rolldown/src/plugin/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use std::{borrow::Cow, fmt::Debug};

use crate::error::BatchedResult;
use rolldown_error::BuildError;

use super::{
args::{HookLoadArgs, HookResolveIdArgs, HookTransformArgs},
context::PluginContext,
output::{HookLoadOutput, HookResolveIdOutput},
};

pub type HookResolveIdReturn = BatchedResult<Option<HookResolveIdOutput>>;
pub type HookTransformReturn = BatchedResult<Option<HookLoadOutput>>;
pub type HookLoadReturn = BatchedResult<Option<HookLoadOutput>>;
pub type HookResolveIdReturn = Result<Option<HookResolveIdOutput>, BuildError>;
pub type HookTransformReturn = Result<Option<HookLoadOutput>, BuildError>;
pub type HookLoadReturn = Result<Option<HookLoadOutput>, BuildError>;

#[async_trait::async_trait]
pub trait Plugin: Debug + Send + Sync {
Expand Down
4 changes: 4 additions & 0 deletions packages/rollup-tests/src/failed-tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@
"rollup@function@implicit-dependencies@dependant-not-part-of-graph: throws when a module that is loaded before an emitted chunk is not part of the module graph",
"rollup@function@implicit-dependencies@external-dependant: throws when a module that is loaded before an emitted chunk does not exist",
"rollup@function@implicit-dependencies@missing-dependant: throws when a module that is loaded before an emitted chunk is external",
"rollup@function@import-assertions@plugin-assertions-this-resolve: allows plugins to provide attributes for this.resolve",
"rollup@function@import-assertions@warn-assertion-conflicts: warns for conflicting import assertions",
"rollup@function@import-assertions@warn-assertion-conflicts: warns for conflicting import attributes",
"rollup@function@import-assertions@warn-unresolvable-assertions: warns for dynamic import attributes that cannot be resolved",
Expand Down Expand Up @@ -576,6 +577,7 @@
"rollup@function@plugins-can-manipulate-options: plugins can manipulate the options object",
"rollup@function@preload-cyclic-module: handles pre-loading a cyclic module in the resolveId hook",
"rollup@function@preload-loading-module: waits for pre-loaded modules that are currently loading",
"rollup@function@preload-module: allows pre-loading modules via this.load",
"rollup@function@preserve-modules-circular-order: preserves execution order for circular dependencies when preserving modules",
"rollup@function@preserve-modules@inline-dynamic-imports: Inlining dynamic imports is not supported when preserving modules",
"rollup@function@preserve-modules@invalid-default-export-mode: throws when using default export mode with named exports",
Expand Down Expand Up @@ -617,12 +619,14 @@
"rollup@function@resolve-relative-external-id: resolves relative external ids",
"rollup@function@resolve-unordered-assignments-2: Assignments should be correctly bound independent of their order",
"rollup@function@resolve-unordered-assignments: Assignments should be correctly bound independent of their order",
"rollup@function@resolveid-is-entry: sends correct isEntry information to resolveId hooks",
"rollup@function@resolves-namespace-default: namespace's 'default' properties should be available",
"rollup@function@respect-default-export-reexporter-side-effects: respect side-effects in reexporting modules even if moduleSideEffects are off",
"rollup@function@respect-reexporter-side-effects: respect side-effects in reexporting modules even if moduleSideEffects are off",
"rollup@function@respect-synthetic-export-reexporter-side-effects: respect side-effects in reexporting modules even if moduleSideEffects are off",
"rollup@function@retains-sort-order-b: sorts statements according to their original order within modules, part 2",
"rollup@function@retains-sort-order: sorts statements according to their original order within modules",
"rollup@function@reuse-resolve-meta: does not modify meta objects passed in resolveId",
"rollup@function@rewrite-member-expressions: rewrites identifiers at the head of member expressions",
"rollup@function@self-referencing-namespace: supports dynamic namespaces that reference themselves",
"rollup@function@shadowed-external-export: external modules are not shadowed",
Expand Down
4 changes: 2 additions & 2 deletions packages/rollup-tests/src/status.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"total": 901,
"failed": 0,
"skipFailed": 648,
"skipFailed": 652,
"ignored": 7,
"skipped": 0,
"passed": 246
"passed": 242
}
4 changes: 2 additions & 2 deletions packages/rollup-tests/src/status.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
|----| ---- |
| total | 901|
| failed | 0|
| skipFailed | 648|
| skipFailed | 652|
| ignored | 7|
| skipped | 0|
| passed | 246|
| passed | 242|

0 comments on commit 1f0b4f2

Please sign in to comment.