Skip to content

Commit

Permalink
feat(rust): store package.json in NormalModule (#1170)
Browse files Browse the repository at this point in the history
<!-- Thank you for contributing! -->

### Description

<!-- Please insert your description here and provide especially info about the "what" this PR is solving -->
  • Loading branch information
hyf0 authored May 19, 2024
1 parent 69e4ad6 commit e9938d9
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions crates/rolldown/src/module_loader/module_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl ModuleLoader {

fn try_spawn_new_task(
&mut self,
info: &ResolvedRequestInfo,
info: ResolvedRequestInfo,
is_user_defined_entry: bool,
) -> ModuleId {
match self.visited.entry(Arc::<str>::clone(&info.path.path)) {
Expand All @@ -145,6 +145,7 @@ impl ModuleLoader {
module_path,
info.module_type,
is_user_defined_entry,
info.package_json,
);
#[cfg(target_family = "wasm")]
{
Expand Down Expand Up @@ -190,7 +191,7 @@ impl ModuleLoader {
.into_iter()
.map(|(name, info)| EntryPoint {
name,
id: self.try_spawn_new_task(&info, true).expect_normal(),
id: self.try_spawn_new_task(info, true).expect_normal(),
kind: EntryPointKind::UserDefined,
})
.inspect(|e| {
Expand Down Expand Up @@ -222,7 +223,7 @@ impl ModuleLoader {
.into_iter()
.zip(resolved_deps)
.map(|(raw_rec, info)| {
let id = self.try_spawn_new_task(&info, false);
let id = self.try_spawn_new_task(info, false);
// Dynamic imported module will be considered as an entry
if let ModuleId::Normal(id) = id {
self.intermediate_normal_modules.importers[id].push(ImporterRecord {
Expand Down
8 changes: 7 additions & 1 deletion crates/rolldown/src/module_loader/normal_module_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use futures::future::join_all;
use oxc::span::SourceType;
use oxc_index::IndexVec;
use rolldown_common::{
AstScope, ImportRecordId, ModuleType, NormalModule, NormalModuleId, RawImportRecord,
AstScope, ImportRecordId, ModuleType, NormalModule, NormalModuleId, PackageJson, RawImportRecord,
ResolvedPath, ResolvedRequestInfo, ResourceId, SymbolRef,
};
use rolldown_error::BuildError;
Expand All @@ -26,6 +26,7 @@ pub struct NormalModuleTask {
ctx: Arc<TaskContext>,
module_id: NormalModuleId,
resolved_path: ResolvedPath,
package_json: Option<PackageJson>,
module_type: ModuleType,
errors: Vec<BuildError>,
is_user_defined_entry: bool,
Expand All @@ -38,6 +39,7 @@ impl NormalModuleTask {
path: ResolvedPath,
module_type: ModuleType,
is_user_defined_entry: bool,
package_json: Option<PackageJson>,
) -> Self {
Self {
ctx,
Expand All @@ -46,6 +48,7 @@ impl NormalModuleTask {
module_type,
errors: vec![],
is_user_defined_entry,
package_json,
}
}

Expand Down Expand Up @@ -136,6 +139,7 @@ impl NormalModuleTask {
dynamic_importers: vec![],
imported_ids,
dynamically_imported_ids,
package_json: self.package_json.take(),
};

self.ctx.plugin_driver.module_parsed(Arc::new(module.to_module_info())).await?;
Expand Down Expand Up @@ -225,6 +229,7 @@ impl NormalModuleTask {
path: specifier.to_string().into(),
module_type: ModuleType::Unknown,
is_external: true,
package_json: None,
}));
}
}
Expand Down Expand Up @@ -298,6 +303,7 @@ impl NormalModuleTask {
path: specifier.to_string().into(),
module_type: ModuleType::Unknown,
is_external: true,
package_json: None,
});
}
_ => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ impl RuntimeNormalModuleTask {
dynamic_importers: vec![],
imported_ids: vec![],
dynamically_imported_ids: vec![],
package_json: None,
};

if let Err(_err) =
Expand Down
1 change: 1 addition & 0 deletions crates/rolldown_common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ rolldown_utils = { workspace = true }
rustc-hash = { workspace = true }
schemars = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"], optional = true }
serde_json = { workspace = true }
sugar_path = { workspace = true }

[features]
Expand Down
1 change: 1 addition & 0 deletions crates/rolldown_common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub use crate::{
types::normal_module_id::NormalModuleId,
types::output::{Output, OutputAsset},
types::output_chunk::OutputChunk,
types::package_json::PackageJson,
types::rendered_chunk::RenderedChunk,
types::rendered_module::RenderedModule,
types::resolved_export::ResolvedExport,
Expand Down
3 changes: 2 additions & 1 deletion crates/rolldown_common/src/module/normal_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{fmt::Debug, sync::Arc};
use crate::{
types::ast_scope::AstScope, DebugStmtInfoForTreeShaking, ExportsKind, ImportRecord,
ImportRecordId, LocalExport, ModuleId, ModuleInfo, ModuleType, NamedImport, NormalModuleId,
ResourceId, StmtInfo, StmtInfos, SymbolRef,
PackageJson, ResourceId, StmtInfo, StmtInfos, SymbolRef,
};
use oxc::span::Span;
use oxc_index::IndexVec;
Expand Down Expand Up @@ -47,6 +47,7 @@ pub struct NormalModule {
pub imported_ids: Vec<ResourceId>,
// the module ids imported by this module via dynamic import()
pub dynamically_imported_ids: Vec<ResourceId>,
pub package_json: Option<PackageJson>,
}

impl NormalModule {
Expand Down
1 change: 1 addition & 0 deletions crates/rolldown_common/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod named_import;
pub mod normal_module_id;
pub mod output;
pub mod output_chunk;
pub mod package_json;
pub mod rendered_chunk;
pub mod rendered_module;
pub mod resolved_export;
Expand Down
17 changes: 17 additions & 0 deletions crates/rolldown_common/src/types/package_json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use std::sync::Arc;

#[derive(Debug)]
pub struct PackageJson {
raw: Arc<serde_json::Value>,
}

impl PackageJson {
pub fn new(raw: impl Into<Arc<serde_json::Value>>) -> Self {
let raw = raw.into();
Self { raw }
}

pub fn r#type(&self) -> Option<&str> {
self.raw.get("type").and_then(|v| v.as_str())
}
}
3 changes: 2 additions & 1 deletion crates/rolldown_common/src/types/resolved_request_info.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::{ModuleType, ResolvedPath};
use crate::{ModuleType, PackageJson, ResolvedPath};

#[derive(Debug)]
pub struct ResolvedRequestInfo {
pub path: ResolvedPath,
pub module_type: ModuleType,
pub is_external: bool,
pub package_json: Option<PackageJson>,
}
4 changes: 4 additions & 0 deletions crates/rolldown_plugin/src/utils/resolve_id_with_plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub async fn resolve_id_with_plugins(
module_type: ModuleType::from_path(&r.id),
path: r.id.into(),
is_external: matches!(r.external, Some(true)),
package_json: None,
}));
}
}
Expand All @@ -51,6 +52,7 @@ pub async fn resolve_id_with_plugins(
module_type: ModuleType::from_path(&r.id),
path: r.id.into(),
is_external: matches!(r.external, Some(true)),
package_json: None,
}));
}

Expand All @@ -60,6 +62,7 @@ pub async fn resolve_id_with_plugins(
path: request.to_string().into(),
module_type: ModuleType::Unknown,
is_external: true,
package_json: None,
}));
}

Expand All @@ -71,5 +74,6 @@ pub async fn resolve_id_with_plugins(
path: resolved.path,
module_type: resolved.module_type,
is_external: false,
package_json: resolved.package_json,
}))
}
22 changes: 18 additions & 4 deletions crates/rolldown_resolver/src/resolver.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use itertools::Itertools;
use rolldown_common::{ImportKind, ModuleType, Platform, ResolveOptions, ResolvedPath};
use rolldown_common::{
ImportKind, ModuleType, PackageJson, Platform, ResolveOptions, ResolvedPath,
};
use rolldown_fs::{FileSystem, OsFileSystem};
use std::path::{Path, PathBuf};
use std::{
path::{Path, PathBuf},
sync::Arc,
};
use sugar_path::SugarPath;

