Skip to content

Add build.static-files-dir config option #2755

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,8 @@ pub struct BuildConfig {
pub use_default_preprocessors: bool,
/// Extra directories to trigger rebuild when watching/serving
pub extra_watch_dirs: Vec<PathBuf>,
/// Directory where to store static files.
pub static_files_dir: Option<PathBuf>,
}

impl Default for BuildConfig {
Expand All @@ -505,6 +507,7 @@ impl Default for BuildConfig {
create_missing: true,
use_default_preprocessors: true,
extra_watch_dirs: Vec::new(),
static_files_dir: None,
}
}
}
Expand Down Expand Up @@ -872,6 +875,7 @@ mod tests {
create_missing: false,
use_default_preprocessors: true,
extra_watch_dirs: Vec::new(),
static_files_dir: None,
};
let rust_should_be = RustConfig { edition: None };
let playground_should_be = Playground {
Expand Down Expand Up @@ -1083,6 +1087,7 @@ mod tests {
create_missing: true,
use_default_preprocessors: true,
extra_watch_dirs: Vec::new(),
static_files_dir: None,
};

let html_should_be = HtmlConfig {
Expand Down
8 changes: 6 additions & 2 deletions src/renderer/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,12 +415,16 @@ impl Renderer for HtmlHandlebars {
}

if html_config.hash_files {
static_files.hash_files()?;
static_files.hash_files(ctx.config.build.static_files_dir.as_deref())?;
}

debug!("Copy static files");
if let Some(ref path) = ctx.config.build.static_files_dir {
fs::create_dir_all(path)
.with_context(|| "Unexpected error when constructing static files directory")?;
}
let resource_helper = static_files
.write_files(&destination)
.write_files(destination)
.with_context(|| "Unable to copy across static files")?;

handlebars.register_helper("resource", Box::new(resource_helper));
Expand Down
19 changes: 16 additions & 3 deletions src/renderer/html_handlebars/static_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,19 @@ impl StaticFiles {

/// Updates this [`StaticFiles`] to hash the contents for determining the
/// filename for each resource.
pub fn hash_files(&mut self) -> Result<()> {
pub fn hash_files(&mut self, static_folder: Option<&Path>) -> Result<()> {
use sha2::{Digest, Sha256};
use std::io::Read;

fn apply_static_folder(name: String, static_folder: Option<&Path>) -> String {
if let Some(static_folder) = static_folder {
if let Some(file_name) = name.split('/').last() {
return static_folder.join(file_name).display().to_string();
}
}
name
}

for static_file in &mut self.static_files {
match static_file {
StaticFile::Builtin {
Expand All @@ -187,7 +197,10 @@ impl StaticFiles {
&& !name.starts_with("FontAwesome/fonts/")
{
let hex = hex::encode(&Sha256::digest(data)[..4]);
let new_filename = format!("{}-{}.{}", name, hex, suffix);
let new_filename = apply_static_folder(
format!("{}-{}.{}", name, hex, suffix),
static_folder,
);
self.hash_map.insert(filename.clone(), new_filename.clone());
*filename = new_filename;
}
Expand Down Expand Up @@ -229,7 +242,7 @@ impl StaticFiles {
pub fn write_files(self, destination: &Path) -> Result<ResourceHelper> {
use crate::utils::fs::write_file;
use regex::bytes::{Captures, Regex};
// The `{{ resource "name" }}` directive in static resources look like
// The `{{ resource "name" }}` directive in static resources looks like
// handlebars syntax, even if they technically aren't.
static RESOURCE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r#"\{\{ resource "([^"]+)" \}\}"#).unwrap());
Expand Down
Loading