Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
alshdavid committed Aug 19, 2024
1 parent 1b7c1da commit 38b218c
Show file tree
Hide file tree
Showing 17 changed files with 101 additions and 124 deletions.
6 changes: 3 additions & 3 deletions packages/mach/src/cmd/build/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ use serde::Deserialize;
use serde::Serialize;

use super::super::MachOptions;
use crate::core::bundling::bundle;
use crate::core::packaging::package;
use crate::core::plugins::load_plugins;
use crate::core::resolve_and_transform::resolve_and_transform;
use crate::types::Compilation;
use crate::types::MachConfig;
use crate::core::bundling::bundle;

#[derive(Debug)]
pub struct BuildOptions {
Expand Down Expand Up @@ -38,7 +39,6 @@ pub fn build(
mach_options: MachOptions,
_build_options: BuildOptions,
) -> anyhow::Result<BuildResult> {

// This is the bundler state. It is passed into the bundling phases with read or write access
// depending on how that phase uses them
let mut compilation = Compilation {
Expand All @@ -65,10 +65,10 @@ pub fn build(

compilation.asset_graph.debug_render();


// This will read the asset graph and organize related assets into groupings (a.k.a bundles)
bundle(&mut compilation)?;

package(&mut compilation)?;
Ok(BuildResult::default())

// /*
Expand Down
2 changes: 1 addition & 1 deletion packages/mach/src/cmd/mach.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::collections::HashMap;
use std::path::PathBuf;

use crate::types::Machrc;
use crate::rpc::RpcHosts;
use crate::types::Machrc;

#[derive(Clone, Debug)]
pub struct MachOptions {
Expand Down
23 changes: 14 additions & 9 deletions packages/mach/src/core/bundling/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,31 @@ use crate::types::BundleId;
use crate::types::Compilation;

// Simple bundling algorithm that does not deduplicate assets or bundle split
pub fn bundle(
c: &mut Compilation,
) -> anyhow::Result<()> {
pub fn bundle(c: &mut Compilation) -> anyhow::Result<()> {
let asset_graph = c.asset_graph.as_graph();
let root_node = c.asset_graph.root_node();

let mut entries = Vec::<(AssetId, BundleId)>::new();

for entry_index in asset_graph.neighbors(root_node) {
let bundle = c.bundle_graph.get_bundle(c.bundle_graph.root_node()).unwrap();
for entry_index in asset_graph.neighbors(root_node) {
let bundle = c
.bundle_graph
.get_bundle(c.bundle_graph.root_node())
.unwrap();
let asset = c.asset_graph.get_asset(entry_index).unwrap();
entries.push((asset.id.clone(), bundle.id.clone()));
}

while let Some((entry_id, parent_bundle_id)) = entries.pop() {
let parent_bundle_nx = c.bundle_graph.get_index(&parent_bundle_id).unwrap().clone();

let entry_asset_nx = c.asset_graph.get_asset_from_asset_id(&entry_id).unwrap().clone();
let entry_asset_nx = c
.asset_graph
.get_asset_from_asset_id(&entry_id)
.unwrap()
.clone();
let entry_asset = c.asset_graph.get_asset(entry_asset_nx).unwrap();

let mut bundle = Bundle {
id: Default::default(),
kind: entry_asset.kind.clone(),
Expand All @@ -36,7 +41,7 @@ pub fn bundle(
};

let mut dependencies = Vec::<NodeIndex>::from(vec![entry_asset_nx]);

while let Some(current_nx) = dependencies.pop() {
let current_asset = c.asset_graph.get_asset(current_nx).unwrap();
bundle.insert_asset(&current_asset);
Expand Down
2 changes: 1 addition & 1 deletion packages/mach/src/core/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod bundling;
pub mod config;
// pub mod emit;
// pub mod packaging;
pub mod packaging;
pub mod plugins;
pub mod resolve_and_transform;
6 changes: 3 additions & 3 deletions packages/mach/src/core/packaging/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod css;
mod html;
mod javascript;
// mod css;
// mod html;
// mod javascript;
mod package;

pub use self::package::*;
83 changes: 2 additions & 81 deletions packages/mach/src/core/packaging/package.rs
Original file line number Diff line number Diff line change
@@ -1,84 +1,5 @@
use std::path::PathBuf;
use std::sync::Arc;

use swc_core::common::SourceMap;

use super::css::package_css;
use super::html::package_html;
use super::javascript::package_javascript;
use super::javascript::runtime_factory::RuntimeFactory;
use crate::public::AssetGraphSync;
use crate::public::AssetMapSync;
use crate::public::BundleGraphSync;
use crate::public::BundleManifestSync;
use crate::public::BundleMapSync;
use crate::public::DependencyMapSync;
use crate::public::MachConfigSync;
use crate::public::Output;
use crate::public::OutputsSync;

pub fn package(
config: MachConfigSync,
asset_map: AssetMapSync,
asset_graph: AssetGraphSync,
dependency_map: DependencyMapSync,
bundle_map: BundleMapSync,
bundle_graph: BundleGraphSync,
bundle_manifest: BundleManifestSync,
outputs: OutputsSync,
) -> Result<(), String> {
let asset_map = asset_map;
let source_map = Arc::new(SourceMap::default());
let js_runtime_factory = Arc::new(RuntimeFactory::new(source_map.clone()));

{
let mut bundle_manifest = bundle_manifest.write().unwrap();

for bundle in bundle_map.read().unwrap().values() {
bundle_manifest.insert(bundle.content_hash(), bundle.name.clone());
}
};

for bundle in bundle_map.read().unwrap().values() {
let bundle = bundle.clone();
let bundle_manifest = bundle_manifest.clone();

if bundle.kind == "js" {
package_javascript(
config.clone(),
asset_map.clone(),
asset_graph.clone(),
dependency_map.clone(),
bundle_map.clone(),
bundle_graph.clone(),
outputs.clone(),
js_runtime_factory.clone(),
bundle,
bundle_manifest,
);
} else if bundle.kind == "css" {
package_css(asset_map.clone(), outputs.clone(), bundle.clone())
} else if bundle.kind == "html" {
package_html(
asset_map.clone(),
asset_graph.clone(),
dependency_map.clone(),
bundle_map.clone(),
bundle_graph.clone(),
outputs.clone(),
bundle,
bundle_manifest,
js_runtime_factory.clone(),
);
}
}

let bundle_manifest_json = serde_json::to_string_pretty(&*bundle_manifest).unwrap();

outputs.write().unwrap().push(Output {
content: bundle_manifest_json.as_bytes().to_vec(),
filepath: PathBuf::from("bundle_manifest.json"),
});
use crate::types::Compilation;

pub fn package(_c: &mut Compilation) -> anyhow::Result<()> {
return Ok(());
}
21 changes: 18 additions & 3 deletions packages/mach/src/core/resolve_and_transform/run_transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ pub fn run_transformers(
break;
};

let mut asset_kind = file_path.extension().unwrap_or_default().to_str().unwrap_or_default().to_string();
let mut asset_kind = file_path
.extension()
.unwrap_or_default()
.to_str()
.unwrap_or_default()
.to_string();
let original_asset_kind = asset_kind.clone();

let mut mutable_asset = MutableAsset::new(
Expand Down Expand Up @@ -63,9 +68,19 @@ pub fn run_transformers(
}

// Update existing Asset with new data
asset.name = file_path.file_stem().unwrap_or_default().to_str().unwrap_or_default().to_string();
asset.name = file_path
.file_stem()
.unwrap_or_default()
.to_str()
.unwrap_or_default()
.to_string();
asset.content = content;
asset.kind = file_path.extension().unwrap_or_default().to_str().unwrap_or_default().to_string();
asset.kind = file_path
.extension()
.unwrap_or_default()
.to_str()
.unwrap_or_default()
.to_string();
asset.linking_symbols = linking_symbols;
asset.bundle_behavior = bundle_behavior;

Expand Down
2 changes: 1 addition & 1 deletion packages/mach/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ pub mod cmd;
pub mod core;
pub mod kit;
pub mod plugins;
pub mod types;
pub mod rpc;
pub mod types;

//
// Mach Lib API
Expand Down
2 changes: 1 addition & 1 deletion packages/mach/src/plugins/resolver_rpc/resolver.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use anyhow;

use crate::plugins::resolver_javascript::resolve;
use crate::rpc::RpcHostRef;
use crate::types::Dependency;
use crate::types::MachConfig;
use crate::types::ResolveResult;
use crate::types::Resolver;
use crate::rpc::RpcHostRef;

#[derive(Debug)]
pub struct ResolverAdapter {
Expand Down
2 changes: 1 addition & 1 deletion packages/mach/src/plugins/transformer_rpc/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use std::sync::Arc;

use anyhow;

use crate::rpc::RpcHost;
// use crate::plugins::resolver_javascript::resolve;
// use crate::public::DependencyOptions;
use crate::types::MachConfig;
use crate::types::MutableAsset;
use crate::types::Transformer;
use crate::rpc::RpcHost;

pub struct TransformerAdapter {
transformer_specifier: String,
Expand Down
2 changes: 1 addition & 1 deletion packages/mach/src/types/asset.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::fmt::Debug;
use std::path::PathBuf;

use crate::kit::hash::hash_sha_256;
use super::AssetId;
use super::BundleBehavior;
use super::LinkingSymbol;
use crate::kit::hash::hash_sha_256;

#[derive(Clone, Default)]
pub struct Asset {
Expand Down
17 changes: 13 additions & 4 deletions packages/mach/src/types/asset_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,11 @@ impl AssetGraph {
for node_index in self.graph.node_indices().into_iter() {
let mut edges = self.get_dependencies(&node_index);
let source_asset = self.get_asset(node_index).unwrap();
let mut src_path = source_asset.file_path_relative.to_str().unwrap().to_string();
let mut src_path = source_asset
.file_path_relative
.to_str()
.unwrap()
.to_string();
if src_path == "" {
// skip root
continue;
Expand All @@ -172,7 +176,7 @@ impl AssetGraph {
let dest_asset = self.get_asset(edge.target().id()).unwrap();
let dest_path = dest_asset.file_path_relative.to_str().unwrap();
let dest_path = format!("[{}] {}", dest_asset.id.0, dest_path);

output.push_str(&format!("{} -> {}\n", src_path, dest_path));
}
}
Expand All @@ -189,14 +193,19 @@ impl AssetGraph {
stdin.write_all(output.as_bytes()).unwrap();
stdin.write_all("\n`".as_bytes()).unwrap();

stdin.write_all(r#"
stdin
.write_all(
r#"
const { init } = require('diagonjs')
void async function() {
const d = await init()
console.log(d.translate.graphDAG(value))
}()
"#.as_bytes()).unwrap();
"#
.as_bytes(),
)
.unwrap();
drop(stdin);

let output = child.wait_with_output().unwrap();
Expand Down
9 changes: 7 additions & 2 deletions packages/mach/src/types/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ pub struct Bundle {
}

impl Bundle {
pub fn insert_asset(&mut self, asset: &Asset) -> Option<AssetId> {
self.assets.insert(asset.file_path_relative.clone(), asset.id.clone())
pub fn insert_asset(
&mut self,
asset: &Asset,
) -> Option<AssetId> {
self
.assets
.insert(asset.file_path_relative.clone(), asset.id.clone())
}
}

Expand Down
Loading

0 comments on commit 38b218c

Please sign in to comment.