TypeScript 学习总结 —— Webpack 中配置使用 TS
原创
©著作权归作者所有:来自51CTO博客作者叹之的原创作品,请联系作者获取转载授权,否则将追究法律责任
npm init -y
npm i -D webpack webpack-cli typescript ts-loader
- 编写
webpack
配置文件:webpack.config.js
const path = require('path')
module.exports = {
entry: './src/index.ts',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.ts$/,
use: 'ts-loader',
exclude: /node_modules/
}
]
},
resolve: {
extensions: ['.ts', 'js']
}
}
- 编写
typescript
配置文件:tsconfig.json
{
"compilerOptions": {
"module": "ES2015",
"target": "ES2015",
"strict": true
}
}
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack"
}
- 当 使用
npm run build
后,npm
就会运行 webpack
进行打包流程,借助 ts-loader
,加载 ts
文件,并在 typescript
的帮助下编译成 js