-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move web-bundler to a stand-alone crate called `metassr-bun…
…dler`
- Loading branch information
Showing
10 changed files
with
145 additions
and
116 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 |
---|---|---|
@@ -1,101 +0,0 @@ | ||
use anyhow::{anyhow, Result}; | ||
use lazy_static::lazy_static; | ||
use metacall::{loaders, metacall, MetacallNull}; | ||
use std::{collections::HashMap, ffi::OsStr, marker::Sized, path::Path, sync::Mutex}; | ||
|
||
use crate::traits::Exec; | ||
lazy_static! { | ||
static ref IS_BUNDLING_SCRIPT_LOADED: Mutex<BundleSciptLoadingState> = | ||
Mutex::new(BundleSciptLoadingState::new()); | ||
} | ||
static BUILD_SCRIPT: &str = include_str!("./scripts/bundle.js"); | ||
const BUNDLING_FUNC: &str = "web_bundling"; | ||
|
||
/// A detector for if the bundling script `./scripts/bundle.js` is loaded or not. | ||
#[derive(Debug)] | ||
pub struct BundleSciptLoadingState(bool); | ||
|
||
impl BundleSciptLoadingState { | ||
pub fn new() -> Self { | ||
Self(false) | ||
} | ||
pub fn loaded(&mut self) { | ||
self.0 = true | ||
} | ||
pub fn is_loaded(&self) -> bool { | ||
self.0 | ||
} | ||
} | ||
|
||
impl Default for BundleSciptLoadingState { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
|
||
pub struct WebBundler<'a> { | ||
pub targets: HashMap<String, &'a Path>, | ||
pub dist_path: &'a Path, | ||
} | ||
|
||
impl<'a> WebBundler<'a> { | ||
pub fn new<S>(targets: &'a HashMap<String, String>, dist_path: &'a S) -> Self | ||
where | ||
S: AsRef<OsStr> + ?Sized, | ||
{ | ||
let targets: HashMap<String, &Path> = targets | ||
.iter() | ||
.map(|(k, v)| (k.into(), Path::new(v))) | ||
.collect(); | ||
|
||
Self { | ||
targets, | ||
dist_path: Path::new(dist_path), | ||
} | ||
} | ||
} | ||
|
||
impl<'a> Exec for WebBundler<'a> { | ||
type Output = (); | ||
fn exec(&self) -> Result<Self::Output> { | ||
if !IS_BUNDLING_SCRIPT_LOADED.lock().unwrap().is_loaded() { | ||
if let Err(e) = loaders::from_memory("node", BUILD_SCRIPT) { | ||
return Err(anyhow!("Cannot load bundling script: {e:?}")); | ||
} | ||
IS_BUNDLING_SCRIPT_LOADED.lock().unwrap().loaded(); | ||
} | ||
|
||
if let Err(e) = metacall::<MetacallNull>( | ||
BUNDLING_FUNC, | ||
[ | ||
serde_json::to_string(&self.targets)?, | ||
self.dist_path.to_str().unwrap().to_owned(), | ||
], | ||
) { | ||
return Err(anyhow!("Cannot running {BUNDLING_FUNC}(): {e:?}")); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use metacall::switch; | ||
#[test] | ||
fn it_works() { | ||
let _metacall = switch::initialize().unwrap(); | ||
WebBundler::new( | ||
&HashMap::from([( | ||
"pages/homes.tsx".to_owned(), | ||
"../../tests/web-app/src/pages/home.tsx".to_owned(), | ||
)]), | ||
"../../tests/web-app/dist", | ||
) | ||
.exec() | ||
.unwrap() | ||
} | ||
} | ||
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "metassr-bundler" | ||
version = "0.0.1-alpha" | ||
edition = "2021" | ||
description = "A simple crate is used for web bundling built on Metacall and Rspack." | ||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
anyhow = "1.0.86" | ||
lazy_static = "1.5.0" | ||
metacall = "0.4.1" | ||
metassr-utils = { version = "0.1.0", path = "../metassr-utils" } | ||
serde_json = "1.0.128" |
File renamed without changes.
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,98 @@ | ||
use anyhow::{anyhow, Result}; | ||
use lazy_static::lazy_static; | ||
use metacall::{loaders, metacall, MetacallNull}; | ||
use std::{collections::HashMap, ffi::OsStr, marker::Sized, path::Path, sync::Mutex}; | ||
|
||
lazy_static! { | ||
static ref IS_BUNDLING_SCRIPT_LOADED: Mutex<BundleSciptLoadingState> = | ||
Mutex::new(BundleSciptLoadingState::new()); | ||
} | ||
static BUILD_SCRIPT: &str = include_str!("./bundle.js"); | ||
const BUNDLING_FUNC: &str = "web_bundling"; | ||
|
||
/// A detector for if the bundling script `./bundle.js` is loaded or not. | ||
#[derive(Debug)] | ||
pub struct BundleSciptLoadingState(bool); | ||
|
||
impl BundleSciptLoadingState { | ||
pub fn new() -> Self { | ||
Self(false) | ||
} | ||
pub fn loaded(&mut self) { | ||
self.0 = true | ||
} | ||
pub fn is_loaded(&self) -> bool { | ||
self.0 | ||
} | ||
} | ||
|
||
impl Default for BundleSciptLoadingState { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
|
||
pub struct WebBundler<'a> { | ||
pub targets: HashMap<String, &'a Path>, | ||
pub dist_path: &'a Path, | ||
} | ||
|
||
impl<'a> WebBundler<'a> { | ||
pub fn new<S>(targets: &'a HashMap<String, String>, dist_path: &'a S) -> Self | ||
where | ||
S: AsRef<OsStr> + ?Sized, | ||
{ | ||
let targets: HashMap<String, &Path> = targets | ||
.iter() | ||
.map(|(k, v)| (k.into(), Path::new(v))) | ||
.collect(); | ||
|
||
Self { | ||
targets, | ||
dist_path: Path::new(dist_path), | ||
} | ||
} | ||
pub fn exec(&self) -> Result<()> { | ||
let mut guard = IS_BUNDLING_SCRIPT_LOADED.lock().unwrap(); | ||
if !guard.is_loaded() { | ||
if let Err(e) = loaders::from_memory("node", BUILD_SCRIPT) { | ||
return Err(anyhow!("Cannot load bundling script: {e:?}")); | ||
} | ||
guard.loaded(); | ||
} | ||
drop(guard); | ||
|
||
if let Err(e) = metacall::<MetacallNull>( | ||
BUNDLING_FUNC, | ||
[ | ||
serde_json::to_string(&self.targets)?, | ||
self.dist_path.to_str().unwrap().to_owned(), | ||
], | ||
) { | ||
return Err(anyhow!("Cannot running {BUNDLING_FUNC}(): {e:?}")); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use metacall::switch; | ||
#[test] | ||
fn it_works() { | ||
let _metacall = switch::initialize().unwrap(); | ||
WebBundler::new( | ||
&HashMap::from([( | ||
"pages/homes.tsx".to_owned(), | ||
"../../tests/web-app/src/pages/home.tsx".to_owned(), | ||
)]), | ||
"../../tests/web-app/dist", | ||
) | ||
.exec() | ||
.unwrap() | ||
} | ||
} |