Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add data-target-path to more steps of the pipeline #726

Merged
merged 6 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions examples/target-path/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<title>Trunk | target-path</title>

<link data-trunk rel="scss" href="src/index.scss"/>
<link data-trunk rel="css" href="src/app.css"/>
<link data-trunk data-no-minify rel="css" href="src/not_minified.css"/>
<link data-trunk rel="css" href="src/app.css" data-target-path="static"/>
<link data-trunk data-no-minify rel="css" href="src/not_minified.css" data-target-path="static"/>
<base data-trunk-public-url/>
</head>
<body>
Expand All @@ -17,8 +17,8 @@
<link data-trunk rel="copy-dir" href="assets" data-target-path="static"/>
<link data-trunk rel="copy-file" href="more-assets/rustacean-flat-happy.svg" data-target-path="static"/>

<script data-trunk src="src/script.js"></script>
<script data-trunk src="src/script.mjs" type="module"></script>
<script data-trunk src="src/script.js" data-target-path="static"></script>
<script data-trunk src="src/script.mjs" type="module" data-target-path="static"></script>

<table>
<tr>
Expand Down
15 changes: 9 additions & 6 deletions site/content/assets.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ This will typically look like: `<link data-trunk rel="{type}" href="{path}" ..ot
- In the future, Trunk will resolve local `@imports`, will handle minification (see [trunk#7](https://github.com/trunk-rs/trunk/issues/7)), 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/trunk-rs/trunk/issues/3) for more details.
- `data-integrity`: (optional) the `integrity` digest type for code & script resources. Defaults to plain `sha384`.
- `data-no-minify`: (optional) by default, CSS files are minified in `--release` mode (unless building with `--no-minification`). Setting this attribute disables minification for that particular file. Defaults to false.
- `data-target-path`: (optional) Path where the directory is placed inside the dist dir. If not present the directory is placed in the dist root. The path must be a relative path without `..`.

## tailwind

Expand All @@ -68,6 +69,7 @@ This will typically look like: `<link data-trunk rel="{type}" href="{path}" ..ot
✅ `rel="icon"`: Trunk will copy the icon image specified in the `href` attribute to the `dist` dir. This content is hashed for cache control.

- `data-integrity`: (optional) the `integrity` digest type for code & script resources. Defaults to plain `sha384`.
- `data-target-path`: (optional) Path where the directory is placed inside the dist dir. If not present the directory is placed in the dist root. The path must be a relative path without `..`.

## inline

