【发布时间】:2020-01-06 01:09:52
【问题描述】:
我正在用 TypeScript 编写一个 npm 模块。假设我的图书馆中有这棵树:
.
├── README.md
├── dist
│ ├── index.d.ts
│ └── index.js
├── lib
│ └── index.ts
├── package-lock.json
├── package.json
├── test
│ └── test.js
└── tsconfig.json
在我的package.json 我有这个:
{
"name": "mylib",
"version": "1.0.1",
"description": "A Node.js module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc"
},
"devDependencies": {
"typescript": "^2.5.3"
}
}
在 tsconfig.json 中——默认配置——我有:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": true,
"outDir": "./dist",
"strict": true
}
}
在dist/index.ts 我有:
export function appendtext(str: string): string {
return str + ' world';
}
我把这个库称为:
var mylib = require('mylib');
var result = mylib.appendtext('Hello');
问题1:当我从我的应用程序myapp 调用mylib 时,myapp 不会构建它;它只会使用dist/ 处的编译代码。这是正确的吗?
问题2:如果1正确,那么我可以将lib/index.ts的源代码拆分成不同的模块,在开发mylib的时候用WebPack编译。生成的代码将保存在dist/,这是myapp 将使用的代码。所以,myapp 不需要编译mylib 的模块,因为它会使用dist/ 已经链接的代码。
感谢您的澄清!
【问题讨论】:
-
当你使用 ts 和 node..通常
import * as something from 'something';..也在你的 package.json 中:你可以试试"scripts": { "build": "tsc -w" } -
你还需要显示你的
tsconfig.json是什么@ -
您好,谢谢!添加了基本的 tsconfig.json 信息。
标签: javascript node.js typescript npm webpack