Skip to content

Commit

Permalink
Ensure JS snippets from wasm-bindgen are being copied to dist.
Browse files Browse the repository at this point in the history
closes #40
  • Loading branch information
thedodd committed Sep 16, 2020
1 parent 403d5d7 commit 7b61431
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 27 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@ changelog
This changelog follows the patterns described here: https://keepachangelog.com/en/1.0.0/.

## Unreleased

## 0.4.0
### added
- Added support for layered configuration via `Trunk.toml` & environment variables.
- Added an example `Trunk.toml` to the root of the repository showing all possible config values along with their defaults.

### changed
- README has been updated with details on how the config system works.
- Removed a fair amount of code duplication as part of the configuration feature.
- Trunk now exits with a non-zero code when an error takes place during execution.
- Added full release automation with optimized release binaries for Linux, MacOS & Windows (all x64).

### fixed
- Closed [#37](https://github.com/thedodd/trunk/issues/37): Trunk now exits with a non-zero code when an error takes place during execution.
- Closed [#40](https://github.com/thedodd/trunk/issues/40): Trunk is now copying JS snippets from wasm-bindgen into the dist dir as part of the standard build/watch/serve commands.

## 0.3.1
### fixed
Expand Down
55 changes: 31 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ async-std = { version="1.6.3", features=["attributes", "unstable"] }
cargo_metadata = "0.11.1"
console = "0.12.0"
envy = "0.4.1"
fs_extra = "1.2.0"
futures = "0.3.5"
indicatif = "0.15.0"
nipper = "0.1.8"
Expand Down
24 changes: 22 additions & 2 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use std::ffi::OsString;
use std::path::PathBuf;
use std::sync::Arc;

use anyhow::{anyhow, bail, ensure, Result};
use anyhow::{anyhow, bail, Context, ensure, Result};
use async_process::{Command, Stdio};
use async_std::fs;
use async_std::{fs, path};
use async_std::task::{spawn, spawn_blocking, JoinHandle};
use cargo_metadata::{Metadata, MetadataCommand, Package};
use console::Emoji;
Expand All @@ -18,6 +18,7 @@ use crate::config::RtcBuild;

const TRUNK_ID: &str = "__trunk-id";
const HREF_ATTR: &str = "href";
const SNIPPETS_DIR: &str = "snippets";

/// A system used for building a Rust WASM app & bundling its assets.
///
Expand Down Expand Up @@ -211,6 +212,7 @@ impl BuildSystem {
/// Spawn the wasm-bindgen build process.
fn spawn_wasm_bindgen_build(&self, file_name: String) -> JoinHandle<Result<WasmBindgenOutput>> {
let (dist, bindgen_out, app_target_wasm) = (self.cfg.dist.clone(), self.bindgen_out.clone(), self.app_target_wasm.clone());
let (snippets_dir_from, snippets_dir_to) = (self.bindgen_out.join(SNIPPETS_DIR), self.cfg.dist.join(SNIPPETS_DIR));

self.progress.set_message(&format!("{}starting wasm-bindgen build", Emoji("📦 ", "")));
spawn(async move {
Expand All @@ -228,6 +230,7 @@ impl BuildSystem {
.map_err(|err| anyhow!("error spawning wasm-bindgen build: {}", err))?
.output()
.await;

// Handle build results.
match build_result {
Ok(output) => {
Expand All @@ -237,6 +240,7 @@ impl BuildSystem {
}
Err(err) => return Err(anyhow!("error during wasm-bindgen build: {}", err)),
}

// Copy the generated WASM & JS loader to the dist dir, and generate the needed body
// for the output HTML.
let hashed_js_name = format!("{}.js", &file_name);
Expand All @@ -248,13 +252,29 @@ impl BuildSystem {
fs::copy(js_loader_path, js_loader_path_dist).await?;
fs::copy(wasm_path, wasm_path_dist).await?;

// Check for any snippets, and copy them over.
Self::copy_dir_recursive(snippets_dir_from, snippets_dir_to).await.with_context(|| "error copying snippets dir")?;

Ok(WasmBindgenOutput {
js_output: hashed_js_name,
wasm_output: hashed_wasm_name,
})
})
}

/// A utility function to recursively copy a directory.
async fn copy_dir_recursive(from_dir: PathBuf, to_dir: PathBuf) -> Result<()> {
if !path::PathBuf::from(&from_dir).exists().await {
return Ok(());
}
spawn_blocking(move || {
let opts = fs_extra::dir::CopyOptions{overwrite: true, content_only: true, ..Default::default()};
Ok(fs_extra::dir::copy(from_dir, to_dir, &opts)?)
})
.await
.map(|_| ())
}

/// Spawn asset building/bundling pipelines.
///
/// Assets are given an ID which corresponds to an ID added to the DOM. Once the processing
Expand Down

0 comments on commit 7b61431

Please sign in to comment.