Av中文字幕乱码免费看,日本免费大黄在线观看,影音先锋中文字幕亚洲资源站,99免费在线观看精品视频

        
        
      1. 新疆信息港歡迎您!

        新疆信息港
        新疆信息港 > 大數(shù)據(jù) >Typescript tsconfig.json 詳解

        Typescript tsconfig.json 詳解

        2020-03-23 08:16:59
        來源:互聯(lián)網(wǎng)
        閱讀:-

        環(huán)境搭建安裝tsnpm i -g typescript初始化工程mkdir ts-demonpm init -y 安裝rollupnpm i -g rollupnpm i rollup -D添加rollup.config.jstouch rollup.config....

        環(huán)境搭建

        安裝ts

        npm i -g typescript

        初始化工程

        mkdir ts-demo
        npm init -y

        安裝rollup

        npm i -g rollup
        npm i rollup -D

        添加rollup.config.js

        touch rollup.config.js 
        npm i rollup-plugin-json -D
        npm i rollup-plugin-typescript typescript tslib -D

        import json from 'rollup-plugin-json';
        import typescript from 'rollup-plugin-typescript';

        export default {
        input: 'src/main.ts',
        output: {
        file: 'dist/app.js',
        format: 'cjs'
        },
        plugins: [ typescript() ]
        };

        package.json

        {
        "name": "ts-demo",
        "version": "1.0.0",
        "description": "",
        "main": "index.js",
        "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "dev-build": "rollup -c",
        "dev": "npm run dev-build && node ./dist/app.js"
        },
        "keywords": [],
        "author": "",
        "license": "ISC",
        "devDependencies": {
        "rollup": "^1.27.5",
        "rollup-plugin-json": "^4.0.0",
        "rollup-plugin-typescript": "^1.0.1",
        "tslib": "^1.10.0",
        "typescript": "^3.7.2"
        }
        }

        main.ts

        // src/main.ts
        function greeter(person: string):string {
        return "Hello, " + person;
        }

        const user = "Jane User,this is js hello from ts";

        document.body.textContent = greeter(user);

        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>Document</title>

        </head>
        <body>

        <script src="./app.js"></script>

        </body>
        </html>
        • 打開index.html, 界面出現(xiàn)Hello, Jane User,this is js hello from ts ,恭喜你,你已經(jīng)typescript入門了?。。?/li>

        typescript配置文件

        typescript支持兩種配置文件:

        • tsconfig.json
        • xxx.json,其中包含其需要的配置項(xiàng)
          下方將側(cè)重介紹tsconfig.json

        http://json.schemastore.org/tsconfig

        {
        "files": [ # 指定需要編譯文件,相對配置文件所在
        "core.ts",
        "sys.ts",
        "types.ts",
        "scanner.ts",
        "parser.ts",
        "utilities.ts",
        "binder.ts",
        "checker.ts",
        "emitter.ts",
        "program.ts",
        "commandLineParser.ts",
        "tsc.ts",
        "diagnosticInformationMap.generated.ts"
        ],
        "exclude": [ # 指定不需要編譯文件
        "node_modules",
        "**/*.spec.ts"
        ],
        "include": [ # 指定需要編譯文件; 不配置files,include,默認(rèn)除了exclude的所有.ts,.d.ts,.tsx
        "src/**/*"
        ],
        # 指定基礎(chǔ)配置文件路徑 大部分配置 compilerOptions, files, include, and exclude。切忌循環(huán)引用。
        "extends": "./configs/base",
        "compilerOptions": { # 告知TypeScript 編譯器怎么編譯
        "baseUrl": "./",
        "paths": { # 相對于baseUrl配置
        "jquery": ["node_modules/jquery/dist/jquery"] ,
        "*": [
        "*",
        "generated/*"
        ]
        },
        "rootDirs":[ # 找平路徑配置依賴
        "src/views",
        "generated/templates/views"
        ],
        "module": "commonjs",
        "noImplicitAny": true,
        "removeComments": true, # 移除代碼注解
        "preserveConstEnums": true,
        "sourceMap": true
        "types": [] #不會(huì)自動(dòng)導(dǎo)入@types定義的包
        "noResolve":true , # 不會(huì)自動(dòng)導(dǎo)入import 依賴, 編譯會(huì)報(bào)錯(cuò)
        "downlevelIteration":true # 進(jìn)行js 語法降級 for..of
        "module": "esnext",
        "moduleResolution": "node",
        "strictNullChecks": true # 開啟null,檢測
        "target":'ES5'
        "strictBindCallApply":true
        "skipLibCheck":true,
        },
        # 以上屬性,為常用配置屬性
        "compileOnSave": false, # 整個(gè)工程而言,需要編譯器支持,譬如Visual Studio 2015 with TypeScript 1.8.4+
        "typeAcquisition":{ # 整個(gè)工程的類型定義.d.ts
        "enable":false, # 默認(rèn)值 false
        "include" : ["jquery", "lodash"]
        "exclue":["jquery", "lodash" ]
        },
        "references":[{ # 引用的工程
        path: 'xxxx'
        }]
        }

        其中,

        • 相對路徑,都是相對配置文件所在路徑
        • 優(yōu)先級:命令行配置 > files > exclude > include 。
        • exclude默認(rèn)為:nodemodules, bowercomponents, jspm_packages and outDir配置項(xiàng)
        • A.ts 引用B.ts ; A.ts在files 或者include中,B.ts也會(huì)被默認(rèn)導(dǎo)入。
        • 一個(gè)路徑或者一個(gè)文件,在include與exclude之間是互斥的。
        • typeRoots與@types互斥,types、@types也可以互斥。

        移除代碼中注釋

        {
        "files": [
        "src/main.ts"
        ],
        "compilerOptions": {
        "removeComments": true,
        }
        }
        // main.ts
        //add the person type
        interface Person{
        firstName: string;
        lastName: string;
        }
        class Student{
        firstName: string;
        lastName: string;
        constructor(firstName:string , lastName: string){
        this.firstName = firstName;
        this.lastName = lastName;
        }
        }
        // add the greeter
        function greeter(person: Person):string {
        return `Hello,${person.firstName}:${person.lastName}`
        }
        //new a student
        const user = new Student('xiaoming','hello');

        document.body.textContent = greeter(user);
        //編譯后
        'use strict';

        var Student = (function () {
        function Student(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
        }
        return Student;
        }());
        function greeter(person) {
        return "Hello," + person.firstName + ":" + person.lastName;
        }
        var user = new Student('xiaoming', 'hello');
        document.body.textContent = greeter(user);

        開啟null、undefined檢測

        {
        "files": ["./src/main.ts"],
        "compilerOptions": {
        "removeComments": true,
        "declaration": true,
        "declarationDir": "./",
        "noResolve": false,
        "strictNullChecks": true
        },
        }
        const user ;
        user = new Student('xiaoming','hello'); # 編譯報(bào)錯(cuò)

        參考文獻(xiàn)

        • www.rollupjs.com/
        • www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html
        • github.com/rollup/rollup-plugin-typescript
        • json.schemastore.org/tsconfig

        本文作者:前端首席體驗(yàn)師(CheongHu)

        聯(lián)系郵箱:simple2012hcz@126.com

        推薦閱讀:查找我的iphone在哪里打開

        免責(zé)聲明:本文僅代表企業(yè)觀點(diǎn),與新疆信息港無關(guān)。其原創(chuàng)性以及文中陳述文字和內(nèi)容未經(jīng)本站證實(shí),對本文以及其中全部或者部分內(nèi)容、文字的真實(shí)性、完整性、及時(shí)性本站不作任何保證或承諾,請讀者僅作參考,并請自行核實(shí)相關(guān)內(nèi)容。
        熱門圖片
        熱門搜索