简体   繁体   English

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

[英]How can I correctly configure my tsconfig.json so only the files necessary are required when using npm run build?

Overview概述

I have a helper function library that I want to import into my projects.我有一个助手 function 库,我想将其导入到我的项目中。

Question问题

How can I correctly configure my tsconfig.json so only the files necessary are required when using npm run build?如何正确配置我的 tsconfig.json,以便在使用 npm run build 时只需要必要的文件? I don't want any extra files in to be included.我不想包含任何额外的文件。

tsconfig.json 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包含在package.json

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

What's showing in node_modules when imported into a project导入项目时node_modules中显示的内容

  1. Should src be showing?? src应该显示吗?
  2. How can I remove jest.config.js ?我怎样才能删除jest.config.js

在此处输入图像描述

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

There are two ways to control the contents of your package有两种方法可以控制你的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.创建一个名为.npmignore的文件,并添加要从 package 中排除的每个文件/文件夹。这与.gitignore文件的工作方式类似。 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.您可以将files数组添加到您的package.json并添加您想要包含在您的 package 中的所有文件/文件夹。有关详细信息,请参阅文档

What files need to be in your package, depends on the functionality your package provides.您的 package 中需要包含哪些文件,取决于您的 package 提供的功能。 The test configuration probably isn't required for the final package, neither is the source...最终的 package 可能不需要测试配置,源代码也不需要...

If you want to exclude some files from your NPM package specify list of them in .npmignore file.如果你想从你的 NPM package 中排除一些文件,请在.npmignore文件中指定它们的列表。 This way when you publish a new version of the package excluded file will disappear from you node_modules.这样当你发布一个新版本的 package 排除文件将从你的 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.此外,如果您使用 yarn,则可以在.yarnclean文件中指定安装包后要删除的文件列表。 But this way files will be deleted from all packages in node_modules.但是这样文件将从 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.如果您还没有发布 package 并将其与npm linkyarn link一起使用,则无法从 node_modules 中删除文件。

In fact, you can specify all the files in the package except dist , package.json and REAMDE.md实际上,您可以指定 package 中除distpackage.jsonREAMDE.md之外的所有文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM