如何设置TypeScript + Babel + Webpack?

ujv3wf0j  于 2022-12-08  发布在  Babel
关注(0)|答案(3)|浏览(160)

我有这些依赖项:

"devDependencies": {
  "@types/node": "^4.0.27-alpha",
  "babel-core": "^6.10.4",
  "babel-loader": "^6.2.4",
  "babel-polyfill": "^6.9.1",
  "babel-preset-es2015": "^6.9.0",
  "babel-preset-stage-0": "^6.5.0",
  "ts-loader": "^0.8.2",
  "typescript": "^2.0.0",
  "webpack": "^1.13.1"
}

.巴伯尔克:

{
  "presets": [
    "es2015",
    "stage-0"
  ]
}

tsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "noImplicitAny": false,
        "sourceMap": false,
        "outDir": "built"
    },
    "exclude": [
        "node_modules"
    ]
}

webpack.config.js:

module.exports = {
  entry: ['babel-polyfill', './src/'],
  output: {
    path: __dirname,
    filename: './built/bundle.js',
  },
  resolve: {
    modulesDirectories: ['node_modules'],
    extensions: ['', '.js', '.ts'],
  },
  module: {
    loaders: [{
      test: /\.tsx?$/, loaders: ['ts-loader', 'babel-loader'], exclude: /node_modules/
    }],
  }
};

/src/索引.ts:

async function foo() {
  const value = await bar();
  console.log(value);
}

function bar() {
  return new Promise((resolve, reject) => {
    return resolve(4);
  });
}

(async function run() {
  await foo();
}());

有了这个设置它确实工作,我可以建立和运行它(日志4正确).然而,我总是得到一些错误的Webpack:

ERROR in ./src/index.ts
(4,32): error TS2304: Cannot find name 'regeneratorRuntime'.

ERROR in ./src/index.ts
(6,12): error TS2304: Cannot find name 'regeneratorRuntime'.

ERROR in ./src/index.ts
(31,451): error TS2346: Supplied parameters do not match any signature of call target.

ERROR in ./src/index.ts
(40,33): error TS2304: Cannot find name 'regeneratorRuntime'.

ERROR in ./src/index.ts
(41,12): error TS2304: Cannot find name 'regeneratorRuntime'.

好像和babel-polyfill有关,我遗漏了什么?

b1payxdu

b1payxdu1#

巴别塔7号不需要ts装载器。

由于巴别塔7的ts-loader是不必要的,因为Babel 7 understands TypeScript.完整的细节TypeScript + Babel7 + Webpack setup are here

不带ts-loader的设置概述。

安装Babel的TypeScript支援。只有@babel/preset-typescript是必要的;其他三个添加了TypeScript支持的附加功能。

npm install --save-dev @babel/preset-typescript 
npm install --save-dev @babel/preset-env 
npm install --save-dev @babel/plugin-proposal-class-properties 
npm install --save-dev @babel/plugin-proposal-object-rest-spread

配置其他.babelrc插件和预设。

{
    "presets": [
        "@babel/preset-env",
        "@babel/preset-typescript"
    ],
    "plugins": [
        "@babel/proposal-class-properties",
        "@babel/proposal-object-rest-spread"
    ]
}

并更新您的webpack.config.js(为清楚起见,省略了其他代码)。

module: {
  rules: [
  {
     test: /\.(js|jsx|tsx|ts)$/,
     exclude: /node_modules/,
     loader: 'babel-loader'
    }
  ]
},
resolve: {
  extensions: ['*', '.js', '.jsx', '.tsx', '.ts'],
},
atmip9wb

atmip9wb2#

加载程序始终从右向左执行,因此更改为

test: /\.tsx?$/, loaders: ['babel-loader', 'ts-loader'], exclude: /node_modules/

修复了此问题,因为它将首先运行ts-loader
完整的webpack.config.js文件:

module.exports = {
  entry: ['babel-polyfill', './src/'],
  output: {
    path: __dirname,
    filename: './dist/index.js',
  },
  resolve: {
    extensions: ['', '.js', '.ts'],
  },
  module: {
    loaders: [{
      test: /\.ts$/, loaders: ['babel-loader', 'ts-loader'], exclude: /node_modules/
    }],
  }
};

示例项目:brunolm/typescript-babel-webpack

cnh2zyt3

cnh2zyt33#

(4,32):错误TS2304:找不到名称'regeneratorRuntime'。
这是babel的输出被馈送到ts的症状。此顺序错误

修复

您的编译设置应该有TS输出馈送到巴别塔。
或者,您也可以使用@babel/preset-typescript只使用Babel编译TypeScript。

更多

使用Babel编译TypeScript:https://babeljs.io/docs/en/babel-preset-typescript

相关问题