[英]@babel/typescript doesn't throw errors while webpack build
我試圖用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
Babel如何處理TypeScript代碼? 它刪除它。
是的,它刪除了所有TypeScript,將其轉換為“常規”JavaScript,並繼續以其快樂的方式。
但是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()
]
};
之后,您將在一個單獨的進程中運行靜態類型檢查,這很棒,因為它可以加速您的構建速度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.