Skip to content

Commit

Permalink
programmatic start
Browse files Browse the repository at this point in the history
  • Loading branch information
alshdavid committed May 16, 2024
1 parent ada62e2 commit 868d1c3
Show file tree
Hide file tree
Showing 34 changed files with 342 additions and 53 deletions.
2 changes: 1 addition & 1 deletion examples/app-react-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
}
}
2 changes: 1 addition & 1 deletion examples/app-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
}
}
2 changes: 1 addition & 1 deletion examples/html-circular-dependencies/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
"@alshdavid/mach": "workspace:*",
"http-server": "*"
}
}
}
2 changes: 1 addition & 1 deletion examples/html-commonjs-in-html/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
"@alshdavid/mach": "workspace:*",
"http-server": "*"
}
}
}
2 changes: 1 addition & 1 deletion examples/html-commonjs-nested/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
}
}
2 changes: 1 addition & 1 deletion examples/html-css-in-html/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
"@alshdavid/mach": "workspace:*",
"http-server": "*"
}
}
}
2 changes: 1 addition & 1 deletion examples/html-css-in-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
"@alshdavid/mach": "workspace:*",
"http-server": "*"
}
}
}
2 changes: 1 addition & 1 deletion examples/html-esm-dynamic-in-html/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
"@alshdavid/mach": "workspace:*",
"http-server": "*"
}
}
}
2 changes: 1 addition & 1 deletion examples/html-esm-in-html-simple/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
"@alshdavid/mach": "workspace:*",
"http-server": "*"
}
}
}
2 changes: 1 addition & 1 deletion examples/html-esm-in-html/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
"@alshdavid/mach": "workspace:*",
"http-server": "*"
}
}
}
2 changes: 1 addition & 1 deletion examples/html-json-in-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
"@alshdavid/mach": "workspace:*",
"http-server": "*"
}
}
}
2 changes: 1 addition & 1 deletion examples/html-multi-entry-same-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
"@alshdavid/mach": "workspace:*",
"http-server": "*"
}
}
}
2 changes: 1 addition & 1 deletion examples/html-ts-in-html/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
}
}
2 changes: 1 addition & 1 deletion examples/js-commonjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
"@alshdavid/mach": "workspace:*",
"http-server": "*"
}
}
}
2 changes: 1 addition & 1 deletion examples/js-simple/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
"@alshdavid/mach": "workspace:*",
"http-server": "*"
}
}
}
2 changes: 1 addition & 1 deletion examples/plugins-nodejs-resolver-noop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
"@alshdavid/mach": "workspace:*",
"http-server": "*"
}
}
}
2 changes: 1 addition & 1 deletion examples/plugins-nodejs-transformer-txt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
"@alshdavid/mach": "workspace:*",
"http-server": "*"
}
}
}
11 changes: 11 additions & 0 deletions mach/src/cmd/build/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ pub fn parse_config(command: BuildCommand) -> Result<MachConfigSync, String> {
vars
};

let diagnostic_port: Option<usize> = 'block: {
let Ok(value) = std::env::var("MACH_DIAGNOSTIC_PORT") else {
break 'block None;
};
let Ok(value) = value.parse::<usize>() else {
break 'block None;
};
Some(value)
};

return Ok(Arc::new(MachConfig {
start_time,
entry_point,
Expand All @@ -126,6 +136,7 @@ pub fn parse_config(command: BuildCommand) -> Result<MachConfigSync, String> {
optimize: !command.no_optimize,
env,
debug: command.debug,
diagnostic_port,
}));
}

