Socket
Socket
Sign inDemoInstall

tsconfig-paths

Package Overview
Dependencies
8
Maintainers
2
Versions
50
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.0 to 1.1.0

lib/package-reader.js

11

CHANGELOG.md

@@ -7,2 +7,13 @@ # Change Log

## [Unreleased]
- Nothing yet.
## [1.1.0]
### Added
- More explanation to readme.
- Match all extensions in require.extensions.
- Match longest pattern prefix first as typesript does.
- Match file in main field of package.json.
- Check for index files explicitly.
## [1.0.0] - 2016-12-30

@@ -9,0 +20,0 @@ - First stable release.

70

lib/match-path.js

@@ -10,2 +10,3 @@ "use strict";

};
var package_reader_1 = require("./package-reader");
var fs = require("fs");

@@ -29,4 +30,4 @@ var path = require("path");

}, {});
return function (sourceFileName, requestedModule, fileExists) {
return matchFromAbsolutePaths(absolutePaths, sourceFileName, requestedModule, fileExists);
return function (sourceFileName, requestedModule, readPackageJson, fileExists, extensions) {
return matchFromAbsolutePaths(absolutePaths, sourceFileName, requestedModule, readPackageJson, fileExists, extensions);
};

@@ -37,27 +38,30 @@ }

* Finds a path from tsconfig that matches a module load request.
* @param absolutePaths The paths to try as specified in tsconfig but resolved to absolute form.
* @param absolutePathMappings The paths to try as specified in tsconfig but resolved to absolute form.
* @param absoluteSourceFileName Absolute path to the file that requested the module.
* @param requestedModule The required module name.
* @param readPackageJson Function that returns parsed package.json if exists or undefined(useful for testing).
* @param fileExists Function that checks for existance of a file (useful for testing).
* @param extensions File extensions to probe for (useful for testing).
* @returns the found path, or undefined if no path was found.
*/
function matchFromAbsolutePaths(absolutePaths, absoluteSourceFileName, requestedModule, fileExists) {
function matchFromAbsolutePaths(absolutePathMappings, absoluteSourceFileName, requestedModule, readPackageJson, fileExists, extensions) {
if (readPackageJson === void 0) { readPackageJson = function (packageJsonPath) { return package_reader_1.readPackage(packageJsonPath); }; }
if (fileExists === void 0) { fileExists = fs.existsSync; }
if (extensions === void 0) { extensions = Object.keys(require.extensions); }
if (requestedModule[0] !== '.'
&& requestedModule[0] !== path.sep
&& absolutePaths
&& absolutePathMappings
&& absoluteSourceFileName
&& requestedModule
&& fileExists) {
for (var _i = 0, _a = Object.keys(absolutePaths); _i < _a.length; _i++) {
var key = _a[_i];
var starMatch = key === requestedModule ? '' : match_star_1.matchStar(key, requestedModule);
for (var _i = 0, _a = sortByLongestPrefix(Object.keys(absolutePathMappings)); _i < _a.length; _i++) {
var virtualPathPattern = _a[_i];
var starMatch = virtualPathPattern === requestedModule ? '' : match_star_1.matchStar(virtualPathPattern, requestedModule);
if (starMatch !== undefined) {
for (var _b = 0, _c = absolutePaths[key]; _b < _c.length; _b++) {
var absolutePathToTry = _c[_b];
var possibleModule = absolutePathToTry.replace('*', starMatch);
if (fileExists(possibleModule)
|| fileExists(possibleModule + '.ts')
|| fileExists(possibleModule + '.tsx')) {
return possibleModule;
for (var _b = 0, _c = absolutePathMappings[virtualPathPattern]; _b < _c.length; _b++) {
var physicalPathPattern = _c[_b];
var physicalPath = physicalPathPattern.replace('*', starMatch);
var resolved = tryResolve(physicalPath, fileExists, readPackageJson, extensions);
if (resolved) {
return resolved;
}

@@ -71,1 +75,37 @@ }

exports.matchFromAbsolutePaths = matchFromAbsolutePaths;
/**
* Tries to resolve a physical path by:
* 1. Check for files named as last part of request and ending in any of the extensions.
* 2. Check for file specified in package.json's main property.
* 3. Check for a file named index ending in any of the extensions.
* @param physicalPath The path to check.
* @param fileExists Function that checks for existance of a file (useful for testing).
* @param readPackageJson Function that returns parsed package.json if exists or undefined(useful for testing).
* @param extensions File extensions to probe for (useful for testing).
* @returns {string}
*/
function tryResolve(physicalPath, fileExists, readPackageJson, extensions) {
if (extensions.reduce(function (prev, curr) { return prev || fileExists(physicalPath + curr); }, false)) {
return physicalPath;
}
var packageJson = readPackageJson(path.join(physicalPath, "/package.json"));
if (packageJson && packageJson.main && fileExists(path.join(physicalPath, packageJson.main))) {
var file = path.join(physicalPath, packageJson.main);
return file.replace(path.extname(file), "");
}
var indexPath = path.join(physicalPath, "/index");
return extensions.reduce(function (prev, curr) { return prev || fileExists(indexPath + curr) && physicalPath; }, "");
}
/**
* Sort path patterns.
* If module name can be matches with multiple patterns then pattern with the longest prefix will be picked.
*/
function sortByLongestPrefix(arr) {
return arr
.concat()
.sort(function (a, b) { return getPrefixLength(b) - getPrefixLength(a); });
}
function getPrefixLength(pattern) {
var prefixLength = pattern.indexOf("*");
return pattern.substr(0, prefixLength).length;
}
{
"name": "tsconfig-paths",
"version": "1.0.0",
"version": "1.1.0",
"description": "Load node modules according to tsconfig paths, in run-time or via API.",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

@@ -7,2 +7,6 @@ # tsconfig-paths

Typescript by default mimics the Node.js runtime resolution strategy of modules. But it also allows the use of [path mapping](https://www.typescriptlang.org/docs/handbook/module-resolution.html) which allows arbitrary module paths (that doesn't start with "/" or ".") to be specified and mapped to physical paths in the filesystem. The typscript compiler can resolve these paths from `tsconfig` so it will compile OK. But if you then try to exeute the compiled files with node (or ts-node), it will only look in the `node_modules` folders all the way up to the root of the filesystem and thus will not find the modules specified by `paths` in `tsconfig`.
If you require this package's `tsconfig-paths/register` module it will read the `paths` from `tsconfig.json` and convert node's module loading calls into to physcial file paths that node can load.
## How to install

@@ -9,0 +13,0 @@

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc