regenerator-runtimenpm包有什么用?
我在我公司的代码库中注意到了它,它每周有 3000 万次下载,所以我很好奇它的重要性。
回答
regenerator-runtime是对编译/转译async函数的运行时支持。(它可能还有其他用途,但这是主要用途。)
当你使用像 Babel 这样的编译器将现代 JavaScript 编译成早期的 JavaScript(这个过程有时被称为transpiling)时,你可以做的一async件事就是将函数编译成可以在不支持async函数的JavaScript 引擎上运行的东西(例如越来越不相关的 IE11)。Babel 进行语法转换,但生成的代码依赖于regenerator-runtime.
- @ezio - Babel doesn't control what `script` files you included in your page/app, that's not its job; it's job is transforming code from A to B. But a bundler (like Webpack or Rollup, etc.) configured to work with Babel will automatically include the runtime support, because that *is* its job.
- @ezio - No need to apologize, there are lots of moving parts here. 🙂 Including the script in the runtime environment. Babel rewrites `async` function code into code using `regenerator-runtime` (in any code file where an `async` function appears), but `regenerator-runtime` itself only has to be included once, not for every file. So you include it (if you need it) in a `script` tag in the resulting page.