Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ts-bridge/resolver

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ts-bridge/resolver

An implementation of the Node.js module resolution algorithm.

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
20
decreased by-82.3%
Maintainers
1
Weekly downloads
 
Created
Source

@ts-bridge/resolver

An implementation of the Node.js module resolution algorithm as defined in the specification here.

Why?

There are several tools out there for resolving modules, such as enhanced-resolve, or import-meta-resolve, or even Node.js' own require.resolve or import.meta.resolve functions. Unfortunately we cannot use these in TS Bridge, since the resolution logic has some specific requirements:

  • TS Bridge needs to know the format of a resolved module, i.e., whether it is a CommonJS module, ES module, or something else.
    • This is not trivial as Node.js has a lot of edge cases when determining the format. Take a look at the ESM_FILE_FORMAT function in the specification if you're curious.
  • TypeScript's compiler API is completely synchronous, so the module resolution needs to be synchronous as well.

To ensure correct behaviour, the best solution seemed to reimplement the algorithm as used by Node.js.

Usage

[!NOTE] Usage outside of TS Bridge is possible, but not recommended. Consider using a library like enhanced-resolve or import-meta-resolve instead.

[!NOTE] This is an ESM-only module. It cannot be used from CommonJS.

resolve

This library exports a single function, resolve, which can be used to resolve a package specifier to its path and format (e.g., module, commonjs).

It takes the following arguments:

NameTypeOptionalDescription
packageSpecifierstringNoThe specifier to resolve, e.g., @ts-bridge/resolver, or some-module/sub-path.
parentUrlstring | URLNoThe URL to start resolving from. This can be an (absolute) path, or instance of URL.
fileSystemFileSystemInterfaceYesThe file system to use for the resolution. This is mainly used for testing purposes, but can be used to use the resolver on custom file systems.
Example
import { resolve } from '@ts-bridge/resolver';

const { path, format } = resolve('some-es-module/sub-path', import.meta.url);
console.log(path); // "/path/to/node_modules/some-es-module/dist/sub-path.mjs"
console.log(format); // "module"

Keywords

FAQs

Package last updated on 11 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc