如何使用深度导出从自定义库中导入对象

huangapple go评论77阅读模式
英文:

How to import objects from custom library with deep export

问题

我已更新我的库结构(添加了一些目录),现在无法从中导入类型。

这是库的结构:

|-dist(已构建的TypeScript)
|-src
|--business
|---entities
|----user.ts
|----site.ts
|---usecases
|----user.usecase.ts
|----site.usecase.ts
|---entities.ts
|---usecases.ts
|--infrastructure
|---...
|--index.ts
|-package.json

user.tssite.ts 包含类和接口(usecases 部分也一样)。

entities.ts 导出了这些类型(usecases 部分也一样):

export { Site, SiteInterface } from "src/business/entities/site.js";
export { User, UserInterface } from "src/business/entities/user.js";

index.ts 从子目录导出所有内容:

export * from "src/business/entities.js";
export * from "src/business/usecases.js";

user.ts 中,我可以这样导入 Siteimport { Site } from "src/index.js";

但是当我在另一个应用程序中安装我的库时,我会得到上面的错误消息:“模块 '' 没有导出的成员 ''。”

我是这样导入的:import { Site, User } from '@xxx/<my-lib>';

以下是库的配置文件:

package.json

{
  "name": "@xxx/<my-lib>",
  "version": "0.0.0-alpha.1",
  "type": "module",
  "main": "src/index.js",
  "scripts": {
    "clean": "rm -rf dist",
    "build": "npm run clean && tsc --build",
    "test": "jest",
    "circular-deps": "madge --ts-config ./tsconfig.json --extensions ts --circular ./src"
  },
  "publishConfig": {
    "registry": "https://gitlab.com/api/v4/projects/<id>/packages/npm/"
  },
  "devDependencies": {
    "@babel/cli": "^7.22.6",
    "@babel/core": "^7.22.8",
    "@babel/preset-env": "^7.22.7",
    "@babel/preset-typescript": "^7.22.5",
    "@types/node": "^20.2.5",
    "babel-jest": "^29.6.1",
    "babel-plugin-module-resolver": "^5.0.0",
    "jest": "^29.6.1",
    "ts-jest": "^29.1.1",
    "ts-jest-resolver": "^2.0.1",
    "ts-node": "^10.9.1",
    "typescript": "^5.0.4"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "composite": true,
    "target": "esnext",
    "module": "esnext",
    "moduleResolution": "nodenext",
    "baseUrl": ".",
    "paths": {
      "@xxx/<my-lib>": ["dist/src/"],
      "@xxx/<my-lib>/*": ["dist/src/*"]
    },
    "resolveJsonModule": true,
    "declaration": true,
    "declarationMap": true,
    "outDir": "dist",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  },
  "include": ["src/**/*", "package.json"],
  "exclude": ["node_modules/**/*"]
}

有人能帮助我理解我做错了什么吗?

英文:

I have update my library structure (adding some directories), and now I can't import types from it.

Here is the structure of the library :

|-dist (builded typescript)
|-src
|--business
|---entities
|----user.ts
|----site.ts
|---usecases
|----user.usecase.ts
|----site.usecase.ts
|---entities.ts
|---usecases.ts
|--infrastructure
|---...
|--index.ts
|-package.json

user.ts and site.ts contains class and interface (same for usecases part).

entities.ts exports these types (same for usecases part) :

export { Site, SiteInterface } from &quot;src/business/entities/site.js&quot;;
export { User, UserInterface } from &quot;src/business/entities/user.js&quot;;

index.ts exports all from subdirectories :

export * from &quot;src/business/entities.js&quot;;
export * from &quot;src/business/usecases.js&quot;;

In user.ts, I can import Site like this : import { Site } from &quot;src/index.js&quot;;

But when I install my library in another application, I get the above error Module &#39;&quot;&lt;my-lib&gt;&quot;&#39; has no exported member &#39;&lt;my-type&gt;&#39;.

I import it like this : import { Site, User } from &#39;@xxx/&lt;my-lib&gt;&#39;;

Here are library's config files :

package.json

