TypeScript
学习 typescript 之前当然需要让它跑在本地才行
环境配置
-
本地测试 ts (ts-node)
- npm i ts-node -g
- npm i tslib @types/node -g
之后通过 ts-node xxx.ts 来运行 ts 文件
-
本地测试 ts (webpack)
- npm init -y
- npm install webpack webpack-cli -D
- makedir ./webpack.config.js
- package.json -> scripts: { "build": "webpack", ... }
- npm install ts-loader typescript -D
- 配置 webpack.config.js
module.exports = {
...,
resolve: {
extensions: ['.ts', '.js', '.cjs', '.json'] // 自动在 import ... from 'xxx' 补全后缀
},
module: {
rules: [ // 使用对应loader 来加载 匹配到的对应文件
{
test: /\.ts$/,
loader: 'ts-loader'
}
]
}
}
- tsc --init (生成 tsconfig.json)
- npm install webpack-dev-server -D (本地服务)
- package.json -> scripts: { serve: 'webpack serve' }
- webpack.config.js
// webpack.config.js
module.exports = {
...,
devServer: { // 定义本地配置
}
}
- 新建 ./index.html (模板)
- npm install html-webpack-plugin -D
// webpack.config.js
...
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
...,
plugins: [
new HtmlWebpackPlugin({
template: './index.html'
})
]
}