Replace dummy manifest with fake include_asset function
This commit is contained in:
@@ -47,48 +47,6 @@ pub struct Manifest {
|
||||
inner: HashMap<Utf8PathBuf, ManifestEntry>,
|
||||
}
|
||||
|
||||
impl Manifest {
|
||||
/// Produce a sample manifest for use in reproducible sample renders.
|
||||
#[must_use]
|
||||
#[allow(clippy::missing_panics_doc)]
|
||||
pub fn sample() -> Self {
|
||||
let mut inner = HashMap::new();
|
||||
|
||||
for name in &[
|
||||
"src/shared.css",
|
||||
"src/templates.css",
|
||||
"src/main.tsx",
|
||||
"src/swagger.ts",
|
||||
] {
|
||||
inner.insert(
|
||||
name.parse().unwrap(),
|
||||
ManifestEntry {
|
||||
name: None,
|
||||
names: None,
|
||||
src: None,
|
||||
// Construct a fake but slightly plausible dummy asset name.
|
||||
file: name
|
||||
.replace('/', "__")
|
||||
.replace('.', "-XXXXX.")
|
||||
.replace(".tsx", ".js")
|
||||
.replace(".ts", ".js")
|
||||
.parse()
|
||||
.unwrap(),
|
||||
css: None,
|
||||
assets: None,
|
||||
is_entry: None,
|
||||
is_dynamic_entry: None,
|
||||
imports: None,
|
||||
dynamic_imports: None,
|
||||
integrity: None,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Manifest { inner }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
|
||||
enum FileType {
|
||||
Script,
|
||||
|
||||
@@ -30,7 +30,7 @@ use url::Url;
|
||||
pub fn register(
|
||||
env: &mut minijinja::Environment,
|
||||
url_builder: UrlBuilder,
|
||||
vite_manifest: ViteManifest,
|
||||
vite_manifest: Option<ViteManifest>,
|
||||
translator: Arc<Translator>,
|
||||
) {
|
||||
env.set_unknown_method_callback(minijinja_contrib::pycompat::unknown_method_callback);
|
||||
@@ -43,13 +43,17 @@ pub fn register(
|
||||
env.add_filter("parse_user_agent", filter_parse_user_agent);
|
||||
env.add_function("add_params_to_url", function_add_params_to_url);
|
||||
env.add_function("counter", || Ok(Value::from_object(Counter::default())));
|
||||
env.add_global(
|
||||
"include_asset",
|
||||
Value::from_object(IncludeAsset {
|
||||
url_builder: url_builder.clone(),
|
||||
vite_manifest,
|
||||
}),
|
||||
);
|
||||
if let Some(vite_manifest) = vite_manifest {
|
||||
env.add_global(
|
||||
"include_asset",
|
||||
Value::from_object(IncludeAsset {
|
||||
url_builder: url_builder.clone(),
|
||||
vite_manifest,
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
env.add_global("include_asset", Value::from_object(FakeIncludeAsset {}));
|
||||
}
|
||||
env.add_global(
|
||||
"translator",
|
||||
Value::from_object(TranslatorFunc { translator }),
|
||||
@@ -456,6 +460,30 @@ impl Object for IncludeAsset {
|
||||
}
|
||||
}
|
||||
|
||||
struct FakeIncludeAsset {}
|
||||
|
||||
impl std::fmt::Debug for FakeIncludeAsset {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("FakeIncludeAsset").finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for FakeIncludeAsset {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str("fake_include_asset")
|
||||
}
|
||||
}
|
||||
|
||||
impl Object for FakeIncludeAsset {
|
||||
fn call(self: &Arc<Self>, _state: &State, args: &[Value]) -> Result<Value, Error> {
|
||||
let (path,): (&str,) = from_args(args)?;
|
||||
|
||||
Ok(Value::from_safe_string(format!(
|
||||
"<!--- include_asset {path} -->"
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct Counter {
|
||||
count: AtomicUsize,
|
||||
|
||||
@@ -206,10 +206,12 @@ impl Templates {
|
||||
.await
|
||||
.map_err(TemplateLoadingError::ViteManifestIO)?;
|
||||
|
||||
serde_json::from_slice::<ViteManifest>(&raw_vite_manifest)
|
||||
.map_err(TemplateLoadingError::ViteManifest)?
|
||||
Some(
|
||||
serde_json::from_slice::<ViteManifest>(&raw_vite_manifest)
|
||||
.map_err(TemplateLoadingError::ViteManifest)?,
|
||||
)
|
||||
} else {
|
||||
ViteManifest::sample()
|
||||
None
|
||||
};
|
||||
|
||||
// Parse it
|
||||
|
||||
Reference in New Issue
Block a user