我想将应用程序划分为不同的节点模块,并拥有一个主模块,该主模块也可以构建所有其他模块,并且我想将Typescript与es6模块一起使用。
这是我计划的项目结构:
我希望能够在可以在dep-a,dep-b和main中使用的框架中定义接口。
如何正确设置?我可以从主模块编译所有内容吗?我是否需要为框架,dep-a,...和另一个键入文件创建不同的包?最好的方法是什么?
我已经设置了一些测试文件和文件夹,并使用npm链接来链接依赖项,并使用webpack捆绑了文件,但我始终遇到找不到文件的问题:
error TS2307: Cannot find module 'framework/interfaces/IComponent'
和
Module not found: Error: Cannot resolve 'file' or 'directory' ./components/test
TL; DR使用declaration: true
in生成模块的声明,tsconfig.json
并在文件typings
条目中为生成的类型指定package.json
文件
使用tsconfig
与此类似的文件:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": true,
"noImplicitAny": true,
"removeComments": true,
"outDir": "dist",
...
},
"files": [
...
]
}
重要的是declaration: true
它将在dist目录中生成内部声明
假设有一个index.ts
文件(重新)导出的所有有趣部分framework
,创建一个package.json
文件,其中包含一个main
和typings
条目,分别指向生成的js和生成的声明,即
{
"name": "framework",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
...
}
将此模块提交到git仓库中,在以下位置说出bitbucket:“ https://[email protected]/myUser/framework.git ”
在package.json
创建对...的依赖framework
{
"dependencies": {
"framework": "https://[email protected]/myUser/framework.git"
},
}
这就对了。
import * from 'framework'
会自动通过输入拉出依赖项
显然,可以使用dep-a完成框架的工作,即生成声明,更新package.json并将dep-a用作在main中嵌入类型的模块
注意:如果您不想通过外部git repo进入,则将在package.json / dependencies中使用文件URL
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句