5

我使用 Vue.js + TypeScript + Parcel 进行了演示,并async/awaitindex.ts中尝试。

索引.html

<html>
...
<body>
  <script src="index.ts"></script>
</body>
</html>

索引.ts

console.log('Not see me')
(async () => {
  console.log('Did not execute here too.')
})()

执行parcel index.html并打开http://localhost:1234/。控制台报错:regeneratorRuntime is not defined

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

似乎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"]
}
4

1 回答 1

11

我的解决方案是在文件中设置target为:"es5"tsconfig.json

{
  "compilerOptions": {
    "target": "es5"
  }
}

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

于 2018-07-18T15:54:15.063 回答