pub struct Vite { /* private fields */ }
Implementations§
Source§impl Vite
impl Vite
Sourcepub async fn new(config: ViteConfig<'_>) -> Result<Vite, ViteError>
pub async fn new(config: ViteConfig<'_>) -> 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::default()
.set_manifest_path("tests/test-manifest.json")
.set_entrypoints(vec!["views/foo.js"])
.set_force_mode(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().unwrap(), expected);
}
Sourcepub fn get_development_scripts(&self) -> Result<String, ViteError>
pub fn get_development_scripts(&self) -> Result<String, ViteError>
Generates scripts and stylesheet link HTML tags referencing the entrypoints directly from the Vite dev-server.
Sourcepub fn get_resolved_vite_scripts(&self) -> Result<String, ViteError>
pub fn get_resolved_vite_scripts(&self) -> Result<String, ViteError>
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.
§Errors
Returns a ViteError
instance if mode is Manifest
and there is no Manifest.
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) -> Result<String, ViteError>
pub fn get_asset_url(&self, path: &str) -> Result<String, ViteError>
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) -> Option<&str>
pub fn get_hash(&self) -> Option<&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.