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 = [ dependencies = [
"equivalent", "equivalent",
"hashbrown", "hashbrown",
"serde",
] ]
[[package]] [[package]]
@ -1542,6 +1543,7 @@ dependencies = [
"handlebars", "handlebars",
"http-body", "http-body",
"image", "image",
"indexmap",
"log", "log",
"pulldown-cmark", "pulldown-cmark",
"rand", "rand",

View file

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

View file

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

View file

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

View file

@ -5,7 +5,7 @@ use treehouse_format::ast::{Branch, Roots};
use walkdir::WalkDir; use walkdir::WalkDir;
use crate::{ use crate::{
cli::parse::parse_tree_with_diagnostics, parse::parse_tree_with_diagnostics,
state::{Source, Treehouse}, 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 anyhow::Context;
use image::ImageError; use image::ImageError;
use log::{debug, error, warn}; use log::{debug, warn};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use walkdir::WalkDir; use walkdir::WalkDir;
@ -48,8 +48,8 @@ pub struct Config {
/// ``` /// ```
pub redirects: Redirects, pub redirects: Redirects,
/// JavaScript configuration. /// How the treehouse should be built.
pub javascript: JavaScript, pub build: Build,
/// Overrides for emoji filenames. Useful for setting up aliases. /// Overrides for emoji filenames. Useful for setting up aliases.
/// ///
@ -78,6 +78,12 @@ pub struct Redirects {
pub page: HashMap<String, String>, 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)] #[derive(Debug, Clone, Deserialize, Serialize)]
pub struct JavaScript { pub struct JavaScript {
/// Import roots to generate in the project's import map. /// Import roots to generate in the project's import map.

View file

@ -17,7 +17,6 @@ use serde::Serialize;
use walkdir::WalkDir; use walkdir::WalkDir;
use crate::{ use crate::{
cli::parse::parse_tree_with_diagnostics,
config::{Config, ConfigDerivedData}, config::{Config, ConfigDerivedData},
fun::seasons::Season, fun::seasons::Season,
html::{ html::{
@ -27,6 +26,7 @@ use crate::{
}, },
import_map::ImportMap, import_map::ImportMap,
include_static::IncludeStatic, include_static::IncludeStatic,
parse::parse_tree_with_diagnostics,
state::Source, state::Source,
static_urls::StaticUrls, static_urls::StaticUrls,
tree::SemaRoots, tree::SemaRoots,
@ -41,6 +41,8 @@ struct Generator {
tree_files: Vec<PathBuf>, tree_files: Vec<PathBuf>,
} }
struct Build {}
struct ParsedTree { struct ParsedTree {
tree_path: String, tree_path: String,
file_id: FileId, file_id: FileId,
@ -421,7 +423,8 @@ pub fn generate(paths: &Paths<'_>) -> anyhow::Result<(Config, Treehouse)> {
)?; )?;
info!("generating import map"); 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( std::fs::write(
paths.target_dir.join("static/generated/import-map.json"), paths.target_dir.join("static/generated/import-map.json"),
serde_json::to_string_pretty(&import_map).context("could not serialize import map")?, 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 log::warn;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use walkdir::WalkDir; use walkdir::WalkDir;
@ -8,7 +9,7 @@ use crate::static_urls::StaticUrls;
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub struct ImportMap { pub struct ImportMap {
pub imports: HashMap<String, String>, pub imports: IndexMap<String, String>,
} }
#[derive(Debug, Clone, Deserialize, Serialize)] #[derive(Debug, Clone, Deserialize, Serialize)]
@ -20,7 +21,7 @@ pub struct ImportRoot {
impl ImportMap { impl ImportMap {
pub fn generate(base_url: String, import_roots: &[ImportRoot]) -> Self { pub fn generate(base_url: String, import_roots: &[ImportRoot]) -> Self {
let mut import_map = ImportMap { let mut import_map = ImportMap {
imports: HashMap::new(), imports: IndexMap::new(),
}; };
for root in import_roots { for root in import_roots {
@ -59,6 +60,8 @@ impl ImportMap {
} }
} }
import_map.imports.sort_unstable_keys();
import_map import_map
} }
} }

View file

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

View file

@ -19,10 +19,11 @@
{{> components/_noscript.hbs }} {{> 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. contain pre elements which shouldn't be indented.
--}} --}}
{{~> components/_tree.hbs }} {{~> components/_tree.hbs }}
<th-emoji-tooltips></th-emoji-tooltips>
{{!-- For all pages except the one linked from the footer, include the footer icon. --}} {{!-- For all pages except the one linked from the footer, include the footer icon. --}}
{{#if (ne page.tree_path "treehouse")}} {{#if (ne page.tree_path "treehouse")}}

View file

@ -16,5 +16,3 @@
{{{ page.tree }}} {{{ page.tree }}}
</main> </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] [pics]
[javascript] [build.javascript]
import_roots = [ import_roots = [
{ name = "treehouse", path = "static/js" }, { name = "treehouse", path = "static/js" },
{ name = "tairu", path = "static/js/components/tairu" }, { name = "tairu", path = "static/js/components/tairu" },