{"_id":"@avensia-oss/ts-transform-async-import","_rev":"5-6fbfe0366d4749607034033debf37b32","name":"@avensia-oss/ts-transform-async-import","dist-tags":{"latest":"0.0.1"},"versions":{"0.0.1":{"name":"@avensia-oss/ts-transform-async-import","version":"0.0.1","keywords":["TypeScript","transform","transformer","async","await","import"],"author":{"name":"Anders Ekdahl","email":"anders.ekdahl@avensia.com"},"license":"MIT","_id":"@avensia-oss/ts-transform-async-import@0.0.1","maintainers":[{"name":"avensia-oss","email":"andekd@avensia.com"}],"homepage":"https://github.com/avensia-oss/ts-transform-async-import","bugs":{"url":"https://github.com/avensia-oss/ts-transform-async-import/issues"},"dist":{"shasum":"959a1fd0ceb46060f1b19176f7434bc5beaafe97","tarball":"https://registry.npmjs.org/@avensia-oss/ts-transform-async-import/-/ts-transform-async-import-0.0.1.tgz","fileCount":9,"integrity":"sha512-3S4r7X5i/Es+plGIZeLjURY3kV3+CrnPGBivBkqoChpsXsNyAzbCvP7iAiWs5wYREsdRLZFG9bACn7hrzxTwnQ==","signatures":[{"sig":"MEUCIQDYuKoaov6xfnKWLGsHD8Am+VTNjp6Q1i/22Iyov63icgIgcDF4mFmZGVnnllZ88AH8kdMEUzs4fPkoetiIoisDZx8=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":14317,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJcJ+QOCRA9TVsSAnZWagAAyLgP/iB6xcH3drY0m4x+7YPl\nYc8NxzgCDF/DEN8njcvKAhSe9rmxvkLC0Jpnohg5qNG02Q2MOT+VHZwVlgBJ\nccdlSY1C1PeGVaLwk3FSX97Ue8kPntj/hRPqbn1sqhfMRW/XHlh6BganOrt6\nx2hPx74MnOdx3tEjksTeqCYLY9UxvwaD9TiWG8mBapHdE1fRFNzOpdIQe4pe\nwnbZY11iz2vyP2gCJT6PUgKXvISQT2sSBa22MLg0LPxCJzhdhsQ+gM6Aafli\nRFbytsIkSnCuat1s1CP6dCNkqO0vE8F/c8j/fOqPlUV6K3YL4F+Ftl2oQB2m\nEMfyPI4LkItrQbrbDSx/p57UHXnYRBNb6imzaNUChHqG76Vsjxu7kkuNyiLI\npw2n9vckqMIEJ3TdoqUNAlyY+8/6pvjMSDo2/go9/AC+LdRaFqZkoHoAygFA\nvqq+aI97LKXgSgYL2hAl4ZCD6PJE2yN+QCWkE1MKTqOn0Q+3u1AZx/akpbid\nYMCsmbyQg5NBxxMq9bAjmgWdllO8lSSsjoM+I120RJinxcbSYJGJMYYQkXgS\nPKNjJZ4jPlTnDJh4bl0HmmptUufNtHyvIfMUAgrIuZjzzZ7xQAYKk9VEWi32\nIWLFW33r53Xaa7/mRDHuZ3gFgy4evTXMc5ABxRP321INdF+9lRVrXK01IENc\nI5ja\r\n=fWO2\r\n-----END PGP SIGNATURE-----\r\n"},"main":"index.js","private":false,"scripts":{"test":"jest","build":"tsc --version && tsc --project \"./\""},"_npmUser":{"name":"avensia-oss","email":"andekd@avensia.com"},"description":"A TypeScript custom transformer that imports async functions when needed","directories":{},"licenseText":"MIT License\n\nCopyright (c) Avensia AB and its affiliates.\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.","_hasShrinkwrap":false,"devDependencies":{"jest":"^23.6.0","ts-jest":"^23.10.5","prettier":"^1.15.3","typescript":"^3.2.2","@types/jest":"^23.3.10","@types/node":"^10.12.18"},"peerDependencies":{"typescript":"*"},"_npmOperationalInternal":{"tmp":"tmp/ts-transform-async-import_0.0.1_1546118157707_0.456231730700259","host":"s3://npm-registry-packages"}}},"time":{"created":"2018-12-29T21:15:57.543Z","modified":"2026-09-21T11:35:04.918Z","0.0.1":"2018-12-29T21:15:57.800Z"},"bugs":{"url":"https://github.com/avensia-oss/ts-transform-async-import/issues"},"author":{"name":"Anders Ekdahl","email":"anders.ekdahl@avensia.com"},"license":"MIT","homepage":"https://github.com/avensia-oss/ts-transform-async-import","keywords":["TypeScript","transform","transformer","async","await","import"],"description":"A TypeScript custom transformer that imports async functions when needed","maintainers":[{"email":"eric.lindell@avensia.com","name":"lindelleric"},{"email":"johan.book@avensia.com","name":"johboo"},{"email":"johan.nilsson@avensia.com","name":"yoohahn-avensia"}],"readme":"# ts-transform-async-import\n\n_Note! This transformer is currently experimental_\n\nA TypeScript custom transformer that turns synchronous imports of async functions into asynchronous imports. The reason being to lazily load code as needed\ninstead of bundling all code together.\n\nAn example would be:\n\n```\n// file1.ts\nexport async function fetchSomething() {\n  return await fetch('/something');\n}\n\n// file2.ts\nimport { fetchSomething } from './file1';\n\nbutton.onclick = () => doSomethingInteresting(await fetchSomething());\n```\n\nWhich gets turned into:\n\n```\n// file1.ts\nexport async function fetchSomething() {\n  return await fetch('/something');\n}\n\n// file2.ts\nbutton.onclick = () => doSomethingInteresting(await import('./file1').then(m => m.fetchSomething()));\n```\n\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.\n\nNote that all functions that returns a promise is considered by this transformer, not just explicitly async functions.\n\n## Bail outs\n\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:\n\n```\n// file2.ts\nimport { fetchSomething } from './file1';\n\nbutton.onclick = () => doSomethingInteresting(await fetchSomething());\npassItSomewhere(fetchSomething);\n```\n\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.\n\n## Limitations\n\nThe transformer doesn't follow passing imported functions around, which means that this code:\n\n```\n// file1.ts\nexport async function fetchSomething() {\n  return await fetch('/something');\n}\n\n// file2.ts\nimport { fetchSomething } from './file1';\n\nfunction init(fetcher: () => Promise<any>) {\n  button.onclick = () => doSomethingInteresting(await fetcher());\n}\ninit(fetchSomething);\n```\n\nWon't be optimized/transformed.\n\n## Other useful transform\n\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\n\n# Installation\n\n```\nyarn add @avensia-oss/ts-transform-async-import\n```\n\n## Usage with webpack\n\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:\nhttps://github.com/TypeStrong/ts-loader#getcustomtransformers-----before-transformerfactory-after-transformerfactory--\n\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:\n\n```\nconst asyncImportTransform = require('@avensia-oss/ts-transform-async-import');\n\nreturn {\n  ...\n  options: {\n    getCustomTransformers: (program) => ({\n      before: [asyncImportTransform(program)]\n    })\n  }\n  ...\n};\n```\n","readmeFilename":"README.md"}