在Web中运行的TypeScript中未定义regeneratorRuntime

jun*_*lin 3 typescript vue.js parceljs

我使用Vue.js + TypeScript + Parcel进行演示,并尝试async/await使用index.ts.

的index.html

<html>
...
<body>
  <script src="index.ts"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

index.ts

console.log('Not see me')
(async () => {
  console.log('Did not execute here too.')
})()
Run Code Online (Sandbox Code Playgroud)

执行parcel index.html并打开http:// localhost:1234 /.控制台抛出错误:未定义regeneratorRuntime

index.a4a28941.js:110 Uncaught ReferenceError: regeneratorRuntime is not defined
    at index.a4a28941.js:110
    at Object.parcelRequire.306 (index.ts:3)
    at newRequire (index.a4a28941.js:48)
    at parcelRequire.306 (index.a4a28941.js:80)
    at index.a4a28941.js:106
Run Code Online (Sandbox Code Playgroud)

似乎async语法需要polyfill?

这是我的tsconfig.json:

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "baseUrl": ".",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "jsx": "preserve",
    "module": "esnext",
    "moduleResolution": "node",
    "noImplicitReturns": true,
    "sourceMap": true,
    "strict": true,
    "target": "esnext",
    "paths": {
      "@/*": ["src/*"]
    },
    "lib": ["esnext", "esnext.array", "dom", "dom.iterable", "scripthost", "es2015.generator"]
  },
  "parcelTsPluginOptions": {
    // If true type-checking is disabled
    "transpileOnly": false
  },
  "include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue", "tests/**/*.ts", "tests/**/*.tsx"],
  "exclude": ["node_modules"]
}
Run Code Online (Sandbox Code Playgroud)

小智 9

我的解决办法是设置target"es5"了在tsconfig.json文件中:

{
  "compilerOptions": {
    "target": "es5"
  }
}
Run Code Online (Sandbox Code Playgroud)

然而,似乎parcel总是使用babel来转换javascript,这导致了与typescript编译器的一些冲突(参见:https://github.com/parcel-bundler/parcel/issues/954 and here:https:// github.com/parcel-bundler/parcel/issues/871).有些人也通过babel-polifill在他们的项目中要求解决了这个问题但是如果你只想使用typescript编译器而不是让babel转换任何东西,这似乎不是最好的选择.