moving some things around; fixed import maps not being deterministic

This commit is contained in:
liquidex 2024-07-21 10:21:00 +02:00
parent 7332a79c2c
commit 87ead3be17
13 changed files with 34 additions and 20 deletions

2
Cargo.lock generated
View file

@ -787,6 +787,7 @@ checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26"
dependencies = [
"equivalent",
"hashbrown",
"serde",
]
[[package]]
@ -1542,6 +1543,7 @@ dependencies = [
"handlebars",
"http-body",
"image",
"indexmap",
"log",
"pulldown-cmark",
"rand",

View file

@ -17,6 +17,7 @@ env_logger = "0.10.0"
handlebars = "4.3.7"
http-body = "1.0.0"
image = "0.24.8"
indexmap = { version = "2.2.6", features = ["serde"] }
log = { workspace = true }
pulldown-cmark = { version = "0.9.3", default-features = false }
rand = "0.8.5"

View file

@ -1,6 +1,4 @@
pub mod fix;
pub mod generate;
mod parse;
pub mod serve;
pub mod wc;

View file

@ -4,13 +4,13 @@ use anyhow::Context;
use treehouse_format::ast::Branch;
use walkdir::WalkDir;
use crate::state::{FileId, Source, Treehouse};
use super::{
use crate::{
parse::{self, parse_toml_with_diagnostics, parse_tree_with_diagnostics},
FixAllArgs, FixArgs, Paths,
state::{FileId, Source, Treehouse},
};
use super::{FixAllArgs, FixArgs, Paths};
struct Fix {
range: Range<usize>,
replacement: String,

View file

@ -5,7 +5,7 @@ use treehouse_format::ast::{Branch, Roots};
use walkdir::WalkDir;
use crate::{
cli::parse::parse_tree_with_diagnostics,
parse::parse_tree_with_diagnostics,
state::{Source, Treehouse},
};

View file

@ -2,7 +2,7 @@ use std::{collections::HashMap, ffi::OsStr, fs::File, io::BufReader, path::Path}
use anyhow::Context;
use image::ImageError;
use log::{debug, error, warn};
use log::{debug, warn};
use serde::{Deserialize, Serialize};
use walkdir::WalkDir;
@ -48,8 +48,8 @@ pub struct Config {
/// ```
pub redirects: Redirects,
/// JavaScript configuration.
pub javascript: JavaScript,
/// How the treehouse should be built.
pub build: Build,
/// Overrides for emoji filenames. Useful for setting up aliases.
///
@ -78,6 +78,12 @@ pub struct Redirects {
pub page: HashMap<String, String>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Build {
/// Configuration for how JavaScript is compiled.
pub javascript: JavaScript,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct JavaScript {
/// Import roots to generate in the project's import map.

View file

@ -17,7 +17,6 @@ use serde::Serialize;
use walkdir::WalkDir;
use crate::{
cli::parse::parse_tree_with_diagnostics,
config::{Config, ConfigDerivedData},
fun::seasons::Season,
html::{
@ -27,6 +26,7 @@ use crate::{
},
import_map::ImportMap,
include_static::IncludeStatic,
parse::parse_tree_with_diagnostics,
state::Source,
static_urls::StaticUrls,
tree::SemaRoots,
@ -41,6 +41,8 @@ struct Generator {
tree_files: Vec<PathBuf>,
}
struct Build {}
struct ParsedTree {
tree_path: String,
file_id: FileId,
@ -421,7 +423,8 @@ pub fn generate(paths: &Paths<'_>) -> anyhow::Result<(Config, Treehouse)> {
)?;
info!("generating import map");
let import_map = ImportMap::generate(config.site.clone(), &config.javascript.import_roots);
let import_map =
ImportMap::generate(config.site.clone(), &config.build.javascript.import_roots);
std::fs::write(
paths.target_dir.join("static/generated/import-map.json"),
serde_json::to_string_pretty(&import_map).context("could not serialize import map")?,

View file

@ -1,5 +1,6 @@
use std::{collections::HashMap, ffi::OsStr, path::PathBuf};
use std::{ffi::OsStr, path::PathBuf};
use indexmap::IndexMap;
use log::warn;
use serde::{Deserialize, Serialize};
use walkdir::WalkDir;
@ -8,7 +9,7 @@ use crate::static_urls::StaticUrls;
#[derive(Debug, Clone, Serialize)]
pub struct ImportMap {
pub imports: HashMap<String, String>,
pub imports: IndexMap<String, String>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
@ -20,7 +21,7 @@ pub struct ImportRoot {
impl ImportMap {
pub fn generate(base_url: String, import_roots: &[ImportRoot]) -> Self {
let mut import_map = ImportMap {
imports: HashMap::new(),
imports: IndexMap::new(),
};
for root in import_roots {
@ -59,6 +60,8 @@ impl ImportMap {
}
}
import_map.imports.sort_unstable_keys();
import_map
}
}

View file

@ -3,19 +3,21 @@ use std::path::Path;
use clap::Parser;
use cli::{
fix::{fix_all_cli, fix_file_cli},
generate::regenerate_or_report_error,
serve::serve,
wc::wc_cli,
Command, Paths, ProgramArgs,
};
use generate::regenerate_or_report_error;
use log::{error, info, warn};
mod cli;
mod config;
mod fun;
mod generate;
mod html;
mod import_map;
mod include_static;
mod parse;
mod paths;
mod state;
mod static_urls;

View file

@ -19,10 +19,11 @@
{{> components/_noscript.hbs }}
{{!--
NOTE: ~ because components/_tree.hbss must not include any extra indentation, because it may
NOTE: ~ because components/_tree.hbs must not include any extra indentation, because it may
contain pre elements which shouldn't be indented.
--}}
{{~> components/_tree.hbs }}
<th-emoji-tooltips></th-emoji-tooltips>
{{!-- For all pages except the one linked from the footer, include the footer icon. --}}
{{#if (ne page.tree_path "treehouse")}}

View file

@ -16,5 +16,3 @@
{{{ page.tree }}}
</main>
<th-emoji-tooltips></th-emoji-tooltips>

View file

@ -47,7 +47,7 @@ description = "a place on the Internet I like to call home"
[pics]
[javascript]
[build.javascript]
import_roots = [
{ name = "treehouse", path = "static/js" },
{ name = "tairu", path = "static/js/components/tairu" },