ts-loader
has one downside: We can’t pipe the output of another loader into it; it always reads the original file. As a work-around, we can use babel-loader
to compile TypeScript. This blog post explains how.
package.json
:
{
···
"devDependencies": {
"@babel/core": "^7.6.4",
"@babel/preset-env": "^7.6.3",
"@babel/preset-react": "^7.6.3",
"@babel/preset-typescript": "^7.6.0",
"babel-loader": "^8.0.6",
···
},
"babel": {
"presets": [
"@babel/preset-env",
"@babel/preset-react",
"@babel/preset-typescript"
]
}
}
Notes:
@babel/env
came after @babel/typescript
.preset-env
: Make sure you get targeted browsers and the inclusion of builtins right (see documentation).
webpack.config.js
:
module.exports = {
···
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'babel-loader',
},
···
],
},
};