use oxc_resolver::{
Expand Down Expand Up @@ -121,6 +126,7 @@ impl<F: FileSystem + Default> Resolver<F> {
pub struct ResolveReturn {
pub path: ResolvedPath,
pub module_type: ModuleType,
pub package_json: Option<PackageJson>,
}

impl<F: FileSystem + Default> Resolver<F> {
Expand Down Expand Up @@ -162,18 +168,21 @@ impl<F: FileSystem + Default> Resolver<F> {

match resolution {
Ok(info) => {
let package_json = info.package_json().map(|p| PackageJson::new(Arc::clone(p.raw_json())));
let module_type = calc_module_type(&info);
Ok(Ok(build_resolve_ret(
info.full_path().to_str().expect("Should be valid utf8").to_string(),
false,
module_type,
package_json,
)))
}
Err(err) => match err {
ResolveError::Ignored(p) => Ok(Ok(build_resolve_ret(
p.to_str().expect("Should be valid utf8").to_string(),
true,
ModuleType::Unknown,
None,
))),
_ => Ok(Err(err)),
},
Expand All @@ -200,6 +209,11 @@ fn calc_module_type(info: &Resolution) -> ModuleType {
ModuleType::Unknown
}

fn build_resolve_ret(path: String, ignored: bool, module_type: ModuleType) -> ResolveReturn {
ResolveReturn { path: ResolvedPath { path: path.into(), ignored }, module_type }
fn build_resolve_ret(
path: String,
ignored: bool,
module_type: ModuleType,
package_json: Option<PackageJson>,
) -> ResolveReturn {
ResolveReturn { path: ResolvedPath { path: path.into(), ignored }, module_type, package_json }
}

0 comments on commit e9938d9

Please sign in to comment.