Struct NpmBuild

Source
pub struct NpmBuild { /* private fields */ }
Expand description

Executes npm commands before collecting resources.

Example usage: Add build.rs with call to bundle resources:

use static_files::NpmBuild;

fn main() {
    NpmBuild::new("./web")
        .install().unwrap() // runs npm install
        .run("build").unwrap() // runs npm run build
        .target("./web/dist")
        .to_resource_dir()
        .build().unwrap();
}

Include generated code in main.rs:

include!(concat!(env!("OUT_DIR"), "/generated.rs"));

Implementations§

Source§

impl NpmBuild

Source

pub fn new<P: AsRef<Path>>(package_json_dir: P) -> Self

Source

pub fn executable(self, executable: &str) -> Self

Allow the user to set their own npm-like executable (like yarn, for instance)

Source

pub fn change_detection(self) -> Self

Generates change detection instructions.

It includes package.json directory, ignores by default node_modules, package.json and package-lock.json and target directory. Each time npm changes timestamps on these files, so if we do not ignore them - it runs npm each time. It is recommended to put your dist files one level deeper. For example, if you have web with package.json and dist just below that, you better generate you index.html somewhere in web\dist\sub_path\index.html. Reason is the same, npm touches dist each time and it touches the parent directory which in its turn triggers the build each time. For complete example see: Angular Router Sample. If default behavior does not work for you, you can use change-detection directly.

Source

pub fn install(self) -> Result<Self>

Executes npm install.

Source

pub fn run(self, cmd: &str) -> Result<Self>

Executes npm run CMD.

Source

pub fn target<P: AsRef<Path>>(self, target_dir: P) -> Self

Sets target (default is node_modules).

Source

pub fn stderr<S: Into<Stdio>>(self, stdio: S) -> Self

Sets stderr for the next command.

You should set it again, if you need also redirect output for the next command.

Source

pub fn stdout<S: Into<Stdio>>(self, stdio: S) -> Self

Sets stdout for the next command.

You should set it again, if you need also redirect output for the next command.

Source

pub fn to_resource_dir(self) -> ResourceDir

Converts to ResourceDir.

Trait Implementations§

Source§

impl Debug for NpmBuild

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for NpmBuild

Source§

fn default() -> NpmBuild

Returns the “default value” for a type. Read more
Source§

impl From<NpmBuild> for ResourceDir

Source§

fn from(npm_build: NpmBuild) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

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, 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, 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.