javascript - @babel/typescript 在 webpack 构建时不会抛出错误

标签 javascript typescript webpack compiler-errors babeljs

我正在尝试使用 Babel 7 的 @babel/typescript 预设转译 TypeScript。它工作正常,但由于某种原因,构建控制台中没有来自 TS 的任何错误消息。

我有下一个配置:

webpack.config.js

const outputPath = require('path').resolve(__dirname, './production');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {

  entry: [
    './src/index.tsx'
  ],

  output: {
    path: outputPath,
    filename: '[name].[chunkhash].js',
  },

  module: {
    rules: [
      {
        test: /\.(ts|tsx)$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'source-map-loader',
        enforce: "pre"
      },
      {
        test: /\.(jpe?g|png|gif|svg)$/i,
        loader: 'file-loader'
      },
      {
        test: /\.(eot|svg|ttf|woff|woff2)$/,
        loader: 'file-loader'
      }
    ]
  },

  plugins: [
    new HtmlWebpackPlugin({template: './src/index.html'})
  ],

  resolve: {
    extensions: ['.js', '.jsx', '.ts', '.tsx']
  },

  devServer: {
    contentBase: outputPath
  }
};

.babelrc

{
  "presets": [
    "@babel/preset-env",
    "@babel/typescript",
    "@babel/preset-react"
  ],
  "plugins": [
    "react-hot-loader/babel",
    "@babel/plugin-transform-runtime",
  ]
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "jsx": "react",
    "outDir": "./production/",
    "sourceMap": true,
    "noImplicitAny": true,
    "lib": ["esnext", "dom"]
  },
  "include": [
    "./src/**/*"
  ]
}

输出是:

ℹ 「wds」: Project is running at http://localhost:8080/
ℹ 「wds」: webpack output is served from /
ℹ 「wds」: Content not from webpack is served from /Users/me/projects/react/production
ℹ 「wdm」: Hash: 1186927fe343142edc70
Version: webpack 4.29.3
Time: 1120ms
Built at: 2019-02-13 19:41:10
                       Asset       Size  Chunks             Chunk Names
                  index.html  433 bytes          [emitted]  
main.3bb79f4b9e2925734f50.js   1.64 MiB    main  [emitted]  main
Entrypoint main = main.3bb79f4b9e2925734f50.js
[0] multi (webpack)-dev-server/client?http://localhost:8080 ./src/index.tsx 40 bytes {main} [built]
...
ℹ 「wdm」: Compiled successfully.

它说没有任何错误。但是代码中存在TS错误。

如果我将 babel-loader 更改为 ts-loader,我将拥有:

ℹ 「wds」: Project is running at http://localhost:8080/
ℹ 「wds」: webpack output is served from /
ℹ 「wds」: Content not from webpack is served from /Users/me/projects/react/production
✖ 「wdm」: Hash: 90ec6ae13f842d672d2d
Version: webpack 4.29.3
Time: 1941ms
Built at: 2019-02-13 19:42:35
                       Asset       Size  Chunks             Chunk Names
                  index.html  433 bytes          [emitted]  
main.90f2073400581ecd9e5b.js   1.59 MiB    main  [emitted]  main
Entrypoint main = main.90f2073400581ecd9e5b.js

...

ERROR in /Users/me/projects/react/src/actions/index.ts
./src/actions/index.ts
[tsl] ERROR in /Users/me/projects/react/src/actions/index.ts(3,28)
      TS7006: Parameter 'type' implicitly has an 'any' type.

ERROR in /Users/me/projects/react/src/actions/index.ts
./src/actions/index.ts
[tsl] ERROR in /Users/me/projects/react/src/actions/index.ts(3,34)
      TS7019: Rest parameter 'argNames' implicitly has an 'any[]' type.

...

ℹ 「wdm」: Failed to compile.

因此,ts-loader 会显示错误。

如何为@babel/typescript 启用错误抛出?

最佳答案

据我了解,Babel 团队在开发时禁用了类型检查:

https://iamturns.com/typescript-babel

How does Babel handle TypeScript code? It removes it.

Yep, it strips out all the TypeScript, turns it into “regular” JavaScript, and continues on its merry way.

但是有一个解决方法 fork-ts-checker-webpack-plugin

作为依赖安装即可

npm install --save-dev fork-ts-checker-webpack-plugin

并更新你的 webpack 配置:

const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');

const webpackConfig = {
  ...
  plugins: [
    new ForkTsCheckerWebpackPlugin()
  ]
};

之后,您将在单独的进程中运行静态类型检查,这非常棒,因为它可以显着加快您的构建速度。

关于javascript - @babel/typescript 在 webpack 构建时不会抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54675587/

相关文章:

javascript - jQuery .each() 不更新

javascript - Underscore.js 的 _.map() 和 JavaScript 的原生 .map() 之间是否存在实质性的功能、性能或兼容性差异?

reactjs - React - 跨组件共享功能的最佳实践

node.js - 尽管文件监视正在工作,但无法在浏览器中启用带有react-hot的HotModuleReplace插件

javascript - 找不到 "process.env.NODE_ENV"

javascript - 使用 window.opener 调用 javascript 函数是一个好的做法吗?

javascript - Node.js - 语法错误 : Unexpected token import while running on node 6. 10.2

javascript - 应用程序路由优先于模块路由

node.js - TypeScript 编译 node-postgres 的问题

javascript - 如何使用 Webpack 生成和提供网站图标?