NodePackageImporter
Hierarchy
- NodePackageImporter
Index
Constructors
Properties
Constructors
constructor
- new
Node Package Importer(entryPointDirectory?: string): NodePackageImporter -
The NodePackageImporter has an optional
entryPointDirectoryoption, which is the directory where the Node Package Importer should start when resolvingpkg:URLs in sources other than files on disk. This will be used as theparentURLin the Node Module Resolution algorithm.In order to be found by the Node Package Importer, a package will need to be inside a node_modules folder located in the
entryPointDirectory, or one of its parent directories, up to the filesystem root.Relative paths will be resolved relative to the current working directory. If a path is not provided, this defaults to the parent directory of the Node.js entrypoint. If that's not available, this will throw an error.
Parameters
-
OptionalentryPointDirectory: string
Returns NodePackageImporter
-
Properties
Private Readonly [node Package Importer Key]
Used to distinguish this type from any arbitrary object.
The built-in Node.js package importer. This loads pkg: URLs from node_modules according to the standard Node.js resolution algorithm.
A Node.js package importer is exposed as a class that can be added to the
importersoption.Writing Sass packages
Package authors can control what is exposed to their users through their
package.jsonmanifest. The recommended method is to add asassconditional export topackage.json.This allows a package user to write
@use "pkg:uicomponents"to load the file atnode_modules/uicomponents/src/scss/index.scss.The Node.js package importer supports the variety of formats supported by Node.js package entry points, allowing authors to expose multiple subpaths.
This allows a package user to write:
@use "pkg:uicomponents";to import the root export.@use "pkg:uicomponents/colors";to import the colors partial.@use "pkg:uicomponents/theme/purple";to import a purple theme.Note that while library users can rely on the importer to resolve partials, index files, and extensions, library authors must specify the entire file path in
exports.In addition to the
sasscondition, thestylecondition is also acceptable. Sass will match thedefaultcondition if it's a relevant file type, but authors are discouraged from relying on this. Notably, the key order matters, and the importer will resolve to the first value with a key that issass,style, ordefault, so you should always putdefaultlast.To help package authors who haven't transitioned to package entry points using the
exportsfield, the Node.js package importer provides several fallback options. If thepkg:URL does not have a subpath, the Node.js package importer will look for asassorstylekey at the root ofpackage.json.This allows a user to write
@use "pkg:uicomponents";to import theindex.scssfile.Finally, the Node.js package importer will look for an
indexfile at the package root, resolving partials and extensions. For example, if the file_index.scssexists in the package root ofuicomponents, a user can import that with@use "pkg:uicomponents";.If a
pkg:URL includes a subpath that doesn't have a match in package entry points, the Node.js importer will attempt to find that file relative to the package root, resolving for file extensions, partials and index files. For example, if the filesrc/sass/_colors.scssexists in theuicomponentspackage, a user can import that file using@use "pkg:uicomponents/src/sass/colors";.