{
  &quot;name&quot;: &quot;@xxx/&lt;my-lib&gt;&quot;,
  &quot;version&quot;: &quot;0.0.0-alpha.1&quot;,
  &quot;type&quot;: &quot;module&quot;,
  &quot;main&quot;: &quot;src/index.js&quot;,
  &quot;scripts&quot;: {
    &quot;clean&quot;: &quot;rm -rf dist&quot;,
    &quot;build&quot;: &quot;npm run clean &amp;&amp; tsc --build&quot;,
    &quot;test&quot;: &quot;jest&quot;,
    &quot;circular-deps&quot;: &quot;madge --ts-config ./tsconfig.json --extensions ts --circular ./src&quot;
  },
  &quot;publishConfig&quot;: {
    &quot;registry&quot;: &quot;https://gitlab.com/api/v4/projects/&lt;id&gt;/packages/npm/&quot;
  },
  &quot;devDependencies&quot;: {
    &quot;@babel/cli&quot;: &quot;^7.22.6&quot;,
    &quot;@babel/core&quot;: &quot;^7.22.8&quot;,
    &quot;@babel/preset-env&quot;: &quot;^7.22.7&quot;,
    &quot;@babel/preset-typescript&quot;: &quot;^7.22.5&quot;,
    &quot;@types/node&quot;: &quot;^20.2.5&quot;,
    &quot;babel-jest&quot;: &quot;^29.6.1&quot;,
    &quot;babel-plugin-module-resolver&quot;: &quot;^5.0.0&quot;,
    &quot;jest&quot;: &quot;^29.6.1&quot;,
    &quot;ts-jest&quot;: &quot;^29.1.1&quot;,
    &quot;ts-jest-resolver&quot;: &quot;^2.0.1&quot;,
    &quot;ts-node&quot;: &quot;^10.9.1&quot;,
    &quot;typescript&quot;: &quot;^5.0.4&quot;
  }
}

tsconfig.json

{
  &quot;compilerOptions&quot;: {
    &quot;composite&quot;: true,                                   /* Enable constraints that allow a TypeScript project to be used with project references. */
    &quot;target&quot;: &quot;esnext&quot;,                                  /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
    &quot;module&quot;: &quot;esnext&quot;,                                /* Specify what module code is generated. */
    &quot;moduleResolution&quot;: &quot;nodenext&quot;,                     /* Specify how TypeScript looks up a file from a given module specifier. */
    &quot;baseUrl&quot;: &quot;.&quot;,                                     /* Specify the base directory to resolve non-relative module names. */
    &quot;paths&quot;: {                                           /* Specify a set of entries that re-map imports to additional lookup locations. */
      &quot;@xxx/&lt;my-lib&gt;&quot;: [&quot;dist/src/&quot;],
      &quot;@xxx/&lt;my-lib&gt;/*&quot;: [&quot;dist/src/*&quot;],
    },
    &quot;resolveJsonModule&quot;: true,                        /* Enable importing .json files. */
    &quot;declaration&quot;: true,                              /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
    &quot;declarationMap&quot;: true,                           /* Create sourcemaps for d.ts files. */
    &quot;outDir&quot;: &quot;dist&quot;,                                   /* Specify an output folder for all emitted files. */
    &quot;esModuleInterop&quot;: true,                             /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables &#39;allowSyntheticDefaultImports&#39; for type compatibility. */
    &quot;forceConsistentCasingInFileNames&quot;: true,            /* Ensure that casing is correct in imports. */
    &quot;strict&quot;: true,                                      /* Enable all strict type-checking options. */
    &quot;skipLibCheck&quot;: true                                 /* Skip type checking all .d.ts files. */
  },
  &quot;include&quot;: [&quot;src/**/*&quot;, &quot;package.json&quot;],
  &quot;exclude&quot;: [&quot;node_modules/**/*&quot;]
}

Does someone can help me to understand what I do wrong please ?

答案1

得分: 0

Gitlab不在各个阶段之间共享工作空间,因此它发布了空包。我已经纠正了这个问题,现在它可以正常工作。

我也根据我的问题中的评论更新了我的包。

感谢您的帮助,@rveerd 和 @aluan-haddad 如何使用深度导出从自定义库中导入对象

英文:

Ok I have found where was the problem :

Gitlab does not share workspace between stages, so it publish empty package. I have corrected this, and now it works.

I update my package following comments on my question too.

Thank you for your help @rveerd and @aluan-haddad 如何使用深度导出从自定义库中导入对象

huangapple
  • 本文由 发表于 2023年7月12日 22:44:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76671815.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定