Skip to content

Commit a259e07

Browse files
committed
resolve_and_transform
1 parent 8c63984 commit a259e07

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+389
-327
lines changed

justfile

+3-2
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,12 @@ build-tsc:
106106
[no-cd]
107107
run *ARGS:
108108
just build
109-
env NODE_OPTIONS="--conditions="source" --experimental-strip-types --no-warnings" npx mach {{ARGS}}
109+
env NODE_OPTIONS="--conditions=source --import tsx" npx mach {{ARGS}}
110110

111111
alias test-integration := integration-tests
112112
integration-tests *ARGS:
113-
node --conditions="source" --experimental-strip-types --no-warnings ./testing/setup.ts {{ARGS}}
113+
just build
114+
node --conditions=source --import tsx ./testing/setup.ts {{ARGS}}
114115

115116
alias test-unit := unit-tests
116117
unit-tests:

packages/mach/src/cmd/build/build.rs

+23-11
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@ use serde::Deserialize;
66
use serde::Serialize;
77

88
use super::super::MachOptions;
9-
use super::build_parse_config::parse_config;
9+
// use super::build_parse_config::parse_config;
1010
// use super::create_result::create_build_result;
1111
// use crate::platform::bundling::bundle;
1212
use crate::core::plugins::load_plugins;
13-
use crate::core::transformation::build_asset_graph;
13+
use crate::core::resolve_and_transform::resolve_and_transform;
1414
// use crate::public::AssetGraphSync;
1515
// use crate::public::AssetMap;
1616
// use crate::public::AssetMapSync;
1717
// use crate::public::BundleGraphSync;
1818
// use crate::public::BundleManifestSync;
1919
// use crate::public::BundleMapSync;
2020
use crate::public::Compilation;
21+
use crate::public::MachConfig;
2122
// use crate::public::DependencyMapSync;
2223
// use crate::rpc::Engine;
2324
// use crate::platform::emit::emit;
@@ -64,32 +65,43 @@ pub fn build(
6465
mach_options: MachOptions,
6566
options: BuildOptions,
6667
) -> anyhow::Result<BuildResult> {
67-
let config = parse_config(&options).map_err(|e| anyhow::anyhow!(e))?;
68+
// let config = parse_config(&options).map_err(|e| anyhow::anyhow!(e))?;
6869

6970
/*
7071
This is the bundler state. It is passed into
71-
the bundling phases with read or write permissions
72+
the bundling phases with read or write access
7273
depending on how that phase uses them
7374
*/
74-
let mut compilation = Compilation::new();
75-
let adapter_map = mach_options.rpc_hosts;
75+
let mut compilation = Compilation{
76+
machrc: mach_options.config,
77+
rpc_hosts: mach_options.rpc_hosts,
78+
config: MachConfig {
79+
threads: mach_options.threads,
80+
entries: mach_options.entries,
81+
project_root: mach_options.project_root,
82+
env: mach_options.env,
83+
out_folder: mach_options.out_folder,
84+
},
85+
asset_contents: Default::default(),
86+
asset_graph: Default::default(),
87+
plugins: Default::default(),
88+
};
7689

7790
/*
78-
load_plugins() will read source the .machrc and will
79-
fetch then initialize the referenced plugins
91+
load_plugins() will read the Machrc and initialize the referenced plugins
8092
*/
81-
let plugins = load_plugins(&config, &config.machrc, &adapter_map)?;
93+
load_plugins(&mut compilation)?;
8294

8395
/*
8496
resolve_and_transform() build the AssetGraph.
8597
8698
It does this by crawling the source files, identify import statements, modifying their contents
8799
(like removing TypeScript types) and looping until there are no more import statements to resolve.
88100
*/
89-
build_asset_graph(config.clone(), plugins.clone(), &mut compilation)
101+
resolve_and_transform(&mut compilation)
90102
.map_err(|e| anyhow::anyhow!(e))?;
91103

92-
println!("{}", &compilation.asset_graph.into_dot(&config));
104+
println!("{}", &compilation.asset_graph.into_dot(&compilation.config));
93105

94106
Ok(BuildResult::default())
95107

packages/mach/src/cmd/build/build_parse_config.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -88,25 +88,18 @@ fn parse_machrc(file_path: PathBuf) -> Result<Machrc, String> {
8888
};
8989

9090
let mut mach_config = Machrc {
91-
is_default: false,
92-
file_path,
93-
engines: vec!["mach".to_string()],
9491
resolvers: None,
9592
transformers: None,
9693
};
9794

98-
let Ok(json_file) = fs::read_to_string(&mach_config.file_path) else {
95+
let Ok(json_file) = fs::read_to_string(&file_path) else {
9996
return Err("Unable to read file".to_string());
10097
};
10198

10299
let Ok(json) = serde_json::from_str::<serde_json::Value>(&json_file) else {
103100
return Err("Unable to parse json".to_string());
104101
};
105102

106-
if json_file.contains("\"node:") {
107-
mach_config.engines.push("node".to_string());
108-
}
109-
110103
if let Some(resolvers_value) = json.get("resolvers") {
111104
let mut resolvers = Vec::<String>::new();
112105
let Some(resolvers_values) = resolvers_value.as_array() else {

packages/mach/src/cmd/build/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
mod build;
2-
mod build_parse_config;
2+
// mod build_parse_config;
33
mod create_result;
44
// mod build_event;
55

packages/mach/src/cmd/mach.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
1-
use crate::rpc::RpcHosts;
1+
use std::{collections::HashMap, path::PathBuf};
22

3-
#[derive(Clone)]
3+
use crate::{public::Machrc, rpc::RpcHosts};
4+
5+
#[derive(Clone, Debug)]
46
pub struct MachOptions {
57
pub rpc_hosts: RpcHosts,
6-
/// How many threads to use for compilation
78
pub threads: usize,
9+
pub entries: Vec<PathBuf>,
10+
pub config: Machrc,
11+
pub env: HashMap<String, String>,
12+
pub out_folder: PathBuf,
13+
pub project_root: PathBuf,
814
}
915

1016
impl Default for MachOptions {
1117
fn default() -> Self {
1218
Self {
1319
rpc_hosts: Default::default(),
1420
threads: num_cpus::get(),
21+
entries: Default::default(),
22+
config: Default::default(),
23+
env: Default::default(),
24+
out_folder: Default::default(),
25+
project_root: Default::default(),
1526
}
1627
}
1728
}

packages/mach/src/core/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ pub mod config;
33
// pub mod emit;
44
// pub mod packaging;
55
pub mod plugins;
6-
pub mod transformation;
6+
pub mod resolve_and_transform;
77
pub mod request_tracker;
+47-51
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
use std::sync::Arc;
2+
13
use anyhow;
24

3-
use super::PluginContainer;
4-
use super::PluginContainerSync;
55
use crate::plugins::resolver_javascript::ResolverJavaScript;
66
use crate::plugins::resolver_rpc::ResolverAdapter;
77
use crate::plugins::transformer_css::TransformerCSS;
@@ -10,54 +10,49 @@ use crate::plugins::transformer_html::TransformerHtml;
1010
use crate::plugins::transformer_javascript::TransformerJavaScript;
1111
use crate::plugins::transformer_json::TransformerJson;
1212
use crate::plugins::transformer_rpc::TransformerAdapter;
13-
use crate::public::MachConfig;
14-
use crate::public::Machrc;
13+
use crate::public::Compilation;
1514
use crate::public::Transformer;
16-
use crate::rpc::RpcHosts;
1715

1816
pub fn load_plugins(
19-
config: &MachConfig,
20-
machrc: &Machrc,
21-
adapter_map: &RpcHosts,
22-
) -> anyhow::Result<PluginContainerSync> {
23-
let mut plugins = PluginContainer::default();
24-
25-
if let Some(resolvers) = &machrc.resolvers {
17+
c: &mut Compilation,
18+
) -> anyhow::Result<()> {
19+
if let Some(resolvers) = &c.machrc.resolvers {
2620
for plugin_string in resolvers {
27-
let Some((engine, specifier)) = plugin_string.split_once(':') else {
28-
return Err(anyhow::anyhow!(format!(
29-
"Unable to parse engine:specifier for {}",
30-
plugin_string
31-
)));
32-
};
33-
34-
if engine == "mach" && specifier == "resolver" {
35-
plugins.resolvers.push(Box::new(ResolverJavaScript::new()));
36-
continue;
21+
match plugin_string.as_str() {
22+
// Built-in
23+
"mach:resolver" => {
24+
c.plugins.resolvers.push(Arc::new(ResolverJavaScript::new()));
25+
continue;
26+
}
27+
// External
28+
specifier => {
29+
let Some((engine, specifier)) = specifier.split_once(':') else {
30+
return Err(anyhow::anyhow!(format!(
31+
"Unable to parse engine:specifier for {}",
32+
plugin_string
33+
)));
34+
};
35+
36+
let Some(adapter) = c.rpc_hosts.get(engine) else {
37+
return Err(anyhow::anyhow!(format!(
38+
"No plugin runtime for engine: {}\nCannot load plugin: {}",
39+
engine, specifier
40+
)));
41+
};
42+
43+
c.plugins.resolvers.push(Arc::new(ResolverAdapter::new(
44+
&c.config,
45+
specifier,
46+
adapter.clone(),
47+
)?));
48+
}
3749
}
38-
39-
let Some(adapter) = adapter_map.get(engine) else {
40-
return Err(anyhow::anyhow!(format!(
41-
"No plugin runtime for engine: {}\nCannot load plugin: {}",
42-
engine, specifier
43-
)));
44-
};
45-
46-
adapter.start()?;
47-
48-
plugins.resolvers.push(Box::new(ResolverAdapter::new(
49-
&*config,
50-
specifier,
51-
adapter.clone(),
52-
)?));
53-
54-
continue;
5550
}
5651
}
5752

58-
if let Some(transformers) = &machrc.transformers {
53+
if let Some(transformers) = &c.machrc.transformers {
5954
for (pattern, specifiers) in transformers {
60-
let mut transformers = Vec::<Box<dyn Transformer>>::new();
55+
let mut transformers = Vec::<Arc<dyn Transformer>>::new();
6156

6257
for plugin_string in specifiers {
6358
let Some((engine, specifier)) = plugin_string.split_once(':') else {
@@ -67,31 +62,31 @@ pub fn load_plugins(
6762
)));
6863
};
6964
if engine == "mach" && specifier == "transformer/javascript" {
70-
transformers.push(Box::new(TransformerJavaScript {}));
65+
transformers.push(Arc::new(TransformerJavaScript {}));
7166
continue;
7267
}
7368

7469
if engine == "mach" && specifier == "transformer/css" {
75-
transformers.push(Box::new(TransformerCSS {}));
70+
transformers.push(Arc::new(TransformerCSS {}));
7671
continue;
7772
}
7873

7974
if engine == "mach" && specifier == "transformer/html" {
80-
transformers.push(Box::new(TransformerHtml {}));
75+
transformers.push(Arc::new(TransformerHtml {}));
8176
continue;
8277
}
8378

8479
if engine == "mach" && specifier == "transformer/json" {
85-
transformers.push(Box::new(TransformerJson {}));
80+
transformers.push(Arc::new(TransformerJson {}));
8681
continue;
8782
}
8883

8984
if engine == "mach" && specifier == "transformer/drop" {
90-
transformers.push(Box::new(TransformerDrop {}));
85+
transformers.push(Arc::new(TransformerDrop {}));
9186
continue;
9287
}
9388

94-
let Some(adapter) = adapter_map.get(engine) else {
89+
let Some(adapter) = c.rpc_hosts.get(engine) else {
9590
return Err(anyhow::anyhow!(format!(
9691
"No plugin runtime for engine: {}\nCannot load plugin: {}",
9792
engine, specifier
@@ -100,18 +95,19 @@ pub fn load_plugins(
10095

10196
adapter.start()?;
10297

103-
transformers.push(Box::new(TransformerAdapter::new(
104-
&*config,
98+
transformers.push(Arc::new(TransformerAdapter::new(
99+
&c.config,
105100
specifier,
106101
adapter.clone(),
107102
)?));
108103
}
109104

110-
plugins
105+
c.plugins
111106
.transformers
112107
.transformers
113108
.insert(pattern.clone(), transformers);
114109
}
115110
}
116-
return Ok(plugins.to_sync());
111+
112+
return Ok(());
117113
}

packages/mach/src/core/plugins/plugin_container.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,28 @@ use crate::public::Transformer;
77

88
pub type PluginContainerSync = Arc<PluginContainer>;
99

10-
#[derive(Default, Debug)]
10+
#[derive(Clone, Default, Debug)]
1111
pub struct PluginContainer {
12-
pub resolvers: Vec<Box<dyn Resolver>>,
12+
pub resolvers: Vec<Arc<dyn Resolver>>,
1313
pub transformers: TransformerMap,
1414
}
1515

16-
impl PluginContainer {
17-
pub fn to_sync(&mut self) -> PluginContainerSync {
18-
Arc::new(std::mem::take(self))
19-
}
20-
}
21-
22-
#[derive(Default, Debug)]
16+
#[derive(Clone, Default, Debug)]
2317
pub struct TransformerMap {
24-
pub transformers: HashMap<String, Vec<Box<dyn Transformer>>>,
18+
pub transformers: HashMap<String, Vec<Arc<dyn Transformer>>>,
2519
}
2620

2721
impl TransformerMap {
2822
pub fn get(
2923
&self,
3024
file_target: &TransformerTarget,
31-
) -> Result<(String, &Vec<Box<dyn Transformer>>), String> {
25+
) -> anyhow::Result<(String, &Vec<Arc<dyn Transformer>>)> {
3226
for (pattern, transformers) in &self.transformers {
3327
if glob_match::glob_match(&pattern, &file_target.file_name) {
3428
return Ok((pattern.clone(), transformers));
3529
}
3630
}
37-
return Err(format!("No transformer found {:?}", file_target.file_path,));
31+
anyhow::bail!("No transformer found {:?}", file_target.file_path)
3832
}
3933
}
4034

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mod resolve_and_transform;
2+
mod run_resolvers;
3+
mod run_transformers;
4+
5+
pub use self::resolve_and_transform::*;

0 commit comments

Comments
 (0)