后面会写一些Typescript相关的一些内容,会先从简单的环境搭建,基本类型,枚举类型,类,接口等基础开始。再慢慢记录一些实战内容。
项目目录结构:
1、安装TS
TS 安装可以选择全局安装
npm i typescript -g
2、TS config
tsc --init
创建成功以后会在跟目录下创建一个tsconfig.json
3、package.json
npm init
会创建一个package.json文件。可以拷贝下面的内容,然后执行npm install 安装
{
"name": "Typescript",
"version": "1.0.0",
"description": "",
"main": "./src/index.ts",
"scripts": {
"start": "webpack-dev-server --mode=development --config ./build/webpack.config.js",
"build": "webpack --mode=production --config ./build/webpack.config.js",
"lint": "eslint src --ext .js,.ts",
"test": "jest"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@types/jest": "^24.0.15",
"@types/jquery": "^3.3.29",
"@types/source-map": "^0.5.2",
"@typescript-eslint/eslint-plugin": "^1.10.2",
"@typescript-eslint/parser": "^1.10.2",
"awesome-typescript-loader": "^5.2.1",
"clean-webpack-plugin": "^3.0.0",
"eslint": "^5.16.0",
"fork-ts-checker-webpack-plugin": "^1.3.7",
"html-webpack-plugin": "^3.2.0",
"jest": "^24.8.0",
"ts-jest": "^24.0.2",
"ts-loader": "^6.0.2",
"typescript": "^3.5.1",
"webpack": "^4.32.2",
"webpack-cli": "^3.3.2",
"webpack-dev-server": "^3.5.1",
"webpack-merge": "^4.2.1"
}
}
创建index.html
创建src/tpl/index.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>TypeScript in Action</title>
</head>
<body>
<div class="app"></div>
</body>
</html>
创建ts文件
新建src, 新建index.ts
let text: string = 'Hello Typescript';
document.querySelectorAll('.app')[0].innerHTML = hello;
webpack 配置
创建build文件。新建webpack.base.config.js, webpack.config.js, webpack.dev.config.js, webpack.pro.config.js
webpack.base.config.js:
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
entry: './src/index.ts',
output: {
filename: 'app.js'
},
resolve: {
extensions: ['.js', '.ts', '.tsx']
},
module: {
rules: [
{
test: /\.tsx?$/i,
use: [{
loader: 'ts-loader'
}],
exclude: /node_modules/
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: './src/tpl/index.html'
})
]
}
webpack.config.js:
const merge = require('webpack-merge')
const baseConfig = require('./webpack.base.config')
const devConfig = require('./webpack.dev.config')
const proConfig = require('./webpack.pro.config')
module.exports = (env, argv) => {
let config = argv.mode === 'development' ? devConfig : proConfig;
return merge(baseConfig, config);
};
webpack.dev.config.js:
module.exports = {
devtool: 'cheap-module-eval-source-map'
}
webpack.pro.config.js:
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
module.exports = {
plugins: [
new CleanWebpackPlugin()
]
}
启动
npm run start
|
请发表评论