@nodejs-loaders/alias
Advanced tools
| export function resolveAliases(url: string, context: import("module").LoadHookContext, nextLoad: (url: string, context?: Partial<import("module").LoadHookContext>) => import("module").LoadFnOutput | Promise<import("module").LoadFnOutput>): import("module").LoadFnOutput | Promise<import("module").LoadFnOutput>; | ||
| export function readConfigFile(filename: any): Promise<void | AliasMap>; | ||
| export { resolveAlias as resolve }; | ||
| export type AliasMap = Map<string, string>; | ||
| declare function resolveAlias(url: string, context: import("module").LoadHookContext, nextLoad: (url: string, context?: Partial<import("module").LoadHookContext>) => import("module").LoadFnOutput | Promise<import("module").LoadFnOutput>): import("module").LoadFnOutput | Promise<import("module").LoadFnOutput>; | ||
| //# sourceMappingURL=alias.loader.d.mts.map |
| {"version":3,"file":"alias.loader.d.mts","sourceRoot":"","sources":["alias.loader.mjs"],"names":[],"mappings":"sHAiF83f,CAAC;AA1C/3f,wEAcC;;uBAGY,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC;qHAyB81f,CAAC"} |
| import { readFile } from 'node:fs/promises'; | ||
| import path from 'node:path'; | ||
| import { pathToFileURL, URL } from 'node:url'; | ||
| const projectRoot = pathToFileURL(`${process.cwd()}/`); | ||
| const aliases = await readConfigFile('tsconfig.json'); | ||
| if (!aliases) | ||
| console.warn( | ||
| 'Alias loader was registered but no "paths" were found in tsconfig.json', | ||
| 'This loader will behave as a noop (but you should probably remove it if you aren’t using it).', | ||
| ); | ||
| /** | ||
| * @type {import('node:module').LoadHook} | ||
| */ | ||
| function resolveAlias(specifier, ctx, next) { | ||
| return (aliases ? resolveAliases : next)(specifier, ctx, next); | ||
| } | ||
| export { resolveAlias as resolve }; | ||
| /** | ||
| * @type {import('node:module').LoadHook} | ||
| */ | ||
| export function resolveAliases(specifier, ctx, next) { | ||
| // biome-ignore format: https://github.com/biomejs/biome/issues/4799 | ||
| for (const [key, dest] of /** @type {AliasMap} */ (aliases)) { | ||
| if (specifier === key) { | ||
| return next(dest, ctx); | ||
| } | ||
| if (specifier.startsWith(key)) { | ||
| return next(specifier.replace(key, dest), ctx); | ||
| } | ||
| } | ||
| return next(specifier, ctx); | ||
| } | ||
| export function readConfigFile(filename) { | ||
| const filepath = path.join(projectRoot.pathname, filename); | ||
| return ( | ||
| readFile(filepath) | ||
| .then((contents) => contents.toString()) | ||
| .then((contents) => JSON.parse(contents)) | ||
| // Get the `compilerOptions.paths` object from the parsed JSON | ||
| .then((contents) => contents?.compilerOptions?.paths) | ||
| .then(buildAliasMaps) | ||
| .catch((err) => { | ||
| if (err.code !== 'ENOENT') throw err; | ||
| }) | ||
| ); | ||
| } | ||
| /** | ||
| * @typedef {Map<string, string>} AliasMap | ||
| */ | ||
| function buildAliasMaps(config) { | ||
| if (!config) return; | ||
| // biome-ignore format: https://github.com/biomejs/biome/issues/4799 | ||
| const aliases = /** @type {AliasMap} */ (new Map()); | ||
| for (const rawKey of Object.keys(config)) { | ||
| const alias = config[rawKey][0]; | ||
| const isPrefix = rawKey.endsWith('*'); | ||
| const key = isPrefix ? rawKey.slice(0, -1) /* strip '*' */ : rawKey; | ||
| const baseDest = isPrefix ? alias.slice(0, -1) /* strip '*' */ : alias; | ||
| const dest = | ||
| baseDest[0] === '/' || URL.canParse(baseDest) | ||
| ? baseDest | ||
| : new URL(baseDest, projectRoot).href; | ||
| aliases.set(key, dest); | ||
| } | ||
| return aliases; | ||
| } |
+1
-5
@@ -1,6 +0,2 @@ | ||
| export function resolveAliases(url: string, context: import("module").LoadHookContext, nextLoad: (url: string, context?: import("module").LoadHookContext) => import("module").LoadFnOutput | Promise<import("module").LoadFnOutput>): import("module").LoadFnOutput | Promise<import("module").LoadFnOutput>; | ||
| export function readConfigFile(filename: any): Promise<void | AliasMap>; | ||
| export { resolveAlias as resolve }; | ||
| export type AliasMap = Map<string, string>; | ||
| declare function resolveAlias(url: string, context: import("module").LoadHookContext, nextLoad: (url: string, context?: import("module").LoadHookContext) => import("module").LoadFnOutput | Promise<import("module").LoadFnOutput>): import("module").LoadFnOutput | Promise<import("module").LoadFnOutput>; | ||
| export * from "./alias.loader.mjs"; | ||
| //# sourceMappingURL=alias.d.mts.map |
+1
-1
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"alias.d.mts","sourceRoot":"","sources":["alias.mjs"],"names":[],"mappings":"sHAmF04Q,CAAC;AA1C34Q,wEAcC;;uBAGY,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC;qHAyB02Q,CAAC"} | ||
| {"version":3,"file":"alias.d.mts","sourceRoot":"","sources":["alias.mjs"],"names":[],"mappings":""} |
+5
-80
@@ -1,83 +0,8 @@ | ||
| import { readFile } from 'node:fs/promises'; | ||
| import path from 'node:path'; | ||
| import { pathToFileURL, URL } from 'node:url'; | ||
| import { isMainThread } from 'node:worker_threads'; | ||
| import module from 'node:module'; | ||
| import _get from 'lodash.get'; | ||
| const projectRoot = pathToFileURL(`${process.cwd()}/`); | ||
| const aliases = await readConfigFile('tsconfig.json'); | ||
| if (!aliases) | ||
| console.warn( | ||
| 'Alias loader was registered but no "paths" were found in tsconfig.json', | ||
| 'This loader will behave as a noop (but you should probably remove it if you aren’t using it).', | ||
| ); | ||
| /** | ||
| * @type {import('node:module').LoadHook} | ||
| */ | ||
| function resolveAlias(specifier, ctx, next) { | ||
| return (aliases ? resolveAliases : next)(specifier, ctx, next); | ||
| if (isMainThread && 'register' in module) { | ||
| module.register('./alias.loader.mjs', import.meta.url); | ||
| } | ||
| export { resolveAlias as resolve }; | ||
| /** | ||
| * @type {import('node:module').LoadHook} | ||
| */ | ||
| export function resolveAliases(specifier, ctx, next) { | ||
| // biome-ignore format: https://github.com/biomejs/biome/issues/4799 | ||
| for (const [key, dest] of /** @type {AliasMap} */ (aliases)) { | ||
| if (specifier === key) { | ||
| return next(dest, ctx); | ||
| } | ||
| if (specifier.startsWith(key)) { | ||
| return next(specifier.replace(key, dest), ctx); | ||
| } | ||
| } | ||
| return next(specifier, ctx); | ||
| } | ||
| export function readConfigFile(filename) { | ||
| const filepath = path.join(projectRoot.pathname, filename); | ||
| return ( | ||
| readFile(filepath) | ||
| .then((contents) => contents.toString()) | ||
| .then((contents) => JSON.parse(contents)) | ||
| // Get the `compilerOptions.paths` object from the parsed JSON | ||
| .then((contents) => _get(contents, 'compilerOptions.paths')) | ||
| .then(buildAliasMaps) | ||
| .catch((err) => { | ||
| if (err.code !== 'ENOENT') throw err; | ||
| }) | ||
| ); | ||
| } | ||
| /** | ||
| * @typedef {Map<string, string>} AliasMap | ||
| */ | ||
| function buildAliasMaps(config) { | ||
| if (!config) return; | ||
| // biome-ignore format: https://github.com/biomejs/biome/issues/4799 | ||
| const aliases = /** @type {AliasMap} */ (new Map()); | ||
| for (const rawKey of Object.keys(config)) { | ||
| const alias = config[rawKey][0]; | ||
| const isPrefix = rawKey.endsWith('*'); | ||
| const key = isPrefix ? rawKey.slice(0, -1) /* strip '*' */ : rawKey; | ||
| const baseDest = isPrefix ? alias.slice(0, -1) /* strip '*' */ : alias; | ||
| const dest = | ||
| baseDest[0] === '/' || URL.canParse(baseDest) | ||
| ? baseDest | ||
| : new URL(baseDest, projectRoot).href; | ||
| aliases.set(key, dest); | ||
| } | ||
| return aliases; | ||
| } | ||
| export * from './alias.loader.mjs'; |
+1
-4
| { | ||
| "version": "1.1.1", | ||
| "version": "2.0.0", | ||
| "name": "@nodejs-loaders/alias", | ||
@@ -28,5 +28,2 @@ "description": "Extend node to support TypeScript 'paths' via customization hooks.", | ||
| }, | ||
| "dependencies": { | ||
| "lodash.get": "^4.4.2" | ||
| }, | ||
| "repository": { | ||
@@ -33,0 +30,0 @@ "type": "git", |
6989
5.21%0
-100%8
60%72
7.46%- Removed
- Removed