pub struct Vite { /* private fields */ }
Implementations§
source§impl Vite
impl Vite
sourcepub async fn new<'b>(config: ViteConfig<'b>) -> Result<Vite, ViteError>
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);
}
sourcepub fn get_development_scripts(&self) -> String
pub fn get_development_scripts(&self) -> String
Generates scripts and stylesheet link HTML tags referencing the entrypoints directly from the Vite dev-server.
sourcepub fn get_resolved_vite_scripts(&self) -> String
pub fn get_resolved_vite_scripts(&self) -> String
Generates HTML tags considering the current ViteMode
:
- If
Development
mode, callsVite::get_development_scripts()
andVite::get_hmr_script()
and return a concatenation of their returns; - If
Manifest
mode, callsVite::get_tags()
and return the assets HTML tags.
sourcepub fn get_hmr_script(&self) -> String
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.
sourcepub fn get_asset_url(&self, path: &str) -> String
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”.
sourcepub fn get_react_script(&self) -> String
pub fn get_react_script(&self) -> String
Returns the react fast refresh script relative to the current Vite dev-server URL.
sourcepub fn get_hash(&self) -> &str
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.
sourcepub fn get_dev_server_url(&self) -> &str
pub fn get_dev_server_url(&self) -> &str
Returns the Vite instance’s dev-server URL.