• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

【Typescript】环境搭建(1)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

后面会写一些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

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TypeScript 学习笔记(一)—— 安装发布时间:2022-07-18
下一篇:
TypeScript namespace 命名空间发布时间:2022-07-18
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap