Skip to content

Commit

Permalink
support multiple package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
arnauorriols committed Jul 19, 2024
1 parent cbeb427 commit 682409a
Showing 1 changed file with 75 additions and 79 deletions.
154 changes: 75 additions & 79 deletions src/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,14 +324,14 @@ impl FromGraphNpmPackages {
pub fn add_package_with_maybe_meta(
&mut self,
package_id: PackageNv,
package_json: Option<FromGraphNpmModule>,
package_jsons: Option<Vec<FromGraphNpmModule>>,
meta_modules: Option<Vec<FromGraphNpmModule>>,
modules: IndexMap<NpmPackageNvReference, FromGraphNpmModule>,
) {
self.packages.insert(
package_id,
FromGraphNpmPackage {
package_json,
package_jsons,
meta_modules: meta_modules
.or(FromGraphNpmPackage::default().meta_modules),
modules,
Expand All @@ -342,18 +342,23 @@ impl FromGraphNpmPackages {
pub fn add_package<N, S>(
&mut self,
package_id: PackageNv,
package_json: (N, S),
package_jsons: impl IntoIterator<Item = (N, S)>,
modules: impl IntoIterator<Item = (NpmPackageNvReference, (N, S))>,
) where
S: Into<Vec<u8>>,
N: Into<String>,
{
self.add_package_with_maybe_meta(
package_id,
Some(FromGraphNpmModule {
specifier: package_json.0.into(),
source: package_json.1.into(),
}),
Some(
package_jsons
.into_iter()
.map(|package_json| FromGraphNpmModule {
specifier: package_json.0.into(),
source: package_json.1.into(),
})
.collect(),
),
None,
modules
.into_iter()
Expand All @@ -373,7 +378,7 @@ impl FromGraphNpmPackages {
pub fn add_package_with_meta<N, S>(
&mut self,
package_id: PackageNv,
package_json: (N, S),
package_jsons: impl IntoIterator<Item = (N, S)>,
meta_modules: impl IntoIterator<Item = (N, S)>,
modules: impl IntoIterator<Item = (NpmPackageNvReference, (N, S))>,
) where
Expand All @@ -382,10 +387,15 @@ impl FromGraphNpmPackages {
{
self.add_package_with_maybe_meta(
package_id,
Some(FromGraphNpmModule {
specifier: package_json.0.into(),
source: package_json.1.into(),
}),
Some(
package_jsons
.into_iter()
.map(|package_json| FromGraphNpmModule {
specifier: package_json.0.into(),
source: package_json.1.into(),
})
.collect(),
),
Some(
meta_modules
.into_iter()
Expand Down Expand Up @@ -458,10 +468,12 @@ impl FromGraphNpmPackages {
.packages
.entry(package_nv_ref.nv().clone())
.or_default()
.package_json = Some(FromGraphNpmModule {
specifier: specifier.into(),
source: source.into(),
});
.package_jsons
.get_or_insert_with(Vec::new)
.push(FromGraphNpmModule {
specifier: specifier.into(),
source: source.into(),
});
}

fn take_package(&mut self, nv: &PackageNv) -> Option<FromGraphNpmPackage> {
Expand All @@ -478,8 +490,11 @@ impl FromGraphNpmPackages {
meta_module
}

fn take_package_json(&mut self, nv: PackageNv) -> Option<FromGraphNpmModule> {
let package_json = self.get_mut(&nv)?.package_json.take();
fn take_package_jsons(
&mut self,
nv: PackageNv,
) -> Option<Vec<FromGraphNpmModule>> {
let package_json = self.get_mut(&nv)?.package_jsons.take();
self.partially_taken.insert(nv);
package_json
}
Expand Down Expand Up @@ -511,35 +526,20 @@ impl FromGraphNpmPackages {
package
.meta_modules
.into_iter()
.chain(package.package_jsons)
.flatten()
.chain(package.package_json)
.chain(package.modules.into_values())
})
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
struct FromGraphNpmPackage {
package_json: Option<FromGraphNpmModule>,
package_jsons: Option<Vec<FromGraphNpmModule>>,
meta_modules: Option<Vec<FromGraphNpmModule>>,
modules: IndexMap<NpmPackageNvReference, FromGraphNpmModule>,
}

impl Default for FromGraphNpmPackage {
fn default() -> Self {
Self {
// We want Some(Vec::new()) instead of None because in EszipV2::from_graph,
// FromGraphNpmPackages::take_meta_modules() is used to detect the first time a module of this
// package is imported, even if there aren't any meta modules
meta_modules: Some(Vec::new()),
package_json: Default::default(),
modules: Default::default(),
}
}
}

impl FromGraphNpmPackage {}

#[derive(Debug, Clone)]
pub struct FromGraphNpmModule {
specifier: String,
Expand Down Expand Up @@ -1402,31 +1402,20 @@ impl EszipV2 {
if visited.should_visit_package_meta() {
let meta_modules = npm_packages
.take_meta_modules(npm_module.nv_reference.nv().clone());
if let Some(meta_modules) = meta_modules {
for meta_module in meta_modules {
modules.insert(
meta_module.specifier,
EszipV2Module::Module {
kind: ModuleKind::OpaqueData,
source: EszipV2SourceSlot::Ready(meta_module.source.into()),
source_map: EszipV2SourceSlot::Ready(Arc::new([])),
},
);
}
let package_json = npm_packages
.take_package_json(npm_module.nv_reference.nv().clone());
if let Some(package_json) = package_json {
modules.insert(
package_json.specifier,
EszipV2Module::Module {
kind: ModuleKind::OpaqueData,
source: EszipV2SourceSlot::Ready(
package_json.source.into(),
),
source_map: EszipV2SourceSlot::Ready(Arc::new([])),
},
);
}
let package_jsons = npm_packages
.take_package_jsons(npm_module.nv_reference.nv().clone());

for meta_module in
meta_modules.into_iter().chain(package_jsons).flatten()
{
modules.insert(
meta_module.specifier,
EszipV2Module::Module {
kind: ModuleKind::OpaqueData,
source: EszipV2SourceSlot::Ready(meta_module.source.into()),
source_map: EszipV2SourceSlot::Ready(Arc::new([])),
},
);
}
} else if visited.should_visit_whole_package() {
let package =
Expand All @@ -1435,8 +1424,8 @@ impl EszipV2 {
let modules_to_insert = package
.meta_modules
.into_iter()
.chain(package.package_jsons)
.flatten()
.chain(package.package_json)
.chain(package.modules.shift_remove(&npm_module.nv_reference))
.chain(package.modules.into_values());
for module in modules_to_insert {
Expand Down Expand Up @@ -3033,10 +3022,10 @@ mod tests {
let mut from_graph_npm_packages = FromGraphNpmPackages::new();
from_graph_npm_packages.add_package(
PackageNv::from_str("[email protected]").unwrap(),
(
[(
"a_1.2.2/package.json",
b"package.json of [email protected]".as_slice(),
),
)],
[
(
NpmPackageNvReference::from_str("npm:[email protected]/foo").unwrap(),
Expand All @@ -3050,10 +3039,10 @@ mod tests {
);
from_graph_npm_packages.add_package_with_meta(
PackageNv::from_str("[email protected]").unwrap(),
(
[(
"d_5.0.0/package.json",
b"package.json of [email protected]".as_slice(),
),
)],
[
("manifest1:[email protected]", b"manifest 1 of [email protected]".as_slice()),
("manifest2:[email protected]", b"manifest 2 of [email protected]"),
Expand Down Expand Up @@ -3159,10 +3148,16 @@ mod tests {
let mut from_graph_npm_packages = FromGraphNpmPackages::new();
from_graph_npm_packages.add_package(
PackageNv::from_str("[email protected]").unwrap(),
(
"a_1.2.2/package.json",
b"package.json of [email protected]".as_slice(),
),
[
(
"a_1.2.2/package.json",
b"package.json of [email protected]".as_slice(),
),
(
"a_1.2.2/bar/package.json",
b"package.json of [email protected]/bar".as_slice(),
),
],
[
(
NpmPackageNvReference::from_str("npm:[email protected]/foo").unwrap(),
Expand All @@ -3176,10 +3171,10 @@ mod tests {
);
from_graph_npm_packages.add_package_with_meta(
PackageNv::from_str("[email protected]").unwrap(),
(
[(
"d_5.0.0/package.json",
b"package.json of [email protected]".as_slice(),
),
)],
[
("manifest1:[email protected]", b"manifest 1 of [email protected]".as_slice()),
("manifest2:[email protected]", b"manifest 2 of [email protected]"),
Expand All @@ -3197,10 +3192,10 @@ mod tests {
);
from_graph_npm_packages.add_package(
PackageNv::from_str("[email protected]").unwrap(),
(
[(
"z_0.1.2/package.json",
b"package.json of [email protected]".as_slice(),
),
)],
[(
NpmPackageNvReference::from_str("npm:[email protected]/foo").unwrap(),
("z_0.1.2/foo", b"source code of [email protected]/foo".as_slice()),
Expand All @@ -3227,6 +3222,7 @@ mod tests {
b"package.json of [email protected]",
// Then 'a'
b"package.json of [email protected]",
b"package.json of [email protected]/bar",
// Then other imports are included depth-first
b"import \"npm:a@^1.2/bar\";\nimport \"npm:other/bar\";",
// After esm modules, load imported npm packages depth-first. We don't have a module graph for cjs,
Expand Down Expand Up @@ -3266,10 +3262,10 @@ mod tests {
let mut from_graph_npm_packages = FromGraphNpmPackages::new();
from_graph_npm_packages.add_package_with_meta(
PackageNv::from_str("[email protected]").unwrap(),
(
[(
"d_5.0.0/package.json",
b"package.json of [email protected]".as_slice(),
),
)],
[
("manifest1:[email protected]", b"manifest 1 of [email protected]".as_slice()),
("manifest2:[email protected]", b"manifest 2 of [email protected]"),
Expand All @@ -3287,10 +3283,10 @@ mod tests {
);
from_graph_npm_packages.add_package(
PackageNv::from_str("[email protected]").unwrap(),
(
[(
"a_1.2.2/package.json",
b"package.json of [email protected]".as_slice(),
),
)],
[
(
NpmPackageNvReference::from_str("npm:[email protected]/foo").unwrap(),
Expand Down Expand Up @@ -3363,7 +3359,7 @@ mod tests {
let mut from_graph_npm_packages = FromGraphNpmPackages::new();
from_graph_npm_packages.add_package(
PackageNv::from_str("[email protected]").unwrap(),
("other/package.json", br#"{"main": "other.js"}"#.as_slice()),
[("other/package.json", br#"{"main": "other.js"}"#.as_slice())],
[
(
NpmPackageNvReference::from_str("npm:[email protected]/other.js")
Expand Down

0 comments on commit 682409a

Please sign in to comment.