NodePackageImporter
Hierarchy
- NodePackageImporter
Index
Constructors
Properties
Constructors
constructor
- new
Node Package Importer(entryPointDirectory?: string): NodePackageImporter -
The NodePackageImporter has an optional
entryPointDirectory
option, 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 theparentURL
in 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
-
Optional
entryPointDirectory: 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
importers
option.Writing Sass packages
Package authors can control what is exposed to their users through their
package.json
manifest. The recommended method is to add asass
conditional 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
sass
condition, thestyle
condition is also acceptable. Sass will match thedefault
condition 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 putdefault
last.To help package authors who haven't transitioned to package entry points using the
exports
field, 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 asass
orstyle
key at the root ofpackage.json
.This allows a user to write
@use "pkg:uicomponents";
to import theindex.scss
file.Finally, the Node.js package importer will look for an
index
file at the package root, resolving partials and extensions. For example, if the file_index.scss
exists 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.scss
exists in theuicomponents
package, a user can import that file using@use "pkg:uicomponents/src/sass/colors";
.