Expand Down Expand Up @@ -107,6 +109,7 @@ This will typically look like: `<script data-trunk src="{path}" ..other options
Trunk will copy script files found in the source HTML without content modification. This content is hashed for cache control. The `src` attribute must be included in the script pointing to the script file to be processed.

- `data-no-minify`: (optional) by default, scripts are minified in `--release` mode (unless building with `--no-minification`). Setting this attribute disables minification for that particular file. Defaults to false.
- `data-target-path`: (optional) Path where the directory is placed inside the dist dir. If not present the directory is placed in the dist root. The path must be a relative path without `..`.

## JS Snippets

Expand All @@ -126,7 +129,7 @@ Trunk can automatically generate hashes of files and add the `integrity` attribu
application. This is enabled by default, but can be overridden using the `data-integrity` attribute. See the different
asset types.

The following value are available:
The following values are available:

* `none`
* `sha256`
Expand All @@ -153,11 +156,11 @@ At the relevant point for each stage, all hooks for that stage are spawned simul
## Trunk's build process

This is a brief overview of Trunk's build process for the purpose of describing when hooks are executed. Please note that the exact ordering may change in the future to add new features.
- Step 1 - Read and parse the HTML file.
- Step 2 - Produce a plan of all assets to be built.
- Step 3 - Build all assets in parallel.
- Step 4 - Finalize and write assets to staging directory.
- Step 5 - Write HTML to staging directory.
- Step 1 Read and parse the HTML file.
- Step 2 Produce a plan of all assets to be built.
- Step 3 Build all assets in parallel.
- Step 4 Finalize and write assets to staging directory.
- Step 5 Write HTML to staging directory.
- Step 6 - Replace `dist` directory contents with staging directory contents.

The hook stages correspond to this as follows:
Expand Down
19 changes: 8 additions & 11 deletions src/pipelines/copy_dir.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
//! Copy-dir asset pipeline.

use std::path::PathBuf;
use std::sync::Arc;

use super::{data_target_path, Attrs, TrunkAssetPipelineOutput, ATTR_HREF};
use crate::{
common::{copy_dir_recursive, target_path},
config::RtcBuild,
};
use anyhow::{Context, Result};
use nipper::Document;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::fs;
use tokio::task::JoinHandle;

use super::{Attrs, TrunkAssetPipelineOutput, ATTR_HREF};
use crate::common::{copy_dir_recursive, target_path};
use crate::config::RtcBuild;

/// A CopyDir asset pipeline.
pub struct CopyDir {
/// The ID of this pipeline's source HTML element.
Expand Down Expand Up @@ -42,10 +42,7 @@ impl CopyDir {
if !path.is_absolute() {
path = html_dir.join(path);
}
let target_path = attrs
.get("data-target-path")
.map(|val| val.parse())
.transpose()?;
let target_path = data_target_path(&attrs)?;

Ok(Self {
id,
Expand Down
16 changes: 8 additions & 8 deletions src/pipelines/copy_file.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
//! Copy-file asset pipeline.

use crate::common::target_path;
use crate::{
common::target_path,
config::RtcBuild,
pipelines::{
data_target_path, AssetFile, AssetFileType, Attrs, TrunkAssetPipelineOutput, ATTR_HREF,
},
};
use anyhow::{Context, Result};
use nipper::Document;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::task::JoinHandle;

use crate::config::RtcBuild;
use crate::pipelines::{AssetFile, AssetFileType, Attrs, TrunkAssetPipelineOutput, ATTR_HREF};

/// A CopyFile asset pipeline.
pub struct CopyFile {
/// The ID of this pipeline's source HTML element.
Expand Down Expand Up @@ -39,10 +42,7 @@ impl CopyFile {
path.extend(href_attr.split('/'));
let asset = AssetFile::new(&html_dir, path).await?;

let target_path = attrs
.get("data-target-path")
.map(|val| val.parse())
.transpose()?;
let target_path = data_target_path(&attrs)?;

Ok(Self {
id,
Expand Down
24 changes: 16 additions & 8 deletions src/pipelines/css.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
//! CSS asset pipeline.

use super::{AssetFile, AttrWriter, Attrs, TrunkAssetPipelineOutput, ATTR_HREF, ATTR_MINIFY};
use super::{
data_target_path, AssetFile, AttrWriter, Attrs, TrunkAssetPipelineOutput, ATTR_HREF,
ATTR_MINIFY,
};
use crate::{
common::target_path,
config::RtcBuild,
pipelines::AssetFileType,
processing::integrity::{IntegrityType, OutputDigest},
Expand All @@ -26,6 +30,8 @@ pub struct Css {
integrity: IntegrityType,
/// Whether to minify or not
minify: bool,
/// Optional target path inside the dist dir.
target_path: Option<PathBuf>,
}

impl Css {
Expand All @@ -49,13 +55,16 @@ impl Css {

let minify = attrs.get(ATTR_MINIFY).is_none();

let target_path = data_target_path(&attrs)?;

Ok(Self {
id,
cfg,
asset,
attrs,
integrity,
minify,
target_path,
})
}

Expand All @@ -71,18 +80,17 @@ impl Css {
let rel_path = crate::common::strip_prefix(&self.asset.path);
tracing::debug!(path = ?rel_path, "copying & hashing css");
let minify = self.cfg.release && self.minify && !self.cfg.no_minification;

let result_path =
target_path(&self.cfg.staging_dist, self.target_path.as_deref(), None).await?;

let file = self
.asset
.copy(
&self.cfg.staging_dist,
self.cfg.filehash,
minify,
AssetFileType::Css,
)
.copy(&result_path, self.cfg.filehash, minify, AssetFileType::Css)
.await?;
tracing::debug!(path = ?rel_path, "finished copying & hashing css");

let result_file = self.cfg.staging_dist.join(&file);
let result_file = result_path.join(&file);
let integrity = OutputDigest::generate(self.integrity, || std::fs::read(&result_file))
.with_context(|| {
format!(
Expand Down
16 changes: 13 additions & 3 deletions src/pipelines/icon.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Icon asset pipeline.

use super::{AssetFile, AttrWriter, Attrs, TrunkAssetPipelineOutput, ATTR_HREF};
use super::{data_target_path, AssetFile, AttrWriter, Attrs, TrunkAssetPipelineOutput, ATTR_HREF};
use crate::common::target_path;
use crate::config::RtcBuild;
use crate::pipelines::{AssetFileType, ImageType};
use crate::processing::integrity::{IntegrityType, OutputDigest};
Expand All @@ -21,6 +22,8 @@ pub struct Icon {
asset: AssetFile,
/// The required integrity setting
integrity: IntegrityType,
/// Optional target path inside the dist dir.
target_path: Option<PathBuf>,
}

impl Icon {
Expand All @@ -42,11 +45,14 @@ impl Icon {

let integrity = IntegrityType::from_attrs(&attrs, &cfg)?;

let target_path = data_target_path(&attrs)?;

Ok(Self {
id,
cfg,
asset,
integrity,
target_path,
})
}

Expand All @@ -66,17 +72,21 @@ impl Icon {
"image/png" => ImageType::Png,
_ => ImageType::Other,
};

let result_dir =
target_path(&self.cfg.staging_dist, self.target_path.as_deref(), None).await?;

let file = self
.asset
.copy(
&self.cfg.staging_dist,
&result_dir,
self.cfg.filehash,
self.cfg.release && !self.cfg.no_minification,
AssetFileType::Icon(image_type),
)
.await?;

let result_file = self.cfg.staging_dist.join(&file);
let result_file = result_dir.join(&file);
let integrity = OutputDigest::generate(self.integrity, || std::fs::read(&result_file))
.with_context(|| {
format!(
Expand Down
17 changes: 14 additions & 3 deletions src/pipelines/js.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! JS asset pipeline.

use super::{AssetFile, AttrWriter, Attrs, TrunkAssetPipelineOutput, ATTR_MINIFY, ATTR_SRC};
use super::{
data_target_path, AssetFile, AttrWriter, Attrs, TrunkAssetPipelineOutput, ATTR_MINIFY, ATTR_SRC,
};
use crate::common::target_path;
use crate::{
config::RtcBuild,
pipelines::AssetFileType,
Expand Down Expand Up @@ -28,6 +31,8 @@ pub struct Js {
module: bool,
/// Whether to minify or not
minify: bool,
/// Optional target path inside the dist dir.
target_path: Option<PathBuf>,
}

impl Js {
Expand All @@ -48,6 +53,7 @@ impl Js {
let integrity = IntegrityType::from_attrs(&attrs, &cfg)?;
let module = attrs.get("type").map(|s| s.as_str()) == Some("module");
let minify = attrs.get(ATTR_MINIFY).is_none();
let target_path = data_target_path(&attrs)?;

Ok(Self {
id,
Expand All @@ -57,6 +63,7 @@ impl Js {
attrs,
integrity,
minify,
target_path,
})
}

Expand All @@ -72,10 +79,14 @@ impl Js {
let rel_path = crate::common::strip_prefix(&self.asset.path);
tracing::debug!(path = ?rel_path, "copying & hashing js");
let minify = self.cfg.release && self.minify && !self.cfg.no_minification;

let result_dir =
target_path(&self.cfg.staging_dist, self.target_path.as_deref(), None).await?;

let file = self
.asset
.copy(
&self.cfg.staging_dist,
&result_dir,
self.cfg.filehash,
minify,
if self.module {
Expand All @@ -87,7 +98,7 @@ impl Js {
.await?;
tracing::debug!(path = ?rel_path, "finished copying & hashing js");

let result_file = self.cfg.staging_dist.join(&file);
let result_file = result_dir.join(&file);
let integrity = OutputDigest::generate(self.integrity, || std::fs::read(&result_file))
.with_context(|| {
format!(
Expand Down
9 changes: 9 additions & 0 deletions src/pipelines/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const ATTR_SRC: &str = "src";
const ATTR_TYPE: &str = "type";
const ATTR_REL: &str = "rel";
const ATTR_MINIFY: &str = "data-no-minify";
const ATTR_TARGET_PATH: &str = "data-target-path";
const SNIPPETS_DIR: &str = "snippets";
const TRUNK_ID: &str = "data-trunk-id";
const PNG_OPTIMIZATION_LEVEL: u8 = 6;
Expand Down Expand Up @@ -398,3 +399,11 @@ impl fmt::Display for AttrWriter<'_> {
Ok(())
}
}

/// Get the target path for an asset
fn data_target_path(attrs: &Attrs) -> Result<Option<PathBuf>> {
Ok(attrs
.get(ATTR_TARGET_PATH)
.map(|val| val.parse())
.transpose()?)
}