{"id":13834715,"url":"https://github.com/avensia-oss/ts-transform-async-import","last_synced_at":"2025-04-07T06:39:01.369Z","repository":{"id":97953740,"uuid":"163528551","full_name":"avensia-oss/ts-transform-async-import","owner":"avensia-oss","description":"A TypeScript custom transform that turns synchronous imports of async functions into asynchronous imports","archived":false,"fork":false,"pushed_at":"2018-12-30T12:31:57.000Z","size":65,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-17T20:18:36.520Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/avensia-oss.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-12-29T17:24:05.000Z","updated_at":"2024-11-16T09:29:53.000Z","dependencies_parsed_at":"2024-01-15T18:55:57.294Z","dependency_job_id":"a0d0c738-23ab-44ae-a262-b97984b192ad","html_url":"https://github.com/avensia-oss/ts-transform-async-import","commit_stats":{"total_commits":4,"total_committers":1,"mean_commits":4.0,"dds":0.0,"last_synced_commit":"58ffffb07e24f6de26beeb7c2c88a5e1d8e1910e"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/avensia-oss%2Fts-transform-async-import","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/avensia-oss%2Fts-transform-async-import/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/avensia-oss%2Fts-transform-async-import/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/avensia-oss%2Fts-transform-async-import/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/avensia-oss","download_url":"https://codeload.github.com/avensia-oss/ts-transform-async-import/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247608183,"owners_count":20965950,"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":[],"created_at":"2024-08-04T14:00:51.343Z","updated_at":"2025-04-07T06:39:01.339Z","avatar_url":"https://github.com/avensia-oss.png","language":"TypeScript","funding_links":[],"categories":["Transformers"],"sub_categories":["Optimization"],"readme":"# ts-transform-async-import\r\n\r\n_Note! This transformer is currently experimental_\r\n\r\nA TypeScript custom transformer that turns synchronous imports of async functions into asynchronous imports. The reason being to lazily load code as needed\r\ninstead of bundling all code together.\r\n\r\nAn example would be:\r\n\r\n```\r\n// file1.ts\r\nexport async function fetchSomething() {\r\n  return await fetch('/something');\r\n}\r\n\r\n// file2.ts\r\nimport { fetchSomething } from './file1';\r\n\r\nbutton.onclick = () =\u003e doSomethingInteresting(await fetchSomething());\r\n```\r\n\r\nWhich gets turned into:\r\n\r\n```\r\n// file1.ts\r\nexport async function fetchSomething() {\r\n  return await fetch('/something');\r\n}\r\n\r\n// file2.ts\r\nbutton.onclick = () =\u003e doSomethingInteresting(await import('./file1').then(m =\u003e m.fetchSomething()));\r\n```\r\n\r\nThis means that the `fetchSomething` function won't get code loaded until the user clicks on a button, instead of having to load the code on initial page load.\r\n\r\nNote that all functions that returns a promise is considered by this transformer, not just explicitly async functions.\r\n\r\n## Bail outs\r\n\r\nThe transformer bails out of removing the import if it sees that the imported function is used for something other than calling it. So in this case:\r\n\r\n```\r\n// file2.ts\r\nimport { fetchSomething } from './file1';\r\n\r\nbutton.onclick = () =\u003e doSomethingInteresting(await fetchSomething());\r\npassItSomewhere(fetchSomething);\r\n```\r\n\r\nThe import statement won't be removed since it's passed to `passItSomewhere` and we can't modify the program in a way that it's still semantically the same.\r\n\r\n## Limitations\r\n\r\nThe transformer doesn't follow passing imported functions around, which means that this code:\r\n\r\n```\r\n// file1.ts\r\nexport async function fetchSomething() {\r\n  return await fetch('/something');\r\n}\r\n\r\n// file2.ts\r\nimport { fetchSomething } from './file1';\r\n\r\nfunction init(fetcher: () =\u003e Promise\u003cany\u003e) {\r\n  button.onclick = () =\u003e doSomethingInteresting(await fetcher());\r\n}\r\ninit(fetchSomething);\r\n```\r\n\r\nWon't be optimized/transformed.\r\n\r\n## Other useful transform\r\n\r\nIf you find this transform useful you might want to use this one as well: https://github.com/avensia-oss/ts-transform-export-const-folding\r\n\r\n# Installation\r\n\r\n```\r\nyarn add @avensia-oss/ts-transform-async-import\r\n```\r\n\r\n## Usage with webpack\r\n\r\nUnfortunately TypeScript doesn't let you specifiy custom transformers in `tsconfig.json`. If you're using `ts-loader` with webpack you can specify it like this:\r\nhttps://github.com/TypeStrong/ts-loader#getcustomtransformers-----before-transformerfactory-after-transformerfactory--\r\n\r\nThe default export of this module is a function which expects a `ts.Program` an returns a transformer function. Your config should look something like this:\r\n\r\n```\r\nconst asyncImportTransform = require('@avensia-oss/ts-transform-async-import');\r\n\r\nreturn {\r\n  ...\r\n  options: {\r\n    getCustomTransformers: (program) =\u003e ({\r\n      before: [asyncImportTransform(program)]\r\n    })\r\n  }\r\n  ...\r\n};\r\n```\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Favensia-oss%2Fts-transform-async-import","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Favensia-oss%2Fts-transform-async-import","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Favensia-oss%2Fts-transform-async-import/lists"}