-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
parallel parsing event loop, circular dependency resolution, lib api …
…surface and examples
- Loading branch information
Adam McKee
committed
Nov 12, 2024
1 parent
d8c98ec
commit 989323d
Showing
29 changed files
with
456 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use l3_fn_build::runtime::node::NodeConfig; | ||
use l3_fn_build::runtime::Runtime; | ||
use l3_fn_build::{build_fn, BuildMode, FnBuildSpec, FnParseSpec}; | ||
use std::env; | ||
use std::path::PathBuf; | ||
use std::sync::Arc; | ||
use temp_dir::TempDir; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let project_dir = PathBuf::from( | ||
env::args() | ||
.nth(1) | ||
.unwrap_or_else(|| "fixtures/node/js/circular_imports".to_string()), | ||
); | ||
let out_dir = TempDir::new().unwrap(); | ||
let node_config = NodeConfig::read_node_config(&project_dir).unwrap(); | ||
let fn_build = build_fn(FnBuildSpec { | ||
function: FnParseSpec { | ||
entrypoint: PathBuf::from("routes/data/lambda.js"), | ||
project_dir: Arc::new(env::current_dir().unwrap().join(&project_dir)), | ||
runtime: Runtime::Node(Arc::new(node_config)), | ||
}, | ||
mode: BuildMode::Debug, | ||
output: out_dir.path().to_path_buf(), | ||
}) | ||
.await | ||
.unwrap(); | ||
println!( | ||
"l3_fn_build::build_fn output in temp dir from project dir {}:", | ||
project_dir.to_string_lossy() | ||
); | ||
for source in fn_build.manifest.sources { | ||
println!(" {}", source.path.to_string_lossy()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use l3_fn_build::runtime::node::NodeConfig; | ||
use l3_fn_build::runtime::Runtime; | ||
use l3_fn_build::{parse_fn, FnParseSpec}; | ||
use std::env; | ||
use std::path::PathBuf; | ||
use std::sync::Arc; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let project_dir = PathBuf::from( | ||
env::args() | ||
.nth(1) | ||
.unwrap_or_else(|| "fixtures/node/js/circular_imports".to_string()), | ||
); | ||
let node_config = NodeConfig::read_node_config(&project_dir).unwrap(); | ||
let fn_manifest = parse_fn(FnParseSpec { | ||
entrypoint: PathBuf::from("routes/data/lambda.js"), | ||
project_dir: Arc::new(env::current_dir().unwrap().join(&project_dir)), | ||
runtime: Runtime::Node(Arc::new(node_config)), | ||
}) | ||
.await | ||
.unwrap(); | ||
println!( | ||
"l3_fn_build::parse_fn result for project dir {}:", | ||
project_dir.to_string_lossy() | ||
); | ||
for source in fn_manifest.sources { | ||
println!(" {}", source.path.to_string_lossy()); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
fn_build/fixtures/node/js/circular_imports/.fixture/build_debug.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"files": [ | ||
{ | ||
"path": "package.json", | ||
"result": "identical" | ||
}, | ||
{ | ||
"path": "lib/api.js", | ||
"result": "identical" | ||
}, | ||
{ | ||
"path": "lib/data.js", | ||
"result": "identical" | ||
}, | ||
{ | ||
"path": "routes/data/lambda.js", | ||
"result": "identical" | ||
} | ||
] | ||
} |
26 changes: 26 additions & 0 deletions
26
fn_build/fixtures/node/js/circular_imports/.fixture/build_release.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"files": [ | ||
{ | ||
"path": "package.json", | ||
"result": "identical" | ||
}, | ||
{ | ||
"path": "lib/api.js", | ||
"result": { | ||
"content": "import{getBackendData as t}from\"./data.js\";export function getData(){return t()}" | ||
} | ||
}, | ||
{ | ||
"path": "lib/data.js", | ||
"result": { | ||
"content": "import{getData as t}from\"./api.js\";export function getBackendData(){return t()}" | ||
} | ||
}, | ||
{ | ||
"path": "routes/data/lambda.js", | ||
"result": { | ||
"content": "import{getData as o}from\"../../lib/api.js\";export const GET=()=>{console.log(\"got\",o())};" | ||
} | ||
} | ||
] | ||
} |
33 changes: 33 additions & 0 deletions
33
fn_build/fixtures/node/js/circular_imports/.fixture/parse.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"dependencies": "unused", | ||
"sources": [ | ||
{ | ||
"imports": [], | ||
"path": "package.json" | ||
}, | ||
{ | ||
"imports": [ | ||
{ | ||
"relativeSource": "lib/data.js" | ||
} | ||
], | ||
"path": "lib/api.js" | ||
}, | ||
{ | ||
"imports": [ | ||
{ | ||
"relativeSource": "lib/api.js" | ||
} | ||
], | ||
"path": "lib/data.js" | ||
}, | ||
{ | ||
"imports": [ | ||
{ | ||
"relativeSource": "lib/api.js" | ||
} | ||
], | ||
"path": "routes/data/lambda.js" | ||
} | ||
] | ||
} |
3 changes: 3 additions & 0 deletions
3
fn_build/fixtures/node/js/circular_imports/.fixture/spec.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"entrypoint": "routes/data/lambda.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import {getBackendData} from './data.js' | ||
|
||
export function getData() { | ||
return getBackendData() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import {getData} from './api.js' | ||
|
||
export function getBackendData() { | ||
return getData() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"type": "module" | ||
} |
5 changes: 5 additions & 0 deletions
5
fn_build/fixtures/node/js/circular_imports/routes/data/lambda.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import {getData} from '../../lib/api.js' | ||
|
||
export const GET = () => { | ||
console.log('got', getData()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,21 @@ | ||
use crate::result::ModuleImport; | ||
use crate::runtime::node::NodeConfig; | ||
use std::path::Path; | ||
use crate::{FnBuildResult, FnSource, ModuleImport}; | ||
use std::path::{Path, PathBuf}; | ||
use std::sync::Arc; | ||
|
||
pub mod node; | ||
mod parse_fn; | ||
|
||
pub trait ImportResolver: Send + Sync { | ||
trait ImportResolver: Send + Sync { | ||
fn resolve(&self, project_dir: &Path, from: &Path, import: &str) -> ModuleImport; | ||
} | ||
|
||
trait FnSourceParser: Send + Sync { | ||
fn collect_runtime_sources(&self, project_dir: &Path) -> Vec<FnSource>; | ||
fn parse(&self, project_dir: &Path, source_path: PathBuf) -> FnBuildResult<FnSource>; | ||
} | ||
|
||
#[derive(Clone)] | ||
pub enum Runtime { | ||
Node(Arc<NodeConfig>), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use crate::fs::copy_dir_all; | ||
use crate::runtime::node::parse_node_fn; | ||
use crate::swc::compiler::SwcCompiler; | ||
use crate::{BuildMode, FnBuild, FnBuildResult, FnBuildSpec, FnDependencies, FnSource}; | ||
use std::fs; | ||
use std::path::Path; | ||
|
||
pub async fn build_node_fn(build_spec: FnBuildSpec) -> FnBuildResult<FnBuild> { | ||
let manifest = parse_node_fn(build_spec.function.clone()).await?; | ||
if let FnDependencies::Required = manifest.dependencies { | ||
copy_dir_all( | ||
&build_spec.function.project_dir.join("node_modules"), | ||
&build_spec.output.join("node_modules"), | ||
)?; | ||
} | ||
for source in &manifest.sources { | ||
debug_assert!(source.path.is_relative()); | ||
let output_path = build_spec.output.join(&source.path); | ||
fs::create_dir_all(output_path.parent().unwrap()).expect("mkdir -p"); | ||
if let Some(extension) = source.path.extension() { | ||
if (extension == "js" || extension == "mjs") && build_spec.mode == BuildMode::Release { | ||
let js_path = build_spec.function.project_dir.join(&source.path); | ||
let minified_js = SwcCompiler::new().minify_js(&js_path).unwrap(); | ||
fs::write(output_path, minified_js)?; | ||
continue; | ||
} | ||
} | ||
fs::copy( | ||
build_spec.function.project_dir.join(&source.path), | ||
output_path, | ||
) | ||
.expect("cp"); | ||
} | ||
Ok(FnBuild { | ||
manifest, | ||
output: build_spec.output, | ||
}) | ||
} | ||
|
||
#[allow(unused)] | ||
async fn copy_sources( | ||
project_dir: &Path, | ||
sources: &Vec<FnSource>, | ||
output_path: &Path, | ||
) -> FnBuildResult<()> { | ||
for source in sources { | ||
fs::copy(project_dir.join(&source.path), output_path)?; | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.