pub struct Scripting { /* private fields */ }Expand description
With this you register your script sources and global/“merged” scripts for the whole page
You usually only use it during bundle initialization. There are already handlers for /scripting/path_to_your_script.js and /scripting/script.js.
Implementations§
Source§impl Scripting
impl Scripting
pub fn try_new() -> Result<Self>
Sourcepub fn register_embedded_scripts<E: RustEmbed + Send + Sync + 'static>(
&mut self,
)
pub fn register_embedded_scripts<E: RustEmbed + Send + Sync + 'static>( &mut self, )
Registers a script from a rust_embed struct
§Example
use quokka::state::Scripting;
#[derive(rust_embed::RustEmbed)]
#[folder = "test/scripts"]
#[include = "*.js"]
struct Scripts;
let mut scripting = Scripting::try_new().unwrap();
assert!(scripting.get_script("test.js").is_none());
scripting.register_embedded_scripts::<Scripts>();
assert_eq!(scripting.get_script("test.js").unwrap(), "console.log(\"Hello World\");\n");Sourcepub fn add_merged_script(&mut self, script: &str)
pub fn add_merged_script(&mut self, script: &str)
Adds a script to the default group. Same as Scripting.add_merged_script_group("default", path)
Sourcepub fn add_merged_script_group(&mut self, group: &str, script: &str)
pub fn add_merged_script_group(&mut self, group: &str, script: &str)
Adds a global script.
The merged scripts are supposed to be loaded on all pages. Adding a big or a lot of scripts will impact the sites performance and reduce the user experiance. Use with caution.
§Note
The script name has to be a valid import part. Relative paths in the current
directory need to start with a ./. An alphanumeric “word” will be seen as
an item of an import map more details here
§Example
use quokka::state::Scripting;
#[derive(rust_embed::RustEmbed)]
#[folder = "test/scripts"]
#[include = "*.js"]
struct Scripts;
let mut scripting = Scripting::try_new().unwrap();
assert!(scripting.get_merged_script().is_empty());
scripting.add_merged_script_group("default", "./test.js");
assert_eq!(scripting.get_merged_script(), "import \"./test.js\";\n");Sourcepub fn get_script(&self, path: &str) -> Option<String>
pub fn get_script(&self, path: &str) -> Option<String>
Gets a script by its path
§Example
use quokka::state::Scripting;
#[derive(rust_embed::RustEmbed)]
#[folder = "test/scripts"]
#[include = "*.js"]
struct Scripts;
let mut scripting = Scripting::try_new().unwrap();
assert!(scripting.get_script("test.js").is_none());
scripting.register_embedded_scripts::<Scripts>();
assert_eq!(scripting.get_script("test.js").unwrap(), "console.log(\"Hello World\");\n");Sourcepub fn get_merged_script(&self) -> String
pub fn get_merged_script(&self) -> String
Gets the default merged scripts. Same as Scripting.get_merged_script_group("default")
Sourcepub fn get_merged_script_group(&self, group: &str) -> String
pub fn get_merged_script_group(&self, group: &str) -> String
Gets the merged script. Merged scripts are supposed to be used on all sides. The merged script will rely on ECMAScript imports to get all the required scripts.
§Example
use quokka::state::Scripting;
#[derive(rust_embed::RustEmbed)]
#[folder = "test/scripts"]
#[include = "*.js"]
struct Scripts;
let mut scripting = Scripting::try_new().unwrap();
assert!(scripting.get_merged_script().is_empty());
scripting.add_merged_script("test.js");
assert_eq!(scripting.get_merged_script(), "import \"test.js\";\n");Trait Implementations§
Source§impl FromRef<DefaultState> for Scripting
impl FromRef<DefaultState> for Scripting
Source§fn from_ref(state: &DefaultState) -> Self
fn from_ref(state: &DefaultState) -> Self
Source§impl ProvideState<Scripting> for DefaultState
impl ProvideState<Scripting> for DefaultState
Source§impl ProvideStateRef<Scripting> for DefaultState
impl ProvideStateRef<Scripting> for DefaultState
fn provide_ref(&self) -> &Scripting
fn provide_mut(&mut self) -> &mut Scripting
Source§impl TryFromModule for Scripting
impl TryFromModule for Scripting
Auto Trait Implementations§
impl Freeze for Scripting
impl !RefUnwindSafe for Scripting
impl Send for Scripting
impl Sync for Scripting
impl Unpin for Scripting
impl !UnwindSafe for Scripting
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more