这是使用 TypeScript 1.8.10 的更新答案:
我的项目结构是:
|
|--- src
|--- test
|--- dist <= My gulp file compiles and places the js, sourcemaps and .d.ts files here
| |--- src
| |--- test
|--- typings
.gitignore
.npmignore
gulpfile.js
package.json
README.md
tsconfig.json
tslint.json
typings.json
我在.npmignore 中添加了以下内容,以避免包含无关文件并保持最低限度的导入和工作包:
node_modules/
*.log
*.tgz
src/
test/
gulpfile.js
tsconfig.json
tslint.json
typings.json
typings
dist/test
我的.gitignore 有:
typings
# ignore .js.map files
*.js.map
*.js
dist
我的package.json 有:
"main": "dist/src/index.js",
"typings": "dist/src/index.d.ts",
现在我运行:
npm pack
生成的文件(解压缩时)具有以下结构:
|
|--- dist
| |--- src
| |
| index.js
| index.js.map
| index.d.ts
|
package.json
README.md
现在我转到我想将其用作库的项目并键入:
npm install ./project-1.0.0.tgz
安装成功。
现在我在我刚刚安装 npm 的项目中创建了一个文件 index.ts
import Project = require("project");
输入 Project. 会给我 Intellisense 选项,这是整个练习的重点。
希望这有助于其他人将他们的 TypeScript npm 项目用作更大项目中的内部库。
PS:我相信这种将项目编译为可用于其他项目的 npm 模块的方法让人想起 .NET 世界中的 .dll。我可以想象在 VS Code 中的解决方案中组织项目,其中每个项目生成一个 npm 包,然后可以在解决方案中的另一个项目中将其用作依赖项。
由于我花了相当多的时间才弄清楚这一点,所以我已经发布了它以防有人被困在这里。
我还在以下位置发布了一个已关闭的错误:
https://github.com/npm/npm/issues/11546
这个例子已经上传到 Github:vchatterji/tsc-seed