-
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.
- Loading branch information
Adam McKee
committed
Nov 5, 2024
1 parent
cb3fa0f
commit 5313ee5
Showing
16 changed files
with
382 additions
and
26 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,3 @@ | ||
{ | ||
"path": "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,6 @@ | ||
[ | ||
{ | ||
"path": "routes/data/lambda.js", | ||
"result": "Identical" | ||
} | ||
] |
8 changes: 8 additions & 0 deletions
8
fn_build/fixtures/swc/nodejs/js/http_route/expect_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,8 @@ | ||
[ | ||
{ | ||
"path": "routes/data/lambda.js", | ||
"result": { | ||
"Content": "export const DELETE=()=>{console.log(\"delete\")};" | ||
} | ||
} | ||
] |
3 changes: 3 additions & 0 deletions
3
fn_build/fixtures/swc/nodejs/js/http_route/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,3 @@ | ||
export const DELETE = () => { | ||
console.log('delete') | ||
} |
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,25 @@ | ||
use crate::build_fn; | ||
use crate::result::FnBuildError; | ||
use crate::spec::{BuildMode, FnBuildSpec}; | ||
use std::env; | ||
use std::path::PathBuf; | ||
use temp_dir::TempDir; | ||
|
||
#[tokio::test] | ||
async fn build_fn_errors_for_invalid_extension() { | ||
let build_dir = TempDir::new().unwrap(); | ||
for entrypoint in &["README", "README.md"] { | ||
let build_spec = FnBuildSpec { | ||
entrypoint: PathBuf::from(entrypoint), | ||
mode: BuildMode::Debug, | ||
output: build_dir.path().to_path_buf(), | ||
project_dir: env::current_dir() | ||
.unwrap() | ||
.join("fixtures/swc/nodejs/js/http_route"), | ||
}; | ||
match build_fn(build_spec).await { | ||
Err(FnBuildError::InvalidFileType) => {} | ||
_ => panic!(), | ||
}; | ||
} | ||
} |
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,18 +1,20 @@ | ||
use crate::spec::FnParseSpec; | ||
use crate::{parse_fn, FnBuildError}; | ||
use std::env; | ||
use std::path::PathBuf; | ||
|
||
#[tokio::test] | ||
async fn parse_fn_errors_for_invalid_extension() { | ||
match parse_fn(PathBuf::from("README.md")).await { | ||
Err(FnBuildError::InvalidFileType) => {} | ||
_ => panic!(), | ||
}; | ||
} | ||
|
||
#[tokio::test] | ||
async fn parse_fn_errors_without_extension() { | ||
match parse_fn(PathBuf::from("README")).await { | ||
Err(FnBuildError::InvalidFileType) => {} | ||
_ => panic!(), | ||
}; | ||
for entrypoint in &["README", "README.md"] { | ||
let parse_spec = FnParseSpec { | ||
entrypoint: PathBuf::from(entrypoint), | ||
project_dir: env::current_dir() | ||
.unwrap() | ||
.join("fixtures/swc/nodejs/js/http_route"), | ||
}; | ||
match parse_fn(parse_spec).await { | ||
Err(FnBuildError::InvalidFileType) => {} | ||
_ => panic!(), | ||
}; | ||
} | ||
} |
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,26 @@ | ||
use std::path::PathBuf; | ||
|
||
#[derive(Clone, Eq, Hash, PartialEq)] | ||
pub enum BuildMode { | ||
Debug, | ||
Release, | ||
} | ||
|
||
pub type FnBuildOutput = PathBuf; | ||
|
||
// pub enum FnBuildOutput { | ||
// Archive(PathBuf), | ||
// Directory(PathBuf), | ||
// } | ||
|
||
pub struct FnBuildSpec { | ||
pub entrypoint: PathBuf, | ||
pub mode: BuildMode, | ||
pub output: FnBuildOutput, | ||
pub project_dir: PathBuf, | ||
} | ||
|
||
pub struct FnParseSpec { | ||
pub entrypoint: PathBuf, | ||
pub project_dir: PathBuf, | ||
} |
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,24 +1,59 @@ | ||
use crate::result::{FnBuildError, FnSource, ModuleImport}; | ||
use crate::result::{FnBuild, FnBuildError, FnSource, ModuleImport}; | ||
use crate::spec::{BuildMode, FnBuildSpec, FnParseSpec}; | ||
use crate::swc::compiler::SwcCompiler; | ||
use crate::swc::visitors::ImportVisitor; | ||
use std::path::PathBuf; | ||
use std::fs; | ||
use swc_ecma_visit::FoldWith; | ||
|
||
mod compiler; | ||
mod visitors; | ||
|
||
#[cfg(test)] | ||
mod swc_test; | ||
|
||
#[cfg(test)] | ||
mod visitors_test; | ||
|
||
pub async fn parse_js_fn(path: PathBuf) -> Result<FnSource, FnBuildError> { | ||
pub async fn build_js_fn(build_spec: FnBuildSpec) -> Result<FnBuild, FnBuildError> { | ||
let output_file = build_spec.output.join(&build_spec.entrypoint); | ||
fs::create_dir_all(output_file.parent().unwrap()).expect("mkdir -p"); | ||
match build_spec.mode { | ||
BuildMode::Debug => { | ||
fs::copy( | ||
build_spec.project_dir.join(&build_spec.entrypoint), | ||
output_file, | ||
) | ||
.expect("cp"); | ||
} | ||
BuildMode::Release => { | ||
let js_path = build_spec.project_dir.join(&build_spec.entrypoint); | ||
let minified_js = SwcCompiler::new().minify_js(&js_path).unwrap(); | ||
fs::write(output_file, minified_js).unwrap(); | ||
} | ||
} | ||
Ok(FnBuild { | ||
entrypoint: FnSource { | ||
imports: Vec::new(), | ||
path: build_spec.entrypoint, | ||
}, | ||
output: build_spec.output, | ||
}) | ||
} | ||
|
||
pub async fn parse_js_fn(parse_spec: FnParseSpec) -> Result<FnSource, FnBuildError> { | ||
let compiler = SwcCompiler::new(); | ||
let module = compiler.parse_es_module(&path).unwrap(); | ||
let module = compiler | ||
.parse_es_module(&parse_spec.project_dir.join(&parse_spec.entrypoint)) | ||
.unwrap(); | ||
let mut visitor = ImportVisitor::new(); | ||
module.fold_with(&mut visitor); | ||
let imports = visitor | ||
.result() | ||
.into_iter() | ||
.map(ModuleImport::Unknown) | ||
.collect(); | ||
Ok(FnSource { imports, path }) | ||
Ok(FnSource { | ||
imports, | ||
path: parse_spec.entrypoint, | ||
}) | ||
} |
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,22 @@ | ||
use crate::spec::FnBuildSpec; | ||
use crate::swc::build_js_fn; | ||
use crate::testing::{run_fixtures, BuildProcess, BuildProcessResult}; | ||
use std::path::PathBuf; | ||
use std::sync::Arc; | ||
|
||
struct JavaScriptBuild {} | ||
|
||
impl BuildProcess for JavaScriptBuild { | ||
fn build(&self, build_spec: FnBuildSpec) -> BuildProcessResult { | ||
Box::pin(build_js_fn(build_spec)) | ||
} | ||
} | ||
|
||
#[tokio::test] | ||
pub async fn test_nodejs_js_fixtures() { | ||
run_fixtures( | ||
Arc::new(Box::new(JavaScriptBuild {})), | ||
PathBuf::from("fixtures/swc/nodejs/js"), | ||
) | ||
.await; | ||
} |
Oops, something went wrong.