Skip to main content

PackageJson

Struct PackageJson 

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

Browser-favoring conditional-exports resolver over a parsed package.json.

Resolves the bare entry and subpaths to relative file paths using the condition order browsers want — browsermoduleimportdefault (never node/require) — with a modulebrowsermain fallback when there is no exports field. Enough of the Node resolution algorithm to generate an ES-module import map; not a general-purpose resolver.

Implementations§

Source§

impl PackageJson

Source

pub fn from_path(path: &Path) -> Result<Self, Box<dyn Error>>

Read and parse a package.json from disk.

Source

pub fn from_json(s: &str) -> Result<Self, Box<dyn Error>>

Parse a package.json from a JSON string (e.g. read out of a tarball).

Source

pub fn from_value(raw: Value) -> Self

Wrap an already-parsed JSON document.

Source

pub fn name(&self) -> Option<&str>

The "name" field, if present.

Source

pub fn version(&self) -> Option<&str>

The "version" field, if present.

Source

pub fn package_type(&self) -> PackageType

Module when "type": "module", else CommonJs (Node’s default).

Source

pub fn resolve_main(&self) -> Option<String>

Resolve the bare entry (the . export) to a relative path, for the browser.

Source

pub fn resolve_subpath(&self, subpath: &str) -> Option<String>

Resolve a subpath (e.g. "./helpers/decorate"; leading ./ optional) via the exports map — exact key first, then the longest "./…/*" pattern.

Source

pub fn entries(&self) -> Vec<Entry>

Enumerate the import-map-worthy entries: the bare entry, concrete subpaths, and "./*"-pattern prefixes.

Source

pub fn referenced_paths(&self) -> Vec<String>

Every relative path the resolution references (concrete targets + pattern directories) — used to keep the right files when vendoring, even under src/.

Trait Implementations§

Source§

impl Clone for PackageJson

Source§

fn clone(&self) -> PackageJson

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PackageJson

Source§

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

Formats the value using the given formatter. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.