如何正确配置 tsconfig.json,以便在使用 npm run build 时只需要必要的文件?

发布于 2025-01-09 18:06:11 字数 1213 浏览 3 评论 0原文

概述

我有一个辅助函数库,我想将其导入到我的项目中。

问题

如何正确配置 tsconfig.json,以便在使用 npm run build 时只需要必要的文件?我不希望包含任何额外的文件。

tsconfig.json

{
  "compilerOptions": {
    "target": "ESNext",
    "module": "CommonJS",
    "declaration": true,
    "checkJs": true,
    "sourceMap": true,
    "outDir": "./dist",
    "removeComments": true,
    "strict": true,
    "noImplicitAny": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "skipLibCheck": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
  },
  "include": ["src/**/*"],
  "compileOnSave": false,
  "buildOnSave": false
}

包含在 package.json

  "main": "./dist/index.js",
  "types": "./dist/index.d.ts",

导入项目时 node_modules 中显示的内容

  1. 应该显示 src 吗?
  2. 如何删除 jest.config.js

输入图片此处描述

Overview

I have a helper function library that I want to import into my projects.

Question

How can I correctly configure my tsconfig.json so only the files necessary are required when using npm run build? I don't want any extra files in to be included.

tsconfig.json

{
  "compilerOptions": {
    "target": "ESNext",
    "module": "CommonJS",
    "declaration": true,
    "checkJs": true,
    "sourceMap": true,
    "outDir": "./dist",
    "removeComments": true,
    "strict": true,
    "noImplicitAny": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "skipLibCheck": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
  },
  "include": ["src/**/*"],
  "compileOnSave": false,
  "buildOnSave": false
}

Included in package.json

  "main": "./dist/index.js",
  "types": "./dist/index.d.ts",

What's showing in node_modules when imported into a project

  1. Should src be showing??
  2. How can I remove jest.config.js?

enter image description here

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流年已逝 2025-01-16 18:06:12

tsconfig.json 文件与最终 npm 包中的内容没有任何关系。默认情况下(或多或少)项目目录中包含的所有内容也将添加到包中(node_modules 文件夹除外)。

有两种方法可以控制包的内容

  1. 创建一个名为 .npmignore 的文件并添加您想要从包中排除的每个文件/文件夹。这与 .gitignore 文件类似。请参阅文档 了解详情。

  2. 您可以将 files 数组添加到 package.json 中,并添加您想要包含在包中的所有文件/文件夹。有关详细信息,请参阅文档

包中需要包含哪些文件取决于包提供的功能。最终包可能不需要测试配置,源代码也不需要......

The tsconfig.json file doesn't have anything to do with what ends up in the final npm package. Per default (more or less) everything that is contained in your project directory will also be added into your package (except the node_modules folder).

There are two ways to control the contents of your package

  1. Create a file named .npmignore and add every file/folder you want to be excluded from your package. This works similar to the .gitignore file. See the docs for details.

  2. You can add a files array to your package.json and add all files/folders you want to be included in your package. See docs for details.

What files need to be in your package, depends on the functionality your package provides. The test configuration probably isn't required for the final package, neither is the source ...

灰色世界里的红玫瑰 2025-01-16 18:06:12

如果您想从 NPM 包中排除某些文件,请在 .npmignore 文件中指定这些文件的列表。这样,当您发布新版本的包时,排除的文件将从您的node_modules中消失。

另外,如果您使用yarn,则可以在.yarnclean 文件中指定安装包后要删除的文件列表。但这样文件将从node_modules中的所有包中删除。

如果您尚未发布包并将其与 npm linkyarn link 一起使用,则无法从 node_modules 中删除文件。

事实上,您可以指定包中除 distpackage.jsonREAMDE.md 之外的所有文件

If you want to exclude some files from your NPM package specify list of them in .npmignore file. This way when you publish a new version of the package excluded file will disappear from you node_modules.

Also, if you use yarn, you can specify the list of files you want to remove after installing packages in the .yarnclean file. But this way files will be deleted from all packages in node_modules.

If you haven't publish your package and use it with npm link or yarn link, you can't remove file from node_modules.

In fact, you can specify all the files in the package except dist, package.json and REAMDE.md

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文