{"id":20830014,"url":"https://github.com/patrickhollweck/autoloaderts","last_synced_at":"2025-05-07T22:11:03.736Z","repository":{"id":41813266,"uuid":"135051306","full_name":"PatrickHollweck/AutoloaderTS","owner":"PatrickHollweck","description":"A nodejs autoloader for typescript","archived":false,"fork":false,"pushed_at":"2022-04-28T13:29:54.000Z","size":110,"stargazers_count":3,"open_issues_count":2,"forks_count":2,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-05-05T08:04:25.410Z","etag":null,"topics":["autoloader","autoloading","evaluator","nodejs","typescript","typescript-library"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/autoloader-ts","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PatrickHollweck.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-05-27T13:58:13.000Z","updated_at":"2021-06-20T07:39:52.000Z","dependencies_parsed_at":"2022-08-11T18:11:23.525Z","dependency_job_id":null,"html_url":"https://github.com/PatrickHollweck/AutoloaderTS","commit_stats":null,"previous_names":["fetzenrndy/autoloaderts"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PatrickHollweck%2FAutoloaderTS","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PatrickHollweck%2FAutoloaderTS/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PatrickHollweck%2FAutoloaderTS/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PatrickHollweck%2FAutoloaderTS/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PatrickHollweck","download_url":"https://codeload.github.com/PatrickHollweck/AutoloaderTS/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252961841,"owners_count":21832197,"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":["autoloader","autoloading","evaluator","nodejs","typescript","typescript-library"],"created_at":"2024-11-17T23:22:55.461Z","updated_at":"2025-05-07T22:11:03.716Z","avatar_url":"https://github.com/PatrickHollweck.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AutoloaderTS\n\n\u003e AutoloaderTS is a nodejs autoloader for typescript\n\n### Why and What ?\n\nImporting files written in typescript in a nodejs environment can be rather tricky, Since you not only have\nto transpile the code but also have to be extra carefull to not mess up the scope and context. For this reason\nAutoloader-ts was born. It transpiles and requires all the selected files, and allow you to continue to use\ncompiler plugins like `tsconfig-paths`.\n\n## Docs\n\n### RECOMMENDED: Take a look at our samples:\n\n1.  [Loading files](https://github.com/FetzenRndy/AutoloaderTS/tree/master/samples/01-Load_a_directory)\n2.  [Injecting Code](https://github.com/FetzenRndy/AutoloaderTS/tree/master/samples/02-Injecting_custom_code)\n3.  [Loading from a glob](https://github.com/FetzenRndy/AutoloaderTS/tree/master/samples/03-Load_from_glob)\n\n---\n\n## Loader Types\n\nThere are 2 kinds of loaders this lib implements.\n\n1.  DynamicImport-Autoloader (Recommended)\n\n    This Autoloader uses the `import(PATH)` function for import.\n\n    ```ts\n    const loader = Autoloader.dynamicImport();\n    ```\n\n2.  NodeEval-Autoloader\n\n    This Autoloader uses the \"node-eval\" lib and uses the fs api to load and dynamically invoke files.\n\n    \u003e The ? after the parameter means \"optional\"\n\n    ```ts\n    const loader = Autoloader.nodeEval(customCompilerOptions?, tsConfigFilePath?);\n    ```\n\nYou can instantiate one of the other by choosing the correct main `AutoloaderTS` method.\nFor one or the other you may have to pass some parameters.\n\n### ATTENTION: Schemantics!\n\nWhat I mean by this is: On the one loader you may need to pass a absolute path and on the other\nyou can pass both a relative and a absolute path!\n\nEven tho both loader have the `SAME API!` the schemantics may not be the same!\nI hope to implement libary level compatability layers in the future, so both can be used\nas a \"drop-in\" replacement.\n\n#### Example\n\nTo use the autoloader you first need to create a instance of it. Most of the library is async so make sure to handle it!\n\n```ts\nimport { Autoloader } from \"autoloader-ts\";\n\nconst loader = await Autoloader.nodeEval();\n```\n\nThen you can use the instance methods to load files. There are 2 main ways to load files.\n\n1.  From directories\n\n```ts\nawait loader.fromDirectories(`${__dirname}/autoload`);\n```\n\n2.  From a glob\n\n```ts\nawait loader.fromGlob(\"*/**/*.autoload.ts\");\n```\n\nIf you need to inject code into you might want to call this method BEFORE loading the files\n\n\u003e The loader will inject this JS code before evaluating it. You may use it to register compiler plugins like\n\u003e tsconfig-paths. DO NOT misuse this. If you are not careful you might override something and create very hard\n\u003e to find bugs. Also make sure to include a line-break or a semicolon at the end. The inject code will be concatenated\n\u003e with the transpiled code so make sure it is error free!.\n\n```ts\nloader.injectCode(\"require('tsconfig-register/paths'\");\n```\n\nWhen you have specified all the files you want to load, access the \"result\" field.\n\n```ts\nconsole.log(loader.result.exports);\n```\n\nThis result field contains all the exported classes / fields / functions from the loaded files.\nFor example take this file:\n\n```ts\nexport class A {\n\tpublic greet() {\n\t\tconsole.log(\"Hello World\");\n\t}\n}\n```\n\nIf you then load the file with a method from above, the `result.exports` will be: `[ [Function A] ]`\nYou can then loop through it and access the file.\n\n### Concret Usecase\n\nTake this example: You build a http framework, this framework allows the developer to define Jobs that will be run by the framework\n\nSo you create a Job Decorator\n\n```ts\nimport \"reflect-metadata\";\n\nexport function Job() {\n\treturn Reflect.metadata([KEY], [VAL]);\n}\n```\n\nThen the user of the framework can define subclasses of that type.\n\n```ts\nimport { Job } from \"lib/Job\";\n\n@Job()\nexport class UpdateUsersJob {\n\trun() {\n\t\tconsole.log(\"Updating user...\");\n\t}\n}\n```\n\nWithout autoloading the user of the framework would have to import the class somewhere in the project or register it like this\n\n```ts\nJobs.register(UpdateUserJob);\n```\n\nBut with autoloading you dont haveto import your Jobs anywhere. Lets say all jobs are stored in `app/jobs`\nThen you can just create a loader and load the classes.\n\n```ts\n// Create the loader\nconst loader = await Autoloader.dynamicImport();\n\n// Load and transpile all files from app/jobs\nawait loader.fromDirectories(`${__dirname}/app/jobs`);\n\n// Iterate ofer the exports\nfor (const exported of loader.result.exports) {\n\t// Get the metadata of the export.\n\tconst metadata: string = Reflect.getMetadata(JobMetadataKey, exported);\n\n\t// You can then access members of the class dynamically!\n\tJobs.register(new exported());\n}\n```\n\nTada... Now you have a much more convenient and less boilerplate solution for jobs loading.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatrickhollweck%2Fautoloaderts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpatrickhollweck%2Fautoloaderts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatrickhollweck%2Fautoloaderts/lists"}