module-lens
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -6,3 +6,9 @@ export interface ResolveOptions { | ||
} | ||
export declare function resolve(specifier: string, { sourceFileName, baseUrlDir, extensions, }: ResolveOptions): string | undefined; | ||
export declare type ResolveCategory = 'built-in' | 'absolute' | 'relative' | 'base-url' | 'node-modules' | 'none'; | ||
export interface ResolveWithCategoryResult { | ||
category: ResolveCategory; | ||
path: string | undefined; | ||
} | ||
export declare function resolveWithCategory(specifier: string, { sourceFileName, baseUrlDir, extensions, }: ResolveOptions): ResolveWithCategoryResult; | ||
export declare function resolve(specifier: string, options: ResolveOptions): string | undefined; | ||
export declare function isNodeBuiltIn(specifier: string): boolean; |
@@ -8,12 +8,20 @@ "use strict"; | ||
const BUILT_IN_MODULE_NAME_SET = new Set(module_1.default.builtinModules); | ||
function resolve(specifier, { sourceFileName, baseUrlDir, extensions = ['.js', '.jsx', '.ts', '.tsx'], }) { | ||
function resolveWithCategory(specifier, { sourceFileName, baseUrlDir, extensions = ['.js', '.jsx', '.ts', '.tsx'], }) { | ||
if (isNodeBuiltIn(specifier)) { | ||
return specifier; | ||
return { | ||
category: 'built-in', | ||
path: specifier, | ||
}; | ||
} | ||
if (/^\//.test(specifier)) { | ||
return Path.resolve(specifier); | ||
return { | ||
category: 'absolute', | ||
path: Path.resolve(specifier), | ||
}; | ||
} | ||
let sourceDir = Path.dirname(sourceFileName); | ||
if (/^\.{1,2}\//.test(specifier)) { | ||
return Path.join(sourceDir, specifier); | ||
return { | ||
category: 'relative', | ||
path: Path.join(Path.dirname(sourceFileName), specifier), | ||
}; | ||
} | ||
@@ -23,26 +31,45 @@ let [specifierFirstFragment] = /^[^/]+/.exec(specifier) || /* istanbul ignore next */ [undefined]; | ||
if (!specifierFirstFragment) { | ||
return undefined; | ||
return { | ||
category: 'none', | ||
path: undefined, | ||
}; | ||
} | ||
if (baseUrlDir) { | ||
let usingBaseUrl = false; | ||
// First segment is exactly the whole specifier, possibly a file name. | ||
if (specifierFirstFragment === specifier) { | ||
for (let extension of ['', ...extensions]) { | ||
usingBaseUrl = ['', ...extensions].some(extension => { | ||
let possiblePathUsingBaseUrl = Path.join(baseUrlDir, `${specifier}${extension}`); | ||
let possiblePathStats = _utils_1.gentleStat(possiblePathUsingBaseUrl); | ||
if (possiblePathStats && possiblePathStats.isFile()) { | ||
return Path.join(baseUrlDir, specifier); | ||
} | ||
} | ||
return !!possiblePathStats && possiblePathStats.isFile(); | ||
}); | ||
} | ||
let possibleDirUsingBaseUrl = Path.join(baseUrlDir, specifierFirstFragment); | ||
let possibleDirStats = _utils_1.gentleStat(possibleDirUsingBaseUrl); | ||
if (possibleDirStats && possibleDirStats.isDirectory()) { | ||
return Path.join(baseUrlDir, specifier); | ||
if (!usingBaseUrl) { | ||
let possibleDirUsingBaseUrl = Path.join(baseUrlDir, specifierFirstFragment); | ||
let possibleDirStats = _utils_1.gentleStat(possibleDirUsingBaseUrl); | ||
usingBaseUrl = !!possibleDirStats && possibleDirStats.isDirectory(); | ||
} | ||
if (usingBaseUrl) { | ||
return { | ||
category: 'base-url', | ||
path: Path.join(baseUrlDir, specifier), | ||
}; | ||
} | ||
} | ||
let nodeModulesParentDir = _utils_1.searchUpperDir(sourceDir, `node_modules/${specifierFirstFragment}`, _utils_1.isDirectorySearchFilter); | ||
let nodeModulesParentDir = _utils_1.searchUpperDir(Path.dirname(sourceFileName), `node_modules/${specifierFirstFragment}`, _utils_1.isDirectorySearchFilter); | ||
if (nodeModulesParentDir) { | ||
return Path.join(nodeModulesParentDir, 'node_modules', specifier); | ||
return { | ||
category: 'node-modules', | ||
path: Path.join(nodeModulesParentDir, 'node_modules', specifier), | ||
}; | ||
} | ||
return undefined; | ||
return { | ||
category: 'none', | ||
path: undefined, | ||
}; | ||
} | ||
exports.resolveWithCategory = resolveWithCategory; | ||
function resolve(specifier, options) { | ||
return resolveWithCategory(specifier, options).path; | ||
} | ||
exports.resolve = resolve; | ||
@@ -49,0 +76,0 @@ function isNodeBuiltIn(specifier) { |
{ | ||
"name": "module-lens", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Simple module specifier utilities.", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10309
171