NextJS:构建组件以导出并进入另一个项目

发布于 2025-01-29 14:45:27 字数 1697 浏览 3 评论 0原文

当前设置:我有两个NextJS应用程序:一个我们将称为项目A和一个库存储库,我们将称为LIB Project Lib。项目A是一个相当简单的应用程序,具有几个独立的组件,而Project Lib是许多其他应用程序杠杆的类似图书馆的应用程序。

我要做的事情:包装项目A以构建分配包含其所有组件的方式,然后我可以在项目中利用这些组件。我目前正在使用npm install [email  protected] :< anchoment>/>/< project a> gt; git,并且效果很好。我当前的问题是,使用npm run build build时,只有页面才能构建页面,并且它也不会生成每个组件的相应.d.t.t.ts文件/组件

我发现设置“声明”:true /*生成相应的'.d.ts'文件。 */将为我生成组件,但这似乎并没有解决问题。关于如何做到这一点,我也找不到太多在线。也许我只是一个糟糕的谷歌手,他知道。

无论如何,这是我的tsconfigpackage.json文件看起来像:

package.json

{
...,
"files": [
        "lib/**/*"
    ]
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "ESNext",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "jsx": "preserve",
    "declaration": true,
    "declarationMap": true,
    "sourceMap": true,
    "outDir": "./lib",
    "rootDir": "./",
    "removeComments": true,
    "noEmit": true,
    "isolatedModules": true,
    "strict": true,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "resolveJsonModule": true
  },
  "include": [
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx",
    "components/**/*"
  ],
  "exclude": [
    "node_modules/*",
    "./node_modules",
    "./node_modules/*",
    "./lib"
  ]
}

Current Setup: I have two NextJS apps: one we'll call Project A and a library repo that we'll call Project LIB. Project A is a fairly simple app that has a couple independent components whereas Project LIB is a library-like app that many other apps leverage.

What I am trying to do: Package up Project A in a way where the build distribution contains all of its components that I can then leverage in Project LIB. I am currently building/deploying Project A as a private repo and pulling it into Project LIB using npm install [email protected]:<organization>/<Project A>.git and that is working fine. My current issue is that only the pages are being build when I build Project A using npm run build and it doesn't also generate each component's corresponding .d.ts file under /components.

I figured setting "declaration": true /* Generates corresponding '.d.ts' file. */ would generate the components for me but that didn't seem to do the trick. I'm also not finding much online regarding how to do this. Maybe i'm just a bad googler, who knows.

Anyways, here's what my tsconfig and package.json files looks like:

package.json:

{
...,
"files": [
        "lib/**/*"
    ]
}

tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "module": "ESNext",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "jsx": "preserve",
    "declaration": true,
    "declarationMap": true,
    "sourceMap": true,
    "outDir": "./lib",
    "rootDir": "./",
    "removeComments": true,
    "noEmit": true,
    "isolatedModules": true,
    "strict": true,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "resolveJsonModule": true
  },
  "include": [
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx",
    "components/**/*"
  ],
  "exclude": [
    "node_modules/*",
    "./node_modules",
    "./node_modules/*",
    "./lib"
  ]
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文