国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

基于 TypeScript 開發 NPM 模塊

songjz / 914人閱讀

摘要:初始化項目添加開發基礎包添加添加測試工具添加初始化配置這會在你的項目根目錄新建一個文件現在的目錄結構如下文件解析這是的配置文件,默認僅啟用了幾項,我一般的配置如下添加了幾條添加文件內

初始化 NPM 項目
mkdir project-name
cd project-name
npm init
添加開發基礎包

添加 TypeScript

yarn add typescript -D

添加 Jest 測試工具

yarn add jest ts-jest @types/jest -D

添加 @types/node

yarn add @types/node -D
初始化 TypeScript 配置
./node_modules/.bin/tsc --init

這會在你的項目根目錄新建一個 tsconfig.json 文件

現在的目錄結構如下:

.
├── node_modules
├── package.json
├── tsconfig.json
└── yarn.lock
文件解析 tsconfig.json

這是 TypeScript 的配置文件,默認僅啟用了幾項,我一般的配置如下:

{
  "compilerOptions": {
    /* Basic Options */
    "target": "es5" /* Specify ECMAScript target version: "ES3" (default), "ES5", "ES2015", "ES2016", "ES2017", "ES2018", "ES2019" or "ESNEXT". */,
    "module": "commonjs" /* Specify module code generation: "none", "commonjs", "amd", "system", "umd", "es2015", or "ESNext". */,
    "lib": [
      "es6"
    ] /* Specify library files to be included in the compilation. */,
    // "allowJs": true,                       /* Allow javascript files to be compiled. */
    // "checkJs": true,                       /* Report errors in .js files. */
    // "jsx": "preserve",                     /* Specify JSX code generation: "preserve", "react-native", or "react". */
    "declaration": true /* Generates corresponding ".d.ts" file. */,
    "declarationMap": true /* Generates a sourcemap for each corresponding ".d.ts" file. */,
    // "sourceMap": true,                     /* Generates corresponding ".map" file. */
    // "outFile": "./",                       /* Concatenate and emit output to single file. */
    "outDir": "./dist" /* Redirect output structure to the directory. */,
    // "rootDir": "./",                       /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
    // "composite": true,                     /* Enable project compilation */
    // "incremental": true,                   /* Enable incremental compilation */
    // "tsBuildInfoFile": "./",               /* Specify file to store incremental compilation information */
    // "removeComments": true,                /* Do not emit comments to output. */
    // "noEmit": true,                        /* Do not emit outputs. */
    // "importHelpers": true,                 /* Import emit helpers from "tslib". */
    // "downlevelIteration": true,            /* Provide full support for iterables in "for-of", spread, and destructuring when targeting "ES5" or "ES3". */
    // "isolatedModules": true,               /* Transpile each file as a separate module (similar to "ts.transpileModule"). */

    /* Strict Type-Checking Options */
    "strict": true /* Enable all strict type-checking options. */,
    "noImplicitAny": true /* Raise error on expressions and declarations with an implied "any" type. */,
    "strictNullChecks": true /* Enable strict null checks. */,
    "strictFunctionTypes": true /* Enable strict checking of function types. */,
    "strictBindCallApply": true /* Enable strict "bind", "call", and "apply" methods on functions. */,
    "strictPropertyInitialization": true /* Enable strict checking of property initialization in classes. */,
    "noImplicitThis": true /* Raise error on "this" expressions with an implied "any" type. */,
    "alwaysStrict": true /* Parse in strict mode and emit "use strict" for each source file. */,

    /* Additional Checks */
    "noUnusedLocals": true /* Report errors on unused locals. */,
    "noUnusedParameters": true /* Report errors on unused parameters. */,
    "noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
    "noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */,

    /* Module Resolution Options */
    // "moduleResolution": "node",            /* Specify module resolution strategy: "node" (Node.js) or "classic" (TypeScript pre-1.6). */
    // "baseUrl": "./",                       /* Base directory to resolve non-absolute module names. */
    // "paths": {},                           /* A series of entries which re-map imports to lookup locations relative to the "baseUrl". */
    // "rootDirs": [],                        /* List of root folders whose combined content represents the structure of the project at runtime. */
    // "typeRoots": [],                       /* List of folders to include type definitions from. */
    // "types": [],                           /* Type declaration files to be included in compilation. */
    // "allowSyntheticDefaultImports": true,  /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
    "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies "allowSyntheticDefaultImports". */
    // "preserveSymlinks": true,              /* Do not resolve the real path of symlinks. */

    /* Source Map Options */
    // "sourceRoot": "",                      /* Specify the location where debugger should locate TypeScript files instead of source locations. */
    // "mapRoot": "",                         /* Specify the location where debugger should locate map files instead of generated locations. */
    // "inlineSourceMap": true,               /* Emit a single file with source maps instead of having a separate file. */
    // "inlineSources": true,                 /* Emit the source alongside the sourcemaps within a single file; requires "--inlineSourceMap" or "--sourceMap" to be set. */

    /* Experimental Options */
    // "experimentalDecorators": true,        /* Enables experimental support for ES7 decorators. */
    // "emitDecoratorMetadata": true,         /* Enables experimental support for emitting type metadata for decorators. */
  },
  "include": ["./src/**/*"],
  "exclude": ["node_modules", "src/__tests__"]
}
package.json

