{"id":19714756,"url":"https://github.com/gaetanlegac/ts-alias","last_synced_at":"2025-04-29T19:33:06.532Z","repository":{"id":57380451,"uuid":"412399796","full_name":"gaetanlegac/ts-alias","owner":"gaetanlegac","description":"Parse module aliases from tsconfig ; Apply / remove them from pathnames ; Generate config for webpack \u0026 module-alias.","archived":false,"fork":false,"pushed_at":"2023-02-11T09:52:04.000Z","size":27,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-18T08:02:14.760Z","etag":null,"topics":["alias","aliases","tsconfig","typescript","webpack"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gaetanlegac.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-10-01T09:08:55.000Z","updated_at":"2024-03-02T05:45:39.000Z","dependencies_parsed_at":"2024-06-19T13:26:24.846Z","dependency_job_id":"16bdd872-c34e-492d-b282-125574afbe95","html_url":"https://github.com/gaetanlegac/ts-alias","commit_stats":{"total_commits":24,"total_committers":1,"mean_commits":24.0,"dds":0.0,"last_synced_commit":"ed7d9254638ee963788fea4f916d76d02caf0691"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gaetanlegac%2Fts-alias","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gaetanlegac%2Fts-alias/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gaetanlegac%2Fts-alias/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gaetanlegac%2Fts-alias/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gaetanlegac","download_url":"https://codeload.github.com/gaetanlegac/ts-alias/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251569682,"owners_count":21610600,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["alias","aliases","tsconfig","typescript","webpack"],"created_at":"2024-11-11T22:35:38.346Z","updated_at":"2025-04-29T19:33:06.234Z","avatar_url":"https://github.com/gaetanlegac.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Typescript Aliases Parser\n\nA NodeJS library to parse, process and convert Typescript aliases from tsconfig.json.\n\n[![npm](https://img.shields.io/npm/v/ts-alias)](https://www.npmjs.com/package/ts-alias) [![npm](https://img.shields.io/npm/dw/ts-alias)](https://www.npmjs.com/package/ts-alias)\n\n## Use cases\n\nWe all agree that module aliases are useful to maintain a clean and readable code.\n\nBut it's frequent that, in large projects, you have to define your same aliases in multiple tools in order to ensure everything runs correctly.\n\nAlso, some tools doesn't support defining custom node_module paths for these aliases, which can be locking in some complex projects.\n\n**I wrote this library to avoid the redondancy between my tsconfig.js, Webpack and module-alias configuration in my projects.**\n\nI assumed that the tsconfig.js format is the most universal, and can be reliably converted to aliases list for other tools like [module-alias](https://github.com/ilearnio/module-alias) and the [Webpack](https://github.com/webpack/webpack) package.\n\nBy parsing your tsconfig, defining custom rules if necessary, and exporting them for your other aliasing tools, it lightens your maintenance and debugging work.\n\n**Are you using this module for another purpose ? Don't hesitate to create a PR so we can list it here!**\n\n-----------\n\n\u003cp align=\"center\"\u003e\n    This project helped you ? Let me know,\n    \u003ca target=\"_blank\" href=\"https://github.com/gaetanlegac/ts-alias/stargazers\"\u003e\u003cb\u003e⭐ Give it a Star :)\u003c/b\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n------------\n\n## Install\n\n```bash\nnpm i --save ts-alias\n```\n\n## Instanciate\n\nThe constructor loads the Typescript aliases in the memory.\nIt can be loaded by two different ways:\n\n### From a tsconfig.json file\n\n1. By default, it will search the tsconfig in the current working directory of the process (`process.cwd()`).\n\n```typescript\nimport Aliases from 'ts-alias';\nconst aliases = new Aliases();\n```\n\n2. You can specify in path of the directory containing the tsconfig with the `rootDir` option.\nThis path can be absolute, or relative (from the process working directory).\n\n```typescript\nconst aliases = new Aliases({ \n    rootDir: './packages/module-containing-a-tsconfig' \n});\n```\n\n3. It's possible to directly provide the tsconfig file path:\n\n```typescript\nconst aliases = new Aliases({ \n    rootDir: './packages/module-containing-a-tsconfig/tsconfig.json' \n});\n```\n\nAn Error will be throwed if rootDir doesn't exists.\n\n### From an AliasList object\n\nIf for any reason, you already loaded the tsconfig aliases in memory, you can provide them via the `aliases` option:\n\n```typescript\nconst list = [{\n    alias: '@server',\n    // A list of destination paths\n    pathnames: ['./src/server'],\n    // If exact = true, only \"@server\" will be matched\n    // If exact = false, \"@server\" and \"@server/*\" will be matched\n    exact: false\n}, {\n    alias: 'react',\n    // pathnames can also be module names\n    pathnames: ['preact'],\n    exact: true\n}]\n\nconst aliases = new Aliases({ aliases: list });\n```\n\n### Specify the module path\n\nAs you saw upper, alias destinations can also be package names.\nThanks to the `modulesDir` option, you can define in which node_modules directory your package should be looked for.\n\n```typescript\nconst aliases = new Aliases({\n    modulesDir: ['./node_modules', '../../global_node_modules']\n});\n```\n\n**Warning**: This feature is experimental. It could lead to resolution problems in some cases.\n\n**Note**: Only relative paths are supported for now.\n\n### Debug\n\nAre you facing to a resolution problem ? Do you balieve these is a bug in this lib ?\n\nThat's not impossible 🤔\n\nTo better understands what ts-alias actually does in your case, you can enable advanced logs with the `debug` option:\n\n```typescript\nconst aliases = new Aliases({\n    debug: true\n});\n```\n\n## Test if a path can be shorten with an alias\n\n```typescript\npublic isAliased( filename: string ): boolean;\n```\n\n```typescript\naliases.isAliased(\"./src/server/services/user\");\n// Result: true\n\naliases.isAliased(\"./src\");\n// Result: false\n```\n\n## Shorten / Replace real path by alias\n\n```typescript\npublic apply( realpath: string, strict?: false ): string;\npublic apply( realpath: string, strict: true ): string | null;\npublic apply( realpath: string, strict?: boolean ): string | null;\n```\n\n```typescript\naliases.apply(\"./src/server/services/user\");\n// Result: \"@server/services/user\"\n\naliases.apply(\"react\");\n// Result: \"./node_modules/react\"\n```\n\nWhen the realpath couldn't be replaced with an alias:\n* When strict is true, null will be returned.\n* Otherwise, the original realpath will be returned, without any alias\n\n## Test if a path contains an alias\n\n```typescript\n public containsAlias( filename: string ): boolean;\n```\n\n```typescript\naliases.containsAlias(\"@server/services/user\");\n// Result: true\n\naliases.containsAlias(\"./src/server/services/user\");\n// Result: false\n```\n\n## Replace alias by real path\n\n```typescript\npublic realpath( request: string, strict?: false): string;\npublic realpath( request: string, strict: true): string | null;\npublic realpath( request: string, strict?: boolean): string | null;\n```\n\n```typescript\naliases.realpath(\"@server/services/user\");\n// Result: \"/home/gaetan/projects/myproject/src/server/services/user\"\n\naliases.realpath(\"./node_modules/react\");\n// Result: \"preact\"\n```\n\n## Convert the aliases list for [Webpack 5](https://github.com/webpack/webpack)\n\n```typescript\nconst webpackAliases = aliases.forWebpack();\n\nmodule.export = {\n    ...\n    resolve: {\n        alias: webpackAliases\n    }\n    ...\n}\n```\n\nYou can pass options in forWebpack():\n\n```typescript\nconst webpackAliases = aliases.forWebpack({\n\n    // The path where to resolve node modules\n    modulesPath: string,\n\n    // Set to true if you want forWebpack to output the package name (ex: `ts-alias/src/index.ts`) instead of the path to node_modules (ex: `./node_modules/ts-alias/src/index.ts`)\n    shortenPaths: boolean,\n    \n    // When set to true, it will return a { aliases, externals } object with the aliases for webpack,\n    //  and a nodeExternals function for webpack\n    nodeExternals: boolean\n});\n```\n\n## Convert the aliases list for [module-alias](https://github.com/ilearnio/module-alias)\n\n```typescript\nimport moduleAlias from 'module-alias';\n\nmoduleAlias.addAliases( aliases.forModuleAlias() );\n```\n\n-----------\n\n\u003cp align=\"center\"\u003e\n    This project helped you ? Let me know,\n    \u003ca target=\"_blank\" href=\"https://github.com/gaetanlegac/ts-alias/stargazers\"\u003e\u003cb\u003e⭐ Give it a Star :)\u003c/b\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n-----------\n\n## Changelog\n\n### 0.0.7 (16 December 2022)\n\n* Use a TOutputOptions object for passing options in forWebpack()\n```typescript\npublic forWebpack\u003cTNodeExternals extends boolean\u003e({ \n    modulesPath, shortenPaths, nodeExternals \n}: TOutputOptions\u003cTNodeExternals\u003e): TWebpackOutput\u003cTNodeExternals\u003e\n```\n\n* Added a shortenPaths option for forWebpack()\nSet to true if you want forWebpack to output the package name (ex: `ts-alias/src/index.ts`) instead of the path to node_modules (ex: `./node_modules/ts-alias/src/index.ts`)\n\n* Store aliases in array instead of indexing by alias.\nIt allows you to create an exact \u0026 prefix version for the same alias.\nBy example:\n```typescript\n{\n    // Exact version: only match \"@server\"\n    \"@server\": [\"./server\"],\n    // Prefix version: match any import that starts by \"@server/\"\n    \"@server/*\": [\"./server/*\"],\n}\n```\n\n## TODO\n\n* Tests (the current version lacks of tests)\n* Strict types checking\n* Better path resolving (traverse extends)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgaetanlegac%2Fts-alias","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgaetanlegac%2Fts-alias","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgaetanlegac%2Fts-alias/lists"}