第一篇:使用Typescript开发Nodejs
<p>这是实现IoC系列的第一步,因为是基于TypeScript的,所以首先我们来看一下如何使用TypeScript来开发NodeJs项目</p><p>TypeScript已经得到广泛的应用,一般开发Nodejs后端时都会使用成熟的框架,比如Nestjs,但是一些小工具,就没有必要使用框架了,但是又想使用typescript开发,网上有很多关于使用typescript来开发Nodejs应用的教程,我这里做下记录,可能的话会写给npm包,一条命令生成应用的骨架。</p>
<p> </p>
<h2>一. npm init: 生成应用骨架</h2>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">npm init
</pre>
</div>
<p> 会在当前目录下生成package.json文件</p>
<h2>二. 安装typescript</h2>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">npm install typescript --save</pre>
</div>
<h2> 三.初始化typescript环境</h2>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">.\node_modules\.bin\tsc --init
</pre>
</div>
<p> 生成默认的tsconfig.json文件</p>
<h2>四.修改默认的tsconfig.json文件</h2>
<ol>
<li>项目中会使用到一些es6的语法,比如Symbol,所以需要在tsconfig.json的lib中添加ES2015</li>
<li>项目中会使用到console等命令,使用需要在lib中添加dom</li>
<li>默认的tsc编译文件的输出是当前路径,我们修改为dist</li>
<li>Debug时需要用到sourcemap,打开sourcemap</li>
<li>项目中还会使用到装饰器和反射的metadata这个实验性技术,所以需要打开experimentalDecorators和emitDecoratorMetadata</li>
<li>项目中使用的代码都是放在src下面,所以在与compilerOptions平级的地方加上include: ["src/**/*"]</li>
</ol>
<div class="cnblogs_code"><img id="code_img_closed_79bcb232-6e7b-403d-8bb6-4ae1b7fe0352" class="code_img_closed lazyload" data-src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif"><img id="code_img_opened_79bcb232-6e7b-403d-8bb6-4ae1b7fe0352" class="code_img_opened lazyload" style="display: none" data-src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif">
<div id="cnblogs_code_open_79bcb232-6e7b-403d-8bb6-4ae1b7fe0352" class="cnblogs_code_hide">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span> "compilerOptions"<span style="color: rgba(0, 0, 0, 1)">: {
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span> <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> Visit https://aka.ms/tsconfig.json to read more about this file </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)"> 4</span>
<span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> Basic Options </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)"> 6</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "incremental": true, /* Enable incremental compilation */</span>
<span style="color: rgba(0, 128, 128, 1)"> 7</span> "target": "es5", <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)"> 8</span> "module": "commonjs", <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)"> 9</span> "lib": ["ES2015", "DOM"], <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> Specify library files to be included in the compilation. </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">10</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "allowJs": true, /* Allow javascript files to be compiled. */</span>
<span style="color: rgba(0, 128, 128, 1)">11</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "checkJs": true, /* Report errors in .js files. */</span>
<span style="color: rgba(0, 128, 128, 1)">12</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */</span>
<span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "declaration": true, /* Generates corresponding '.d.ts' file. */</span>
<span style="color: rgba(0, 128, 128, 1)">14</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */</span>
<span style="color: rgba(0, 128, 128, 1)">15</span> "sourceMap": <span style="color: rgba(0, 0, 255, 1)">true</span>, <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> Generates corresponding '.map' file. </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">16</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "outFile": "./", /* Concatenate and emit output to single file. */</span>
<span style="color: rgba(0, 128, 128, 1)">17</span> "outDir": "dist", <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> Redirect output structure to the directory. </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">18</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */</span>
<span style="color: rgba(0, 128, 128, 1)">19</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "composite": true, /* Enable project compilation */</span>
<span style="color: rgba(0, 128, 128, 1)">20</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */</span>
<span style="color: rgba(0, 128, 128, 1)">21</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "removeComments": true, /* Do not emit comments to output. */</span>
<span style="color: rgba(0, 128, 128, 1)">22</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "noEmit": true, /* Do not emit outputs. */</span>
<span style="color: rgba(0, 128, 128, 1)">23</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "importHelpers": true, /* Import emit helpers from 'tslib'. */</span>
<span style="color: rgba(0, 128, 128, 1)">24</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */</span>
<span style="color: rgba(0, 128, 128, 1)">25</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */</span>
<span style="color: rgba(0, 128, 128, 1)">26</span>
<span style="color: rgba(0, 128, 128, 1)">27</span> <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> Strict Type-Checking Options </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">28</span> "strict": <span style="color: rgba(0, 0, 255, 1)">true</span>, <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> Enable all strict type-checking options. </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">29</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */</span>
<span style="color: rgba(0, 128, 128, 1)">30</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "strictNullChecks": true, /* Enable strict null checks. */</span>
<span style="color: rgba(0, 128, 128, 1)">31</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "strictFunctionTypes": true, /* Enable strict checking of function types. */</span>
<span style="color: rgba(0, 128, 128, 1)">32</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */</span>
<span style="color: rgba(0, 128, 128, 1)">33</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "strictPropertyInitialization": true,/* Enable strict checking of property initialization in classes. */</span>
<span style="color: rgba(0, 128, 128, 1)">34</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */</span>
<span style="color: rgba(0, 128, 128, 1)">35</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */</span>
<span style="color: rgba(0, 128, 128, 1)">36</span>
<span style="color: rgba(0, 128, 128, 1)">37</span> <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> Additional Checks </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">38</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "noUnusedLocals": true, /* Report errors on unused locals. */</span>
<span style="color: rgba(0, 128, 128, 1)">39</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "noUnusedParameters": true, /* Report errors on unused parameters. */</span>
<span style="color: rgba(0, 128, 128, 1)">40</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */</span>
<span style="color: rgba(0, 128, 128, 1)">41</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */</span>
<span style="color: rgba(0, 128, 128, 1)">42</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */</span>
<span style="color: rgba(0, 128, 128, 1)">43</span>
<span style="color: rgba(0, 128, 128, 1)">44</span> <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> Module Resolution Options </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">45</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */</span>
<span style="color: rgba(0, 128, 128, 1)">46</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "baseUrl": "./", /* Base directory to resolve non-absolute module names. */</span>
<span style="color: rgba(0, 128, 128, 1)">47</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */</span>
<span style="color: rgba(0, 128, 128, 1)">48</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */</span>
<span style="color: rgba(0, 128, 128, 1)">49</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "typeRoots": [], /* List of folders to include type definitions from. */</span>
<span style="color: rgba(0, 128, 128, 1)">50</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "types": [], /* Type declaration files to be included in compilation. */</span>
<span style="color: rgba(0, 128, 128, 1)">51</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "allowSyntheticDefaultImports": true,/* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */</span>
<span style="color: rgba(0, 128, 128, 1)">52</span> "esModuleInterop": <span style="color: rgba(0, 0, 255, 1)">true</span>, <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">53</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */</span>
<span style="color: rgba(0, 128, 128, 1)">54</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */</span>
<span style="color: rgba(0, 128, 128, 1)">55</span>
<span style="color: rgba(0, 128, 128, 1)">56</span> <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> Source Map Options </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">57</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */</span>
<span style="color: rgba(0, 128, 128, 1)">58</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */</span>
<span style="color: rgba(0, 128, 128, 1)">59</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */</span>
<span style="color: rgba(0, 128, 128, 1)">60</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */</span>
<span style="color: rgba(0, 128, 128, 1)">61</span>
<span style="color: rgba(0, 128, 128, 1)">62</span> <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> Experimental Options </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">63</span> "experimentalDecorators": <span style="color: rgba(0, 0, 255, 1)">true</span>, <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> Enables experimental support for ES7 decorators. </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">64</span> "emitDecoratorMetadata": <span style="color: rgba(0, 0, 255, 1)">true</span>, <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> Enables experimental support for emitting type metadata for decorators. </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">65</span>
<span style="color: rgba(0, 128, 128, 1)">66</span> <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> Advanced Options </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">67</span> "skipLibCheck": <span style="color: rgba(0, 0, 255, 1)">true</span>, <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> Skip type checking of declaration files. </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">68</span> "forceConsistentCasingInFileNames": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> Disallow inconsistently-cased references to the same file. </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 128, 1)">69</span> <span style="color: rgba(0, 0, 0, 1)">},
</span><span style="color: rgba(0, 128, 128, 1)">70</span> "include"<span style="color: rgba(0, 0, 0, 1)">: [
</span><span style="color: rgba(0, 128, 128, 1)">71</span> "src/**/*"
<span style="color: rgba(0, 128, 128, 1)">72</span> <span style="color: rgba(0, 0, 0, 1)">]
</span><span style="color: rgba(0, 128, 128, 1)">73</span> }</pre>
</div>
<span class="cnblogs_code_collapse">tsconfig.json</span></div>
<h2>五.修改package.json文件</h2>
<p> 1. 添加typescript编译命令 tsc --watch</p>
<p> 2. 添加启动项目的命令 node dist/index.js</p>
<div class="cnblogs_code"><img id="code_img_closed_a14b6504-57be-4c94-a413-787ddbc5e8e9" class="code_img_closed lazyload" data-src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif"><img id="code_img_opened_a14b6504-57be-4c94-a413-787ddbc5e8e9" class="code_img_opened lazyload" style="display: none" data-src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif">
<div id="cnblogs_code_open_a14b6504-57be-4c94-a413-787ddbc5e8e9" class="cnblogs_code_hide">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span> "name": "ts-ioc"<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span> "version": "1.0.0"<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span> "description": "this is a tool for project to use the IoC and DI"<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)"> 5</span> "main": "index.js"<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span> "scripts"<span style="color: rgba(0, 0, 0, 1)">: {
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span> "test": "echo \"Error: no test specified\" && exit 1"<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> "tsc": "tsc --watch"<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span> "start": "node dist/index.js"
<span style="color: rgba(0, 128, 128, 1)">10</span> <span style="color: rgba(0, 0, 0, 1)">},
</span><span style="color: rgba(0, 128, 128, 1)">11</span> "author": "Jason"<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)">12</span> "license": "ISC"<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)">13</span> "dependencies"<span style="color: rgba(0, 0, 0, 1)">: {
</span><span style="color: rgba(0, 128, 128, 1)">14</span> "typescript": "^4.1.3"
<span style="color: rgba(0, 128, 128, 1)">15</span> <span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 128, 1)">16</span> }</pre>
</div>
<span class="cnblogs_code_collapse">package.json</span></div>
<h2>六.启动项目</h2>
<p> 1. 先在一个窗口中编译typescript</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">npm run-script tsc</pre>
</div>
<p> 2. 打开另外一个窗口,启动项目</p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">npm start</pre>
</div>
<p>至此,简单的使用typescript来开发nodejs就完成了,还有很多需要优化的地方,比如使用webpack把启动变成一步,还有debug的步骤。</p>
<h2>七.热更新</h2>
<p> 之前启动项目需要npm run-script tsc && npm start,并且是没有热更新的,即修改完代码没有立即重新启动。</p>
<p>这里使用nodemon这个库,实现typescript代码的热更新。</p>
<div class="cnblogs_code">
<pre>nodemon -e ts --exec ts-node src/index.ts</pre>
</div>
<p>这里用到了nodemon和ts-node,所以需要先安装它们</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> npm install nodemon --save-<span style="color: rgba(0, 0, 0, 1)">dev
</span><span style="color: rgba(0, 128, 128, 1)">2</span> npm install ts-node --save-dev</pre>
</div>
<p>然后更新package.json文件</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">scripts</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">: {
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">test</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">echo \"Error: no test specified\" && exit 1</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">tsc</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">tsc --watch</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)">4</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">start</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">nodemon -e ts --exec ts-node src/index.ts</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)">5</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">webpack</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">webpack --config webpack.config.js --watch</span><span style="color: rgba(128, 0, 0, 1)">"</span>
<span style="color: rgba(0, 128, 128, 1)">6</span> },</pre>
</div>
<p>可以看到,使用了 nodemon -e ts --exec ts-node src/index.ts 来启动命令,-e ts表示这个是监听ts文件的,也可以写-e ts,tsx表示监听ts文件和tsc文件。</p>
<p>--exec表示需要使用ts-node来编译代码。</p>
<p>这样,就可以实现修改代码后自己重启了。</p>
<p> </p>
<p> 下一篇文章会实现一个简单的IoC container</p><br><br>
来源:https://www.cnblogs.com/JasonWang-code/p/14247104.html
頁:
[1]