添加了幾條 scripts

{
  "name": "project-name",
  "version": "0.0.0-development",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "rm -rf ./dist && tsc",
    "watch": "yarn test --watch",
    "setup": "yarn install && npx npm-install-peers",
    "test": "jest"
  },
  "author": "",
  "license": "SEE LICENSE IN LICENSE FILE",
  "devDependencies": {
    "@types/jest": "^24.0.13",
    "@types/node": "^12.0.3",
    "jest": "^24.8.0",
    "ts-jest": "^24.0.2",
    "typescript": "^3.4.5"
  }
}
添加 jest.config.js 文件

內容如下:

module.exports = {
  // We always recommend having all TypeScript files in a src folder in your project. We assume this is true and specify this using the roots option.
  roots: ["/src"],
  transform: {
    "^.+.tsx?$": "ts-jest" //The transform config just tells jest to use ts-jest for ts / tsx files.
  }
};
添加 .gitignore 文件

該文件告訴 git 哪些文件不需要跟蹤

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of "npm pack"
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# next.js build output
.next

# map
*.map

# gitbook
_book
初始化 git 項目
git init
jest watch 需要有 git 環境才能運行
添加 src/index.ts 文件

內容如下:

export const sum = (a: number, b: number): number => a + b;
添加 src/__tests__

按我們的 jest 測試用例:src/__tests__/index.test.ts,內容如下:

import { sum } from "..";

test("sum", () => {
  expect(sum(1, 2)).toBe(3);
});
開始開發
yarn jest

此時終端顯示如下信息:

 PASS  src/__tests__/index.test.ts
  ? sum (15ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        2.133s
Ran all test suites related to changed files.

Watch Usage
 ? Press a to run all tests.
 ? Press f to run only failed tests.
 ? Press p to filter by a filename regex pattern.
 ? Press t to filter by a test name regex pattern.
 ? Press q to quit watch mode.
 ? Press Enter to trigger a test run.

現在修改一下 index.ts,文件內容改為下面這樣:

export const sum = (a: number, b: number): number => a + b;

export const multiply = (a: number, b: number): number => a * b;

再修改 __tests__/index.test.ts,內容如下:

import { sum, multiply } from "..";

test("sum", () => {
  expect(sum(1, 2)).toBe(3);
});

test("multiply", () => {
  expect(multiply(2, 3)).toBe(6);
});

會發現終端會自動測試你的代碼:

 PASS  src/__tests__/index.test.ts
  ? sum (4ms)
  ? multiply

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        4.164s
Ran all test suites related to changed files.

完整的代碼可以在下面看到:

typescript-module-boilerplate

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/104246.html

相關文章

  • 前端常用技術概述--Less、typescript與webpack

    摘要:前言講起前端,我們就不能不講與,在這兩種技術廣泛應用的今天,他們的擴展也是層出不窮,的擴展有等,的超集有等。如下注意你的樣式文件一定要在引入前先引入。截止目前,已經發布了最新的版本。 前言:講起前端,我們就不能不講CSS與Javascript,在這兩種技術廣泛應用的今天,他們的擴展也是層出不窮,css的擴展有Less、Sass、Stylus等,js的超集有Typescript等。今天我們就簡...

    番茄西紅柿 評論0 收藏0
  • React的移動端和PC端生態圈的使用匯總

    摘要:調用通過注冊表調用到實例,透過的,調用到中的,最后通過,調用,根據參數相應模塊執行。京東的,多端解決方案是一套遵循語法規范的多端開發解決方案。 showImg(https://segmentfault.com/img/bVbuMkw?w=1304&h=808); 對于一項技術,我們不能停留在五分鐘狀態,特別喜歡一句話,用什么方式繪制UI界面一點不重要,重要的是底層的思維,解決問題和優化...

    kun_jian 評論0 收藏0
  • React的移動端和PC端生態圈的使用匯總

    摘要:調用通過注冊表調用到實例,透過的,調用到中的,最后通過,調用,根據參數相應模塊執行。京東的,多端解決方案是一套遵循語法規范的多端開發解決方案。 showImg(https://segmentfault.com/img/bVbuMkw?w=1304&h=808); 對于一項技術,我們不能停留在五分鐘狀態,特別喜歡一句話,用什么方式繪制UI界面一點不重要,重要的是底層的思維,解決問題和優化...

    J4ck_Chan 評論0 收藏0
  • React的移動端和PC端生態圈的使用匯總

    摘要:調用通過注冊表調用到實例,透過的,調用到中的,最后通過,調用,根據參數相應模塊執行。京東的,多端解決方案是一套遵循語法規范的多端開發解決方案。 showImg(https://segmentfault.com/img/bVbuMkw?w=1304&h=808); 對于一項技術,我們不能停留在五分鐘狀態,特別喜歡一句話,用什么方式繪制UI界面一點不重要,重要的是底層的思維,解決問題和優化...

    Travis 評論0 收藏0

發表評論

0條評論

songjz

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<