{"name":"vite-plugin-top-level-await","version":"1.2.3","keywords":["vite","plugin","top-level await","await"],"author":{"name":"Menci","email":"huanghaorui301@gmail.com"},"license":"MIT","_id":"vite-plugin-top-level-await@1.2.3","maintainers":[{"name":"menci","email":"huanghaorui301@gmail.com"}],"homepage":"https://github.com/Menci/vite-plugin-top-level-await#readme","bugs":{"url":"https://github.com/Menci/vite-plugin-top-level-await/issues"},"dist":{"shasum":"6aa76335045acbca1d93e0c68a519c5d7fe4c759","tarball":"http://registry-npm.ywwl.com/vite-plugin-top-level-await/download/vite-plugin-top-level-await-1.2.3.tgz","fileCount":33,"integrity":"sha512-Hc4W7R+Hw4413lzv+kuVdXLrgNU+Pb07853Q15n/4JytEtyLykpN2rmtlG7J5uWij2btDXEEiv+DcrCiRZo/zA==","signatures":[{"sig":"MEYCIQDlXG0JGdA9/zMGxep32M7aoV3lFu5RUUC8IpdRWGiP3wIhAMBm7IvGJigcGvgb5eTKbH9Jd+YFZUyGyQsQPu60MmoT","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":63285,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJj0/98ACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmpGng//f0RkVzAvchkF+NPxq/5pj85/dIUj/yeiKiE8jvDLpzTYGUYc\r\nmV9FwTCpX1gv+m8+mTIzOCNPKSxva1tTDS7fv6qiN7AfvqhZLgCzyFwy5Xzv\r\nisrdunK09gB1f7tZiaWk2JJzIiKlGvFajMeJ+FeruE9DtGzxlZG2Ziyc9Rhr\r\n3GkiJmLJWM15s6Sfuthtlqy5sXDhJ4DMZC6qVi9vuiRqMWdQ/bDnq0qf5LeL\r\nr2AdGTnb7PmNho6qB8FsxAG4It5tNk2BLEDeCRa7CaPI6wZD4UtM1pmv0k7k\r\nrp8/pK4h6zBGsgeBCn1VkyRTZdR85HuAYXEbqMwgr7DfWf9Bx1d2YphzmGmA\r\n2q/j0x0oa1XmfSdVy2rRoxOAj6k2qKUNeGfEAwmM5us0/GRqdoKlxoJPBkGQ\r\nQ93XijYyBt0/QfCMgcWHE5IDCxYLEQpKx8qOxb9vmcgrmRJy2qEioqpyjymZ\r\nBbRF4Hz4drRs4N4FKZB0EcQ9cFoMU/wwDto35URjNnVCbAOyMbtai/YDn8rw\r\nKVsq3rj7ulXEtzj45DN8KxEYq/C1TVpGHv+Iom2Vo/kRjQjnKCOFenzNYUiJ\r\nlQcLRKMFFL7IRsAtSGlAXumIzLUNPnrpMO1HJzPhKHdnBbOVb+2ieFL0M1SR\r\n1eAqqnxcmjrGX78JfN1oHj+mUFYbozXOMFY=\r\n=Fn1L\r\n-----END PGP SIGNATURE-----\r\n","size":14144,"noattachment":false,"key":"/vite-plugin-top-level-await/-/vite-plugin-top-level-await-1.2.3.tgz"},"main":"dist/index.js","types":"./dist/index.d.ts","config":{"commitizen":{"path":"./node_modules/cz-conventional-changelog"}},"exports":{"import":"./exports/import.mjs","require":"./exports/require.cjs"},"gitHead":"eab6cc1b93e8d49825d93213e984254f5e02b244","scripts":{"test":"jest --verbose --coverage","build":"tsc","format":"prettier --write '**/*.{mjs,cjs,js,ts}'"},"_npmUser":{"name":"menci","email":"huanghaorui301@gmail.com"},"repository":{"url":"git+https://github.com/Menci/vite-plugin-top-level-await.git","type":"git"},"_npmVersion":"8.19.3","description":"Transform code to support top-level await in normal browsers for Vite.","directories":{},"_nodeVersion":"16.19.0","dependencies":{"uuid":"^9.0.0","@swc/core":"^1.3.10"},"_hasShrinkwrap":false,"devDependencies":{"jest":"^29.2.1","vite":"^3.1.8","esbuild":"^0.15.12","ts-jest":"^29.0.3","prettier":"^2.7.1","typescript":"^4.8.4","@types/jest":"^29.2.0","@types/uuid":"^9.0.0","jest-extended":"^3.1.0","cz-conventional-changelog":"^3.3.0"},"peerDependencies":{"vite":">=2.8"},"_npmOperationalInternal":{"tmp":"tmp/vite-plugin-top-level-await_1.2.3_1674837884292_0.23370265095074716","host":"s3://npm-registry-packages"},"readme":"# vite-plugin-top-level-await\n\n[![Test Status](https://img.shields.io/github/actions/workflow/status/Menci/vite-plugin-top-level-await/test.yaml?branch=main&style=flat-square)](https://github.com/Menci/vite-plugin-top-level-await/actions?query=workflow%3ATest)\n[![npm](https://img.shields.io/npm/v/vite-plugin-top-level-await?style=flat-square)](https://www.npmjs.com/package/vite-plugin-top-level-await)\n[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=flat-square)](http://commitizen.github.io/cz-cli/)\n[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)\n[![License](https://img.shields.io/github/license/Menci/vite-plugin-top-level-await?style=flat-square)](LICENSE)\n\nTransform code to support top-level await in normal browsers for Vite. Support all modern browsers of Vite's default target without need to set `build.target` to `esnext`.\n\n## Installation\n\n```bash\nyarn add -D vite-plugin-top-level-await\n```\n\n## Usage\n\nPut this plugin in your plugin list. At most case you don't need to care the order, but if there're any plugin transforming bundle before it, there's a little chance that this plugin fails to parse code since it does only parse Rollup's output `export { ... }` export statement.\n\n```typescript\nimport topLevelAwait from \"vite-plugin-top-level-await\";\n\nexport default defineConfig({\n  plugins: [\n    topLevelAwait({\n      // The export name of top-level await promise for each chunk module\n      promiseExportName: \"__tla\",\n      // The function to generate import names of top-level await promise in each chunk module\n      promiseImportName: i => `__tla_${i}`\n    })\n  ]\n});\n```\n\n## Workers\n\nYou can use this plugin for workers (by putting it in `config.worker.plugins`).\n\n* If the worker format is ES, the plugin works normally.\n* If the worker format is IIFE, the plugin first let Vite build your worker as an ES bundle since IIFE doesn't support top-level awaits, and then build the transformed ES bundle to IIFE. Please use IIFE when targeting Firefox.\n  ```js\n  const myWorker = import.meta.env.DEV\n      // In development mode, `import`s in workers are not transformed, so you\n      // must use `{ type: \"module\" }`.\n    ? new Worker(new URL(\"./my-worker.js\", import.meta.url), { type: \"module\" })\n      // In build mode, let Vite and vite-plugin-top-level-await build a single-file\n      // bundle of your worker that works on both modern browsers and Firefox.\n    : new Worker(new URL(\"./my-worker.js\", import.meta.url), { type: \"classic\" });\n  ```\n\n## Note\n\nThis plugin transforms code from:\n\n```js\nimport { a } from \"./a.js\"; // This module uses top-level await\nimport { b } from \"./b.js\"; // This module uses top-level await too\nimport { c } from \"./c.js\"; // This module does NOT use top-level await\n\nconst x = 1;\nawait b.func();\nconst { y } = await somePromise;\n\nexport { x, y };\n```\n\nTo:\n\n```js\nimport { a, __tla as __tla_0 } from \"./a.js\"; // This module uses top-level await\nimport { b, __tla as __tla_1 } from \"./b.js\"; // This module uses top-level await too\nimport { c } from \"./c.js\"; // This module does NOT use top-level await\n\n// Original exported variables\nlet x, y;\n\n// Await imported TLA promises and execute original top-level statements\nlet __tla = Promise.all([\n  (() => { try { return __tla_0; } catch {} })(),\n  (() => { try { return __tla_1; } catch {} })()\n]).then(async () => {\n  // Transform exported variables to assignments\n  x = 1;\n\n  await b.func();\n\n  // Destructing patterns (and function / class declarations as well) are handled correctly\n  ({ y } = await somePromise);\n});\n\n// Export top-level await promise\nexport { x, y, __tla };\n```\n\nIt could handle **correct usage** of circular dependencies with the default behavior of ES standard. But when an TLA dependency is being awaited, an accessing to one of its exports **will NOT raise an exception**. At most time you don't need to care about this. These *could* be supported by doing more transformations of the whole AST but it will make building a lot slower. Open an issue and tell me your scenario if you really need the exception.\n","publish_time":1674837884509,"_cnpm_publish_time":1674837884509,"dist-tags":{"latest":"1.6.0"}}