npm i typescript -D

npm i ts-loader -D

Initialize the tsconfig.json file

npx tsc –init

Tsconfig. json file contents:

Configuration in webpack.config.js

Matters needing attention:

Be sure to configure: it can omit the extension when importing TS files. Otherwise, the file will not be found without the extension.

extensions: [‘.js’, ‘.css’, ‘.ts’, ‘.tsx’]

Test it by writing a.ts file

The introduction of Babel – loader

At this point, the ts file is compiled with ts-loader. So far, babel-loader can also compile TS files, so we can uninstall ts-loader. The configuration of babel7 is explained in the webpack configuration, so we just need to configure it on this basis.

npm i @babel/preset-typescript -D

This is a Babel preset, which contains all tScript compiler plug-ins.

In webpack. Config. Js

Configure it in tsconfig.json

The test file

Both TS and Polyfill were found to work normally.

At this point, js and TS should be merged with the configuration parsed by babel-loader into the webpack.config.js file