从外部节点模块导入打字稿

大卫

我想将应用程序划分为不同的节点模块,并拥有一个主模块,该主模块也可以构建所有其他模块,并且我想将Typescript与es6模块一起使用。

这是我计划的项目结构:

  • 主要的
    • node_modules
      • 深度
      • 深度b
  • 框架
    • 介面
      • IComponent.ts
  • 深度
    • 成分
      • 测试
    • node_modules
      • 框架
    • 索引
  • 深度b
    • node_modules
      • 框架

我希望能够在可以在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
布鲁诺·格里德(Bruno Grieder)

TL; DR使用declaration: truein生成模块的声明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文件,其中包含一个maintypings条目,分别指向生成的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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章