vite_rust

Struct Vite

source
pub struct Vite { /* private fields */ }

Implementations§

source§

impl Vite

source

pub async fn new<'b>(config: ViteConfig<'b>) -> Result<Vite, ViteError>

Creates a new Vite instance.

§Arguments
  • config - a [ViteConfig<'_>] instance.
§Errors

Returns Err if the given manifest path is not valid.

§Example
use vite_rust::{Vite, ViteConfig};
 
#[tokio::main]
async fn main() {
    let mut vite_config = ViteConfig::new_with_defaults("tests/test-manifest.json");
    vite_config.entrypoints = Some(vec!["views/foo.js"]);
    vite_config.force_mode = Some(vite_rust::ViteMode::Manifest);
     
    let vite = Vite::new(vite_config).await.unwrap();
 
    let expected =
        r#"<link rel="stylesheet" href="assets/foo-5UjPuW-k.css" />
        <link rel="stylesheet" href="assets/shared-ChJ_j-JJ.css" />
        <script type="module" src="assets/foo-BRBmoGS9.js"></script>
        <link rel="modulepreload" href="assets/shared-B7PI925R.js" />"#;
 
    let expected = expected.replace("\t", "     ")
        .lines()
        .map(str::trim)
        .collect::<Vec::<&str>>()
        .join("\n");
     
    assert_eq!(vite.get_tags(), expected);
}
source

pub fn get_tags(&self) -> String

Generates assets HTML tags from manifest.json file.

§Panics

Might panic if the file doesn’t exist.

source

pub fn get_development_scripts(&self) -> String

Generates scripts and stylesheet link HTML tags referencing the entrypoints directly from the Vite dev-server.

source

pub fn get_resolved_vite_scripts(&self) -> String

Generates HTML tags considering the current ViteMode:

  • If Development mode, calls Vite::get_development_scripts() and Vite::get_hmr_script() and return a concatenation of their returns;
  • If Manifest mode, calls Vite::get_tags() and return the assets HTML tags.
source

pub fn get_hmr_script(&self) -> String

Returns a script tag referencing the Hot Module Reload client script from the Vite dev-server.

If ViteMode is set to Manifest, only an empty string is returned.

source

pub fn get_asset_url(&self, path: &str) -> String

Returns the bundled file by the given original file’s path. If it is not present in the manifest file, an empty string is returned.

§Arguments
  • path - the root-relative path to an asset file. E.g. “src/assets/react.svg”.
source

pub fn get_react_script(&self) -> String

Returns the react fast refresh script relative to the current Vite dev-server URL.

source

pub fn get_hash(&self) -> &str

Returns the current manifest.json file hash. Might be used for assets versioning.

The resultant string is a hex-encoded MD5 hash.

source

pub fn get_dev_server_url(&self) -> &str

Returns the Vite instance’s dev-server URL.

source

pub fn mode(&self) -> &ViteMode

Returns the current Vite instance’s mode.

Trait Implementations§

source§

impl Debug for Vite

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Vite

§

impl RefUnwindSafe for Vite

§

impl Send for Vite

§

impl Sync for Vite

§

impl Unpin for Vite

§

impl UnwindSafe for Vite

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

source§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more