hello-publishing-ts2

typescript模块发布 ``` npm init -y # 生成package.json tsc --init # 生成tsconfig.json ``` 修改tsconfig.json, ``` { "compilerOptions": { "module": "commonjs", "moduleResolution": "node", "target": "es6", "rootDir": "src", "outDir": "dist", "

Usage no npm install needed!

<script type="module">
  import helloPublishingTs2 from 'https://cdn.skypack.dev/hello-publishing-ts2';
</script>

README

typescript模块发布

npm init -y # 生成package.json
tsc --init # 生成tsconfig.json

修改tsconfig.json,

{
  "compilerOptions": {
    "module": "commonjs",
    "moduleResolution": "node",
    "target": "es6",
    "rootDir": "src",
    "outDir": "dist",
    "sourceMap": true,
    "declaration": true
  }
}

修改package.json

{
  "main": "dist/math.js",
  "typings": "dist/math.d.ts",
  "scripts": {
    "compile": "rm -rf dist && tsc",
    "prepublish": "npm run compile"
  }
}

npm publish 发布模块,会先在dist生成math.js, math.d.ts, math.js.map,只对dist,package,README.md上传,而src不需要。

测试代码:

npm i @types/chai @types/mocha chai mocha -D

在package.json,增加

  "scripts": {
    "test": "mocha --require ts-node/register test/*"
  },

在tsconfig.json,增加

  "exclude": [
    "test"
  ]

否则出现error TS6059: 'rootDir' is expected to contain all source files.