Skip to content

Commit 476dff2

Browse files
authored
Merge pull request #73 from thedodd/hacking-on-0.7.0
Major refactor of pipeline system to support next wave of features.
2 parents f0e72e5 + 109c934 commit 476dff2

29 files changed

+1254
-795
lines changed

.github/workflows/release.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ jobs:
107107
with:
108108
files: trunk-*/trunk-*
109109
body_path: RELEASE_LOG.md
110-
prerelease: true
110+
draft: true
111111
env:
112112
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
113113

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ changelog
33
This changelog follows the patterns described here: https://keepachangelog.com/en/1.0.0/.
44

55
## Unreleased
6+
- Made a lot of refactoring progress to be able to support [#28](https://github.com/thedodd/trunk/issues/28) & [#46](https://github.com/thedodd/trunk/issues/46). More work remains to be done, but the foundation to be able to knock these items out is now in place.
7+
8+
### changed
9+
- All assets which are to be processed by trunk must now be declared as HTML `link` elements as such: `<link data-trunk rel="rust|sass|css|icon|copy-file|..." data-attr0 data-attr1/>`. The links may appear anywhere in the HTML and Trunk will process them and replace them or delete them based on the associated pipeline's output. If the link element does not have the `data-trunk` attribute, it will not be processed.
10+
11+
### fixed
12+
- Fixed [#50](https://github.com/thedodd/trunk/issues/50): the ability to copy a file or an entire dir into the dist dir is now supported with two different pipeline types: `<link data-trunk rel="copy-file" href="target/file"/>` and `<link data-trunk rel="copy-dir" href="target/dir"/>` respectively.
13+
14+
### removed
15+
- The `manifest-path` option has been removed from all Trunk subcommands. The path to the `Cargo.toml` is now specified in the source HTML as `<link rel="rust" href="path/to/Cargo.toml"/>`. The `href="..."` attribute may be omitted, in which case Trunk will look for a `Cargo.toml` within the same directory as the source HTML. If the `href` attribute points to a directory, Trunk will look for the `Cargo.toml` file in that directory.
616

717
## 0.6.0
818
### added

Cargo.lock

Lines changed: 147 additions & 149 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "trunk"
3-
version = "0.6.0"
3+
version = "0.7.0"
44
edition = "2018"
55
description = "Build, bundle & ship your Rust WASM application to the web."
66
license = "MIT/Apache-2.0"
@@ -34,7 +34,7 @@ seahash = "4.0.1"
3434
serde = { version="1", features=["derive"] }
3535
structopt = "0.3.18"
3636
structopt-derive = "0.4.11"
37-
surf = "=2.0.0-alpha.7"
37+
surf = "2.0.0"
3838
tide = { version="0.13.0", features=["unstable"] }
3939
toml = "0.5.6"
4040

README.md

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,15 @@ Trunk is a WASM web application bundler for Rust. Trunk uses a simple, zero-conf
2323
### install
2424
First, install Trunk via one of the following options.
2525
```bash
26-
# Install via cargo.
27-
cargo install trunk
26+
# Install via homebrew on Mac, Linux or Windows (WSL).
27+
brew install trunk
2828

2929
# Install a release binary (great for CI).
3030
VERSION=v0.6.0
3131
wget -qO- https://github.com/thedodd/trunk/releases/download/${VERSION}/trunk-x86_64-unknown-linux-gnu.tar.gz | tar -xzf-
3232

33-
# Install via homebrew on Mac, Linux or Windows (WSL).
34-
# (coming soon)
35-
brew install trunk
33+
# Install via cargo.
34+
cargo install trunk
3635
```
3736
<small>Release binaries can be found on the [Github releases page](https://github.com/thedodd/trunk/releases).</small>
3837

@@ -55,7 +54,7 @@ Trunk uses a source HTML file to drive all asset building and bundling. Trunk al
5554
```html
5655
<html>
5756
<head>
58-
<link rel="stylesheet" href="index.scss"/>
57+
<link data-trunk rel="scss" href="path/to/index.scss"/>
5958
</head>
6059
</html>
6160
```
@@ -95,19 +94,36 @@ Trunk leverages Rust's powerful concurrency primitives for maximum build speeds
9594
`trunk config show` prints out Trunk's current config, before factoring in CLI arguments. Nice for testing & debugging.
9695

9796
## assets
97+
Declaring assets to be processed by Trunk is simple and extensible. All assets to be processed by Trunk must follow these three rules:
98+
- must be declared as a valid HTML `link` tag.
99+
- must have the attribute `data-trunk`.
100+
- must have the attribute `rel="{type}"`, where `{type}` is one of the asset types listed below.
101+
102+
This will typically look like: `<link data-trunk rel="{type}" href="{path}" ..other options here.. />`. Each asset type described below specifies the required and optional attributes for its asset type. All `<link data-trunk .../>` HTML elements will be replaced with the output HTML of the associated pipeline.
103+
104+
Currently supported asset types:
105+
-`rust`: Trunk will compile the specified Cargo project as the main WASM application. This is optional. If not specified, Trunk will look for a `Cargo.toml` in the parent directory of the source HTML file.
106+
- `href`: (optional) the path to the `Cargo.toml` of the Rust project. If a directory is specified, then Trunk will look for the `Cargo.toml` in the given directory. If no value is specified, then Trunk will look for a `Cargo.toml` in the parent directory of the source HTML file.
107+
- `data-bin`: (optional) the name of the binary to compile and use as the main WASM application. If the Cargo project has multiple binaries, this value will be required for proper functionality.
108+
-`sass`, `scss`: Trunk ships with a [built-in sass/scss compiler](https://github.com/compass-rs/sass-rs). Just link to your sass files from your source HTML, and Trunk will handle the rest. This content is hashed for cache control. The `href` attribute must be included in the link pointing to the sass/scss file to be processed.
109+
-`css`: Trunk will copy linked css files found in the source HTML without content modification. This content is hashed for cache control. The `href` attribute must be included in the link pointing to the css file to be processed.
110+
- In the future, Trunk will resolve local `@imports`, will handle minification (see [trunk#7](https://github.com/thedodd/trunk/issues/3)), and we may even look into a pattern where any CSS found in the source tree will be bundled, which would enable a nice zero-config "component styles" pattern. See [trunk#3](https://github.com/thedodd/trunk/issues/3) for more details.
111+
-`icon`: Trunk will copy the icon image specified in the `href` attribute to the `dist` dir. This content is hashed for cache control.
112+
-`copy-file`: Trunk will copy the file specified in the `href` attribute to the `dist` dir. This content is copied exactly, no hashing is performed.
113+
-`copy-dir`: Trunk will recursively copy the directory specified in the `href` attribute to the `dist` dir. This content is copied exactly, no hashing is performed.
114+
-`rust-worker`: (in-progress) Trunk will compile the specified Rust project as a WASM web worker. The following attributes are required:
115+
- `href`: (optional) the path to the `Cargo.toml` of the Rust project. If a directory is specified, then Trunk will look for the `Cargo.toml` in the given directory. If no value is specified, then Trunk will look for a `Cargo.toml` in the parent directory of the source HTML file.
116+
- `data-bin`: (optional) the name of the binary to compile and use as the web worker. If the Cargo project has multiple binaries, this value will be required for proper functionality.
117+
98118
Trunk is still a young project, and new asset types will be added as we move forward. Keep an eye on [trunk#3](https://github.com/thedodd/trunk/issues/3) for more information on planned asset types, implementation status, and please contribute to the discussion if you think something is missing.
99119

100-
Currently supported assets:
101-
-`sass`: Trunk ships with a [built-in sass/scss compiler](https://github.com/compass-rs/sass-rs). Just link to your sass files from your source HTML, and Trunk will handle the rest. This content is hashed for cache control.
102-
-`css`: Trunk will copy linked css files found in the source HTML without content modification. This content is hashed for cache control.
103-
- In the future, Trunk will resolve local `@imports`, will handle minification (see [trunk#7](https://github.com/thedodd/trunk/issues/3)), and we may even look into a pattern where any CSS found in the source tree will be bundled, which would enable a nice zero-config "component styles" pattern. See [trunk#3](https://github.com/thedodd/trunk/issues/3) for more details.
104-
-`icon`: Trunk will automatically copy referenced icons to the `dist` dir. This content is hashed for cache control.
105-
-`js snippets`: [wasm-bindgen JS snippets](https://rustwasm.github.io/docs/wasm-bindgen/reference/js-snippets.html) are automatically copied to the dist dir, hashed and ready to rock.
120+
### js snippets
121+
JS snippets generated from the [wasm-bindgen JS snippets feature](https://rustwasm.github.io/docs/wasm-bindgen/reference/js-snippets.html) are automatically copied to the dist dir, hashed and ready to rock. No additional setup is required. Just use the feature in your application, and Trunk will take care of the rest.
106122

107123
### images & other resources
108-
Images and other resource types can be copied into the `dist` dir by adding a link like this to your source HTML: `<link rel="trunk-dist" href="path/to/resource"/>` (note the `rel="trunk-dist"` attribute). This will cause Trunk to find the target resource, and copy it to the `dist` dir unmodified. No hashing will be applied. The link itself will be removed from the HTML.
124+
Images and other resource types can be copied into the `dist` dir by adding a link like this to your source HTML: `<link data-trunk rel="copy-file" href="path/to/image"/>`. Any normal file type is supported. This will cause Trunk to find the target resource, and copy it to the `dist` dir unmodified. No hashing will be applied. The link itself will be removed from the HTML. To copy an entire directory of assets/images, you can use the following HTML: `<link data-trunk rel="copy-dir" href="path/to/images-dir"/>`.
109125

110-
This will allow your WASM application to reference images directly from the `dist` dir, and Trunk will ensure that the images are available in the `dist` dir to be served. You will need to be sure to use the correct public URL in your code, which can be configured via the `--public-url` CLI flag to Trunk.
126+
This will allow your WASM application to reference images directly from the `dist` dir, and Trunk will ensure that the images are available in the `dist` dir to be served.
111127

112128
**NOTE:** as Trunk continues to mature, we will find better ways to include images and other resources. Hashing content for cache control is great, we just need to find a nice pattern to work with images referenced in Rust components. Please contribute to the discussion over in [trunk#9](https://github.com/thedodd/trunk/issues/9)! See you there.
113129

Trunk.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ target = "index.html"
77
release = false
88
# The output dir for all final assets.
99
dist = "dist"
10-
# Path to Cargo.toml.
11-
manifest = "Cargo.toml"
1210
# The public URL from which assets are to be served.
1311
public_url = "/"
1412

examples/yew/Trunk.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
[build]
22
target = "index.html"
33
dist = "dist"
4-
manifest = "Cargo.toml"

examples/yew/index.html

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1"/>
66
<title>Trunk | Yew | YBC</title>
77
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.0/css/bulma.min.css"/>
8-
<link rel="stylesheet" href="src/index.scss"/>
9-
<link rel="stylesheet" href="src/app.css"/>
10-
<link rel="icon" href="src/yew.svg"/>
11-
<link rel="trunk-dist" href="src/yew.svg"/>
8+
9+
<link data-trunk rel="scss" href="src/index.scss"/>
10+
<link data-trunk rel="css" href="src/app.css"/>
11+
<link data-trunk rel="icon" href="src/yew.svg"/>
12+
<link data-trunk rel="copy-file" href="src/yew.svg"/>
1213
<base data-trunk-public-url/>
1314
</head>
1415
<body>
16+
<link data-trunk rel="rust" href="Cargo.toml" data-bin="yew-example"/>
1517
</body>
1618
</html>

examples/yew/src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl Component for App {
2727
}
2828

2929
fn view(&self) -> Html {
30-
html!{
30+
html! {
3131
<>
3232
<ybc::Navbar
3333
classes="is-success"
@@ -97,7 +97,6 @@ impl Component for App {
9797
}
9898
}
9999

100-
101100
#[wasm_bindgen(inline_js = "export function snippetTest() { console.log('Hello from JS FFI!'); }")]
102101
extern "C" {
103102
fn snippetTest();

src/build.rs

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
//! Build system & asset pipelines.
22
3+
use std::path::PathBuf;
34
use std::sync::Arc;
45

56
use anyhow::Result;
67
use async_std::fs;
8+
use futures::channel::mpsc::Sender;
79
use indicatif::ProgressBar;
810

911
use crate::common::{BUILDING, ERROR, SUCCESS};
1012
use crate::config::RtcBuild;
11-
use crate::pipelines::cargo::CargoBuild;
12-
use crate::pipelines::html::HtmlPipeline;
13-
use crate::pipelines::wasmbg::WasmBindgen;
13+
use crate::pipelines::HtmlPipeline;
1414

1515
/// A system used for building a Rust WASM app & bundling its assets.
1616
///
@@ -21,11 +21,7 @@ use crate::pipelines::wasmbg::WasmBindgen;
2121
pub struct BuildSystem {
2222
/// Runtime config.
2323
cfg: Arc<RtcBuild>,
24-
/// Cargo build pipeline system.
25-
cargo_build_pipeline: Arc<CargoBuild>,
26-
/// WASM bindgen build pipeline system.
27-
wasm_bindgen_pipeline: Arc<WasmBindgen>,
28-
/// HTML build pipeline system.
24+
/// HTML build pipeline.
2925
html_pipeline: Arc<HtmlPipeline>,
3026
/// The build system progress bar for displaying the state of the build system overall.
3127
progress: ProgressBar,
@@ -36,18 +32,10 @@ impl BuildSystem {
3632
///
3733
/// Reducing the number of assumptions here should help us to stay flexible when adding new
3834
/// commands, rafctoring and the like.
39-
pub async fn new(cfg: Arc<RtcBuild>, progress: ProgressBar) -> Result<Self> {
40-
let mode_segment = if cfg.release { "release" } else { "debug" };
41-
let bindgen_out = Arc::new(cfg.manifest.metadata.target_directory.join("wasm-bindgen").join(mode_segment));
42-
43-
let cargo_build_pipeline = Arc::new(CargoBuild::new(cfg.clone(), progress.clone()));
44-
let wasm_bindgen_pipeline = Arc::new(WasmBindgen::new(cfg.clone(), bindgen_out, progress.clone()));
45-
let html_pipeline = Arc::new(HtmlPipeline::new(cfg.clone(), progress.clone())?);
46-
35+
pub async fn new(cfg: Arc<RtcBuild>, progress: ProgressBar, ignore_chan: Option<Sender<PathBuf>>) -> Result<Self> {
36+
let html_pipeline = Arc::new(HtmlPipeline::new(cfg.clone(), progress.clone(), ignore_chan)?);
4737
Ok(Self {
4838
cfg,
49-
cargo_build_pipeline,
50-
wasm_bindgen_pipeline,
5139
html_pipeline,
5240
progress,
5341
})
@@ -71,26 +59,20 @@ impl BuildSystem {
7159
Err(err) => {
7260
self.progress.set_prefix(&format!("{}", ERROR));
7361
self.progress.finish_with_message("error");
74-
self.progress.println(err.to_string());
7562
Err(err)
7663
}
7764
}
7865
}
7966

8067
async fn do_build(&mut self) -> Result<()> {
81-
// Spawn cargo build. It will run concurrently without polling.
82-
let cargo_handle = self.cargo_build_pipeline.clone().spawn();
68+
// TODO: delete the contents of the `dist/.current` dir (currently in flight elsewhere).
8369

8470
// Ensure the output dist directory is in place.
8571
fs::create_dir_all(self.cfg.dist.as_path()).await?;
8672

87-
// Spawn the wasm-bindgen call, it will await the cargo build.
88-
let wasmbg_handle = self.wasm_bindgen_pipeline.clone().spawn(cargo_handle);
89-
90-
// Spawn the source HTML pipeline. This will spawn all other asset pipelines derived from
91-
// the source HTML, and will await the cargo build & wasm-bindgen build in order to
92-
// generate the final HTML.
93-
self.html_pipeline.clone().spawn(wasmbg_handle).await?;
73+
// Spawn the source HTML pipeline. This will spawn all other pipelines derived from
74+
// the source HTML, and will ultimately generate and write the final HTML.
75+
self.html_pipeline.clone().spawn().await?;
9476
Ok(())
9577
}
9678
}

src/cmd/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub struct Build {
1818
impl Build {
1919
pub async fn run(self, config: Option<PathBuf>) -> Result<()> {
2020
let cfg = ConfigOpts::rtc_build(self.build, config).await?;
21-
let mut system = BuildSystem::new(cfg, spinner()).await?;
21+
let mut system = BuildSystem::new(cfg, spinner(), None).await?;
2222
system.build().await?;
2323
Ok(())
2424
}

src/cmd/clean.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::path::PathBuf;
22

3-
use anyhow::Result;
3+
use anyhow::{ensure, Result};
44
use async_process::{Command, Stdio};
55
use async_std::fs;
66
use structopt::StructOpt;
@@ -26,9 +26,7 @@ impl Clean {
2626
.stderr(Stdio::piped())
2727
.output()
2828
.await?;
29-
if !output.status.success() {
30-
eprintln!("{}", String::from_utf8_lossy(&output.stderr));
31-
}
29+
ensure!(output.status.success(), "{}", String::from_utf8_lossy(&output.stderr));
3230
}
3331
Ok(())
3432
}

src/common.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::path::PathBuf;
44

5-
use anyhow::{Context, Result};
5+
use anyhow::{anyhow, Context, Result};
66
use async_std::path::PathBuf as AsyncPathBuf;
77
use async_std::task::spawn_blocking;
88

@@ -24,7 +24,7 @@ pub fn parse_public_url(val: &str) -> String {
2424
/// A utility function to recursively copy a directory.
2525
pub async fn copy_dir_recursive(from_dir: PathBuf, to_dir: PathBuf) -> Result<()> {
2626
if !AsyncPathBuf::from(&from_dir).exists().await {
27-
return Ok(());
27+
return Err(anyhow!("directory can not be copied as it does not exist {:?}", &from_dir));
2828
}
2929
spawn_blocking(move || {
3030
let opts = fs_extra::dir::CopyOptions {

src/config/manifest.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::path::PathBuf;
1+
use std::path::Path;
22

33
use anyhow::{anyhow, Result};
44
use async_std::task::spawn_blocking;
@@ -16,16 +16,11 @@ pub struct CargoMetadata {
1616
}
1717

1818
impl CargoMetadata {
19-
/// Get the project's cargo metadata of the CWD, or of the project specified by the given manifest path.
20-
pub async fn new(manifest: &Option<PathBuf>) -> Result<Self> {
21-
// Fetch the cargo project's metadata.
19+
// Create a new instance from the Cargo.toml at the given path.
20+
pub async fn new(manifest: &Path) -> Result<Self> {
2221
let mut cmd = MetadataCommand::new();
23-
if let Some(manifest) = manifest.as_ref() {
24-
cmd.manifest_path(manifest);
25-
}
22+
cmd.manifest_path(manifest);
2623
let metadata = spawn_blocking(move || cmd.exec()).await?;
27-
28-
// Get a handle to this project's package info.
2924
let package = metadata
3025
.root_package()
3126
.cloned()

0 commit comments

Comments
 (0)