如何正确配置 tsconfig.json,以便在使用 npm run build 时只需要必要的文件?
概述
我有一个辅助函数库,我想将其导入到我的项目中。
问题
如何正确配置 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
中显示的内容
- 应该显示
src
吗? - 如何删除
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
- Should
src
be showing?? - How can I remove
jest.config.js
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
tsconfig.json
文件与最终 npm 包中的内容没有任何关系。默认情况下(或多或少)项目目录中包含的所有内容也将添加到包中(node_modules
文件夹除外)。有两种方法可以控制包的内容
创建一个名为
.npmignore
的文件并添加您想要从包中排除的每个文件/文件夹。这与 .gitignore 文件类似。请参阅文档 了解详情。您可以将
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 thenode_modules
folder).There are two ways to control the contents of your package
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.You can add a
files
array to yourpackage.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 ...
如果您想从 NPM 包中排除某些文件,请在 .npmignore 文件中指定这些文件的列表。这样,当您发布新版本的包时,排除的文件将从您的node_modules中消失。
另外,如果您使用yarn,则可以在
.yarnclean
文件中指定安装包后要删除的文件列表。但这样文件将从node_modules中的所有包中删除。如果您尚未发布包并将其与
npm link
或yarn link
一起使用,则无法从 node_modules 中删除文件。事实上,您可以指定包中除
dist
、package.json
和REAMDE.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
oryarn link
, you can't remove file from node_modules.In fact, you can specify all the files in the package except
dist
,package.json
andREAMDE.md