Expand Down
11 changes: 11 additions & 0 deletions mach/src/cmd/build/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ use crate::platform::config::load_plugins;
use crate::platform::emit::emit;
use crate::platform::packaging::package;
use crate::platform::transformation::resolve_and_transform;
use crate::public::programmatic::ProgrammaticReporter;
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::OutputsSync;
Expand All @@ -28,7 +30,14 @@ pub fn main(command: BuildCommand) -> Result<(), String> {
let asset_graph = AssetGraphSync::default();
let bundles = BundleMapSync::default();
let bundle_graph = BundleGraphSync::default();
let bundle_manifest = BundleManifestSync::default();
let outputs = OutputsSync::default();
let programmatic_reporter = ProgrammaticReporter::new(
config.clone(),
asset_map.clone(),
bundles.clone(),
bundle_manifest.clone(),
);
let nodejs_adapter = NodejsAdapter::new(NodejsAdapterOptions {
workers: config.node_workers.clone() as u8,
})?;
Expand Down Expand Up @@ -100,6 +109,7 @@ pub fn main(command: BuildCommand) -> Result<(), String> {
dependency_map.clone(),
bundles.clone(),
bundle_graph.clone(),
bundle_manifest.clone(),
outputs.clone(),
)?;

Expand All @@ -112,5 +122,6 @@ pub fn main(command: BuildCommand) -> Result<(), String> {

reporter.print_emit_stats();
reporter.print_finished_stats();
programmatic_reporter.emit_build_report();
Ok(())
}
2 changes: 1 addition & 1 deletion mach/src/cmd/build/reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl AppReporter {
let bundles = self.bundles.read().unwrap();
let time_bundle = self.config.time_elapsed();
let mut bundle_kinds = HashMap::<String, usize>::new();
for bundle in bundles.iter() {
for bundle in bundles.values() {
if !bundle_kinds.contains_key(&bundle.kind) {
bundle_kinds.insert(bundle.kind.clone(), 0);
}
Expand Down
9 changes: 5 additions & 4 deletions mach/src/platform/packaging/html/package_html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::public::AssetGraphSync;
use crate::public::AssetMapSync;
use crate::public::Bundle;
use crate::public::BundleGraphSync;
use crate::public::BundleManifest;
use crate::public::BundleManifestSync;
use crate::public::BundleMapSync;
use crate::public::DependencyMapSync;
use crate::public::Output;
Expand All @@ -31,13 +31,14 @@ pub fn package_html(
bundle_graph: BundleGraphSync,
outputs: Arc<RwLock<Outputs>>,
bundle: Bundle,
bundle_manifest: &BundleManifest,
bundle_manifest: BundleManifestSync,
js_runtime_factory: Arc<RuntimeFactory>,
) {
let asset_graph = asset_graph.read().unwrap();
let dependency_map = dependency_map.read().unwrap();
let bundle_map = bundle_map.read().unwrap();
let bundle_graph = bundle_graph.read().unwrap();
let bundle_manifest = bundle_manifest.read().unwrap();

let entry_asset = bundle.entry_asset.as_ref().unwrap();
let Some(dependencies) = asset_graph.get_dependencies(&entry_asset) else {
Expand Down Expand Up @@ -131,7 +132,7 @@ pub fn package_html(

let bundle_id = bundle_graph.get(&dependency.id).unwrap();
let bundle_hash = bundle_map
.iter()
.values()
.find(|b| &b.id == bundle_id)
.unwrap()
.content_hash();
Expand All @@ -148,7 +149,7 @@ pub fn package_html(
html::set_attribute(script_node, "src", file_path);
}

for bundle in bundle_map.iter() {
for bundle in bundle_map.values() {
if bundle.kind == "css" {
let elm = html::create_element(html::CreateElementOptions {
tag_name: "link",
Expand Down
5 changes: 3 additions & 2 deletions mach/src/platform/packaging/javascript/package_javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::public::AssetId;
use crate::public::AssetMapSync;
use crate::public::Bundle;
use crate::public::BundleGraphSync;
use crate::public::BundleManifest;
use crate::public::BundleManifestSync;
use crate::public::BundleMapSync;
use crate::public::DependencyMapSync;
use crate::public::MachConfigSync;
Expand All @@ -39,9 +39,10 @@ pub fn package_javascript<'a>(
outputs: Arc<RwLock<Outputs>>,
runtime_factory: Arc<RuntimeFactory>,
bundle: Bundle,
bundle_manifest: Arc<BundleManifest>,
bundle_manifest: BundleManifestSync,
) {
let bundle_map = bundle_map.read().unwrap();
let bundle_manifest = bundle_manifest.read().unwrap();
let bundle_graph = bundle_graph.read().unwrap();
let source_map = Arc::new(SourceMap::default());

Expand Down
20 changes: 10 additions & 10 deletions mach/src/platform/packaging/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use super::javascript::runtime_factory::RuntimeFactory;
use crate::public::AssetGraphSync;
use crate::public::AssetMapSync;
use crate::public::BundleGraphSync;
use crate::public::BundleManifest;
use crate::public::BundleManifestSync;
use crate::public::BundleMapSync;
use crate::public::DependencyMapSync;
use crate::public::MachConfigSync;
Expand All @@ -24,22 +24,22 @@ pub fn package(
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 runtime_factory = Arc::new(RuntimeFactory::new(source_map.clone()));
let js_runtime_factory = Arc::new(RuntimeFactory::new(source_map.clone()));

let bundle_manifest = {
let mut bundle_manifest = BundleManifest::new();
{
let mut bundle_manifest = bundle_manifest.write().unwrap();

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

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

Expand All @@ -52,7 +52,7 @@ pub fn package(
bundle_map.clone(),
bundle_graph.clone(),
outputs.clone(),
runtime_factory.clone(),
js_runtime_factory.clone(),
bundle,
bundle_manifest,
);
Expand All @@ -67,8 +67,8 @@ pub fn package(
bundle_graph.clone(),
outputs.clone(),
bundle,
&bundle_manifest,
runtime_factory.clone(),
bundle_manifest,
js_runtime_factory.clone(),
);
}
}
Expand Down
3 changes: 3 additions & 0 deletions mach/src/public/bundle_manifest.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::RwLock;

pub type BundleManifestSync = Arc<RwLock<HashMap<String, String>>>;
pub type BundleManifest = HashMap<String, String>;
7 changes: 6 additions & 1 deletion mach/src/public/bundle_map.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::hash_map::Iter;
use std::collections::hash_map::Values;
use std::collections::HashMap;
use std::sync::Arc;
Expand All @@ -21,10 +22,14 @@ impl BundleMap {
self.bundles.insert(bundle.id.clone(), bundle);
}

pub fn iter(&self) -> Values<'_, BundleId, Bundle> {
pub fn values(&self) -> Values<'_, BundleId, Bundle> {
self.bundles.values()
}

pub fn iter(&self) -> Iter<'_, BundleId, Bundle> {
self.bundles.iter()
}

pub fn len(&self) -> usize {
self.bundles.len()
}
Expand Down
1 change: 1 addition & 0 deletions mach/src/public/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct MachConfig {
pub bundle_splitting: bool,
pub env: HashMap<String, String>,
pub debug: bool,
pub diagnostic_port: Option<usize>,
}

impl MachConfig {
Expand Down
1 change: 1 addition & 0 deletions mach/src/public/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ mod mutable_asset;
pub mod nodejs;
mod package_json;
mod packages;
pub mod programmatic;
mod resolver;
mod specifier_type;
mod transformer;
Expand Down
10 changes: 10 additions & 0 deletions mach/src/public/programmatic/action.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use serde::Deserialize;
use serde::Serialize;

use super::BuildReport;

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum ProgrammaticAction {
BuildReport { data: BuildReport },
}
11 changes: 11 additions & 0 deletions mach/src/public/programmatic/build_report.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use std::collections::HashMap;

use serde::Deserialize;
use serde::Serialize;

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BuildReport {
pub bundle_manifest: HashMap<String, String>,
pub entries: HashMap<String, String>,
}
7 changes: 7 additions & 0 deletions mach/src/public/programmatic/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mod action;
mod build_report;
mod programmatic_reporter;

pub use self::action::*;
pub use self::build_report::*;
pub use self::programmatic_reporter::*;
Loading

0 comments on commit 868d1c3

Please sign in to comment.