【问题标题】:Npm workspaces: Different "main" property for development and publishing?Npm 工作区:用于开发和发布的不同“主要”属性?
【发布时间】:2021-08-03 21:05:09
【问题描述】:

我创建了一个 npm 工作区,其中包含两个包“foo”和“bar”。
此外,我使用的是打字稿,所以两个包都包含src/index.ts。

在开发过程中,我希望两个包的“主要”属性为src/index.ts。
但是在构建和发布包之后,“main”条目必须指向构建的dist/index.js。

我怎样才能做到这一点,或者我的假设是错误的,我可以在开发过程中指向打字稿文件?
但是我必须在开发过程中一直重建包。
我希望这不是必需的。

提前感谢您的帮助

更新
更准确地说,我需要让“main”指向 Typescript 文件 src/index.ts,否则,我无法在“foo”本身中引用“foo”:

// foo/src/index.ts
import { something } from 'foo';

抛出此错误:
[vite:resolve] Failed to resolve entry for package "foo". The package may have incorrect main/module/exports specified in its package.json.

上述导入仅适用,如果“main”指向src/index.ts。 否则我将不得不使用同一个项目的导入文件的相对路径,例如

// foo/src/index.ts
import { something } from './someotherfile.ts';

【问题讨论】:

标签: npm package.json workspace monorepo vite


【解决方案1】:

我发现,那个 vite 需要插件 vite-tsconfig-paths 来找到我的主要 tsconfig.json 来正确解析项目:

// vite.config.ts
import tsconfigPaths from "vite-tsconfig-paths";

plugins: [
  tsconfigPaths({
    root: "../../",
  }),
],

【讨论】:

    猜你喜欢
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多