Skip to content

Commit

Permalink
feat: call PluginDriver load to get source content (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin authored Nov 9, 2023
1 parent 526f896 commit 01b3563
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 45 deletions.
35 changes: 23 additions & 12 deletions crates/rolldown/src/bundler/module_loader/normal_module_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ use rolldown_resolver::Resolver;
use sugar_path::AsPath;

use super::{module_task_context::ModuleTaskContext, Msg};
use crate::bundler::{
module::normal_module_builder::NormalModuleBuilder,
module_loader::NormalModuleTaskResult,
utils::{
ast_scope::AstScope,
ast_symbol::AstSymbol,
resolve_id::{resolve_id, ResolvedRequestInfo},
use crate::{
bundler::{
module::normal_module_builder::NormalModuleBuilder,
module_loader::NormalModuleTaskResult,
utils::{
ast_scope::AstScope,
ast_symbol::AstSymbol,
resolve_id::{resolve_id, ResolvedRequestInfo},
},
visitors::scanner::{self, ScanResult},
},
visitors::scanner::{self, ScanResult},
error::BatchedResult,
HookLoadArgs,
};
pub struct NormalModuleTask<'task, T: FileSystemExt + Default> {
ctx: &'task ModuleTaskContext<'task, T>,
Expand Down Expand Up @@ -50,11 +54,18 @@ impl<'task, T: FileSystemExt + Default + 'static> NormalModuleTask<'task, T> {
}
}

pub async fn run(mut self) -> anyhow::Result<()> {
pub async fn run(mut self) -> BatchedResult<()> {
let mut builder = NormalModuleBuilder::default();
tracing::trace!("process {:?}", self.path);
// load
let source = self.ctx.fs.read_to_string(self.path.as_path())?;

// Run plugin load to get content first, if it is None using read fs as fallback.
let source = if let Some(r) =
self.ctx.plugin_driver.load(&HookLoadArgs { id: self.path.as_ref() }).await?
{
r.code
} else {
self.ctx.fs.read_to_string(self.path.as_path())?
};
// TODO: transform

let (ast, scope, scan_result, ast_symbol, namespace_symbol) = self.scan(source);
Expand Down Expand Up @@ -183,7 +194,7 @@ impl<'task, T: FileSystemExt + Default + 'static> NormalModuleTask<'task, T> {
async fn resolve_dependencies(
&mut self,
dependencies: &IndexVec<ImportRecordId, ImportRecord>,
) -> anyhow::Result<Vec<(ImportRecordId, ResolvedRequestInfo)>> {
) -> BatchedResult<Vec<(ImportRecordId, ResolvedRequestInfo)>> {
let jobs = dependencies.iter_enumerated().map(|(idx, item)| {
let specifier = item.module_request.clone();
// FIXME(hyf0): should not use `Arc<Resolver>` here
Expand Down
39 changes: 12 additions & 27 deletions crates/rolldown/src/bundler/plugin_driver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,36 @@ use crate::{
pub type SharedPluginDriver = Arc<PluginDriver>;

pub struct PluginDriver {
_plugins: Vec<BoxPlugin>,
plugins: Vec<BoxPlugin>,
}

impl PluginDriver {
pub fn new(plugins: Vec<BoxPlugin>) -> Self {
Self { _plugins: plugins }
Self { plugins }
}

pub async fn _resolve_id(&self, args: &HookResolveIdArgs<'_>) -> HookResolveIdReturn {
for plugin in &self._plugins {
match plugin.resolve_id(&mut PluginContext::new(), args).await {
Err(e) => return Err(e),
Ok(r) => {
if let Some(r) = r {
return Ok(Some(r));
}
}
for plugin in &self.plugins {
if let Some(r) = plugin.resolve_id(&mut PluginContext::new(), args).await? {
return Ok(Some(r));
}
}
Ok(None)
}

pub async fn _load(&self, args: &HookLoadArgs<'_>) -> HookLoadReturn {
for plugin in &self._plugins {
match plugin.load(&mut PluginContext::new(), args).await {
Err(e) => return Err(e),
Ok(r) => {
if let Some(r) = r {
return Ok(Some(r));
}
}
pub async fn load(&self, args: &HookLoadArgs<'_>) -> HookLoadReturn {
for plugin in &self.plugins {
if let Some(r) = plugin.load(&mut PluginContext::new(), args).await? {
return Ok(Some(r));
}
}
Ok(None)
}

pub async fn _transform(&self, args: &HookTransformArgs<'_>) -> HookTransformReturn {
for plugin in &self._plugins {
match plugin.transform(&mut PluginContext::new(), args).await {
Err(e) => return Err(e),
Ok(r) => {
if let Some(r) = r {
return Ok(Some(r));
}
}
for plugin in &self.plugins {
if let Some(r) = plugin.transform(&mut PluginContext::new(), args).await? {
return Ok(Some(r));
}
}
Ok(None)
Expand Down
6 changes: 6 additions & 0 deletions crates/rolldown/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ impl From<BuildError> for BatchedErrors {
}
}

impl From<std::io::Error> for BatchedErrors {
fn from(err: std::io::Error) -> Self {
Self::with_error(err.into())
}
}

impl From<BatchedErrors> for Vec<BuildError> {
fn from(errs: BatchedErrors) -> Self {
errs.0.into_vec()
Expand Down
1 change: 1 addition & 0 deletions crates/rolldown_error/src/error/impl_to_diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ impl BuildError {
Self::UnresolvedImport(err) => {
Diagnostic { code, summary: err.to_string(), ..Default::default() }
}
Self::Io(err) => Diagnostic { code, summary: err.to_string(), ..Default::default() },
Self::Napi { status, reason } => Diagnostic {
code,
summary: format!("Napi error: {status}: {reason}"),
Expand Down
9 changes: 9 additions & 0 deletions crates/rolldown_error/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ pub enum BuildError {
ExternalEntry(Box<ExternalEntry>),
#[error(transparent)]
UnresolvedImport(Box<UnresolvedImport>),
#[error(transparent)]
Io(Box<std::io::Error>),
// TODO: probably should remove this error
#[error("Napi error: {status}: {reason}")]
Napi { status: String, reason: String },
Expand All @@ -37,6 +39,7 @@ impl BuildError {
match self {
Self::UnresolvedEntry(_) | Self::ExternalEntry(_) => error_code::UNRESOLVED_ENTRY,
Self::UnresolvedImport(_) => error_code::UNRESOLVED_IMPORT,
Self::Io(_) => error_code::IO_ERROR,
Self::Napi { .. } => todo!(),
}
}
Expand All @@ -63,3 +66,9 @@ impl BuildError {
Self::Napi { status, reason }
}
}

impl From<std::io::Error> for BuildError {
fn from(e: std::io::Error) -> Self {
Self::Io(Box::new(e))
}
}
2 changes: 1 addition & 1 deletion crates/rolldown_error/src/error_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ pub const UNRESOLVED_IMPORT: &str = "UNRESOLVED_IMPORT";
// // Rolldown error codes

// pub const PANIC: &str = "PANIC";
// pub const IO_ERROR: &str = "IO_ERROR";
pub const IO_ERROR: &str = "IO_ERROR";
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct JsAdapterPlugin {
impl JsAdapterPlugin {
pub fn new(option: PluginOptions) -> napi::Result<Self> {
let resolve_id_fn = option.resolve_id.as_ref().map(ResolveIdCallback::new).transpose()?;
let load_fn = option.resolve_id.as_ref().map(LoadCallback::new).transpose()?;
let load_fn = option.load.as_ref().map(LoadCallback::new).transpose()?;
let transform_fn = option.transform.as_ref().map(TransformCallback::new).transpose()?;
Ok(Self { name: option.name, resolve_id_fn, load_fn, transform_fn })
}
Expand Down
3 changes: 3 additions & 0 deletions packages/rollup-tests/src/failed-tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@
"rollup@function@missing-export-preserve-modules: supports shimming missing exports when preserving modules",
"rollup@function@missing-spaces-after-simplification: handle situations where the simplification of an expression can lead to issues due to missing white-space",
"rollup@function@mixed-external-paths: allows using the path option selectively",
"rollup@function@modify-meta: allows to freely modify moduleInfo.meta and maintain object identity",
"rollup@function@modify-this-via-getter@deoptimized-props-with-getter: handles fully deoptimized objects",
"rollup@function@modify-this-via-getter@unknown-prop-getter: handles unknown getters that modify \"this\"",
"rollup@function@modify-this-via-getter@unknown-prop-unknown-access: handles unknown getters that modify \"this\" for unknown property access",
Expand Down Expand Up @@ -541,6 +542,7 @@
"rollup@function@pass-namespace-to-function: allows a namespace to be passed to a function",
"rollup@function@paths-are-case-sensitive: insists on correct casing for imports",
"rollup@function@per-output-plugins-warn-hooks: warns when input hooks are used in output plugins",
"rollup@function@perf-adds-plugin-context-to-plugins: Adds plugin context to plugins with perf=true",
"rollup@function@perf-supports-object-hooks: Supports object hooks with perf=true",
"rollup@function@plugin-cache@anonymous-delete: throws for anonymous plugins deleting from the cache",
"rollup@function@plugin-cache@anonymous-get: throws for anonymous plugins reading the cache",
Expand Down Expand Up @@ -572,6 +574,7 @@
"rollup@function@plugin-warn: plugin transform hooks can use `this.warn({...}, char)` (#1140)",
"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@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
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": 644,
"skipFailed": 647,
"ignored": 7,
"skipped": 0,
"passed": 250
"passed": 247
}
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 | 644|
| skipFailed | 647|
| ignored | 7|
| skipped | 0|
| passed | 250|
| passed | 247|

0 comments on commit 01b3563

Please sign in to comment.