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

feat(html): support js template without child compiler #7704

Merged
merged 5 commits into from
Aug 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1239,8 +1239,9 @@ export interface RawHtmlRspackPluginOptions {
filename?: string
/** template html file */
template?: string
templateFn?: (data: string) => Promise<string>
templateContent?: string
templateParameters?: Record<string, string>
templateParameters?: boolean | Record<string, any> | ((params: string) => Promise<string>)
/** "head", "body" or "false" */
inject: "head" | "body" | "false"
/** path or `auto` */
Expand Down
43 changes: 40 additions & 3 deletions crates/rspack_binding_options/src/options/raw_builtins/raw_html.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
use std::collections::HashMap;
use std::str::FromStr;

use napi::bindgen_prelude::Either3;
use napi_derive::napi;
use rspack_napi::threadsafe_function::ThreadsafeFunction;
use rspack_plugin_html::config::HtmlInject;
use rspack_plugin_html::config::HtmlRspackPluginBaseOptions;
use rspack_plugin_html::config::HtmlRspackPluginOptions;
use rspack_plugin_html::config::HtmlScriptLoading;
use rspack_plugin_html::config::TemplateParameterFn;
use rspack_plugin_html::config::TemplateParameters;
use rspack_plugin_html::config::TemplateRenderFn;
use rspack_plugin_html::sri::HtmlSriHashFunction;

pub type RawHtmlScriptLoading = String;
pub type RawHtmlInject = String;
pub type RawHtmlSriHashFunction = String;
pub type RawHtmlFilename = String;

type RawTemplateRenderFn = ThreadsafeFunction<String, String>;

type RawTemplateParameter =
Either3<HashMap<String, String>, bool, ThreadsafeFunction<String, String>>;

#[derive(Debug)]
#[napi(object)]
#[napi(object, object_to_js = false)]
pub struct RawHtmlRspackPluginOptions {
/// emitted file name in output path
#[napi(ts_type = "string")]
pub filename: Option<RawHtmlFilename>,
/// template html file
pub template: Option<String>,
#[napi(ts_type = "(data: string) => Promise<string>")]
pub template_fn: Option<RawTemplateRenderFn>,
pub template_content: Option<String>,
pub template_parameters: Option<HashMap<String, String>>,
#[napi(ts_type = "boolean | Record<string, any> | ((params: string) => Promise<string>)")]
pub template_parameters: Option<RawTemplateParameter>,
/// "head", "body" or "false"
#[napi(ts_type = "\"head\" | \"body\" | \"false\"")]
pub inject: RawHtmlInject,
Expand Down Expand Up @@ -59,8 +72,32 @@ impl From<RawHtmlRspackPluginOptions> for HtmlRspackPluginOptions {
HtmlRspackPluginOptions {
filename: value.filename.unwrap_or_else(|| String::from("index.html")),
template: value.template,
template_fn: value.template_fn.map(|func| TemplateRenderFn {
inner: Box::new(move |data| {
let f = func.clone();
Box::pin(async move { f.call(data).await })
}),
}),
template_content: value.template_content,
template_parameters: value.template_parameters,
template_parameters: match value.template_parameters {
Some(parameters) => match parameters {
Either3::A(data) => TemplateParameters::Map(data),
Either3::B(enabled) => {
if enabled {
TemplateParameters::Map(Default::default())
} else {
TemplateParameters::Disabled
}
}
Either3::C(func) => TemplateParameters::Function(TemplateParameterFn {
inner: Box::new(move |data| {
let f = func.clone();
Box::pin(async move { f.call(data).await })
}),
}),
},
None => TemplateParameters::Map(Default::default()),
},
inject,
public_path: value.public_path,
script_loading,
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_plugin_html/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ default = []

[dependencies]
anyhow = { workspace = true }
futures = { workspace = true }
itertools = { workspace = true }
path-clean = { workspace = true }
rayon = { workspace = true }
Expand Down
43 changes: 41 additions & 2 deletions crates/rspack_plugin_html/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::{collections::HashMap, path::PathBuf, str::FromStr};

use futures::future::BoxFuture;
use rspack_core::{Compilation, PublicPath};
use rspack_error::Result;
use serde::Serialize;
use sugar_path::SugarPath;

Expand Down Expand Up @@ -62,13 +64,46 @@ impl FromStr for HtmlScriptLoading {
}
}

type TemplateParameterTsfn =
Box<dyn for<'a> Fn(String) -> BoxFuture<'static, Result<String>> + Sync + Send>;

pub struct TemplateParameterFn {
pub inner: TemplateParameterTsfn,
}

impl std::fmt::Debug for TemplateParameterFn {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TemplateParameterFn").finish()
}
}

#[derive(Debug)]
pub enum TemplateParameters {
Map(HashMap<String, String>),
Function(TemplateParameterFn),
Disabled,
}

#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct HtmlRspackPluginBaseOptions {
pub href: Option<String>,
pub target: Option<String>,
}

type TemplateRenderTsfn =
Box<dyn for<'a> Fn(String) -> BoxFuture<'static, Result<String>> + Sync + Send>;

pub struct TemplateRenderFn {
pub inner: TemplateRenderTsfn,
}

impl std::fmt::Debug for TemplateRenderFn {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TemplateRenderFn").finish()
}
}

#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct HtmlRspackPluginOptions {
Expand All @@ -77,8 +112,11 @@ pub struct HtmlRspackPluginOptions {
pub filename: String,
/// template html file
pub template: Option<String>,
#[serde(skip)]
pub template_fn: Option<TemplateRenderFn>,
pub template_content: Option<String>,
pub template_parameters: Option<HashMap<String, String>>,
#[serde(skip)]
pub template_parameters: TemplateParameters,
/// `head`, `body`, `false`
#[serde(default = "default_inject")]
pub inject: HtmlInject,
Expand Down Expand Up @@ -121,8 +159,9 @@ impl Default for HtmlRspackPluginOptions {
HtmlRspackPluginOptions {
filename: default_filename(),
template: None,
template_fn: None,
template_content: None,
template_parameters: None,
template_parameters: TemplateParameters::Map(Default::default()),
inject: default_inject(),
public_path: None,
script_loading: default_script_loading(),
Expand Down
Loading
Loading