0.2.1 • Published 2 years ago

esm-root-loader v0.2.1

Weekly downloads
4
License
MIT
Repository
github
Last release
2 years ago

ESM Root Loader

ESM Root Loader is a custom resolver implementation for the experimental loader hooks feature of Node.js ECMAScript modules.

Using this loader will enhance the default loader behavior to allow you to import from the root of your project, even if the import occurs from a file located in a subdirectory:

import config from '/config.js';

By default, the above code snippet will resolve a config.js file from the root of your project.

This can be especially useful in projects which run both in the browser and in Node.js, since browser imports on the root path can be leveraged to resolve from the root of a domain.

Installation

Install as a dependency using npm:

npm install esm-root-loader

Usage

Loaders can be defined as an argument when running node.

node --experimental-loader=esm-root-loader entry.js

Configuration

Default usage will resolve imports from the root of your project.

If needed, you can assign a custom root path using an esmRoot property in your project's package.json. This will be evaluated as relative to the root path of the project.

{
	"esmRoot": "./public"
}

You can also customize or omit the prefix for resolving specifiers from the root.

For example, to import without the leading slash, you can set esmRootPrefix to false in your project's package.json.

import config from 'config.js';
{
	"esmRootPrefix": false
}

You can also customize the prefix by assigning any string value to this property.

import config from '~config.js';
{
	"esmRootPrefix": "~"
}

These options are also configurable by environment variables ESM_ROOT and ESM_ROOT_PREFIX.

Versioning

This project follows SemVer.

To better align with the experimental status of the loader hooks, initial releases will follow major version zero until the feature stabilizes in Node.js. Minor versions on the zero major will always include breaking changes. Patch versions on the zero major will include bug fixes and backwards-compatible changes.

Versioning will proceed from 1.0.0 once the feature stabilizes in Node.js.

License

Copyright (c) 2022 Andrew Duthie

Released under the MIT License. See LICENSE.md.