如何在 package.json 中使用 'files' 和 'directories' 属性?
How do you use the 'files' and 'directories' properties in package.json?
如果 package.json
有一个 files
道具,and/or 一个 directories
道具:
"files": [
"./src/assets/fonts/"
],
"directories": {
"assets:": "./src/assets"
}
有哪些使用方法?文档没有提到指定它们后可以用它们做什么。
例如,files docs 表示:
The "files" field is an array of files to include in your project. If you name a folder in the array, then it will also include the files inside that folder.
"include in your project"是什么意思?包括在哪里?以前不可以访问,现在如何访问?
在 directories section 中,文档说:
In the future, this information may be used in other creative ways.
现有的创意使用方式有哪些?
"include in your project" 表示文件将在您 运行 npm publish
时创建的打包 tarball 中。您还可以 运行 npm pack
生成 tarball 以供检查,而无需实际触发发布。通过这种方式,您实际上可以打开生成的 tarball 并检查哪些文件 were/were 未包含。
虽然 .npmignore
(或 .gitignore
作为代理,如果没有 .npmignore
)作为要忽略的文件的黑名单(因此默认包括所有其他内容),files
数组是一个白名单。也就是说,如果指定了 files
数组,默认情况下将排除所有内容,而不是默认包含所有内容,并且只有那些明确列出的文件才会包含在打包的 tarball 中。
例如,假设您的包是一个供浏览器使用的库。您的代码在 lib/
中,您 运行 浏览器编译成浏览器兼容的库 dist/index.js
。您从 .gitignore
中列出的一堆文件开始,这些文件用作不存在的事实上的 .npmignore
。但是现在 dist/
充满了生成的文件,您希望从 git 存储库中忽略它们。如果将它们添加到 .gitignore
,它们将被排除在 git 存储库之外,但它们也会从包 tarball 中被忽略。因此,您有两个选择:将 .gitignore
复制为 .npmignore
,但仅在 .gitignore
中列出 dist/
。如果这样做,您将不得不使两个文件保持几乎但不完全同步。这种方式繁琐且容易出错。
另一种选择是不使用 .npmignore
,而只是在 files
数组中列出包中您实际需要的文件。 README.*
、package.json
、CHANGELOG.*
(可能还有其他几个)无论如何都会自动包含在 tarball 中。因此,您只需添加 "files": [ "dist" ]
即可。现在你的包 tarball 将不包含来自 lib
的原始源代码 JS,也不包含 tests/
等,而是只包含 dist/
.
中的实际编译库
至于 directories
,我通常列出 lib
(对于 es5),src
(对于 es6、coffeescript、typescript 等源),dist
(对于浏览器或 vm 特定的构建),test
,output
(用于临时生成的文件,如覆盖率报告等),doc
,等等。尽管未使用此 属性直接通过 npm 或其他工具,它使目录结构明确。此外,它使目录在 npm 脚本中可引用,如下所示:
"scripts": {
"clean": "rm -rf $npm_package_directories_dist $npm_package_directories_output",
"lint": "eslint $npm_package_directories_src",
"test": "teenytest $npm_package_directories_test",
}
这样,目录只指定一次,如果它们发生变化,只需在一个位置进行更改(而不是整个 package.json 中的许多)。
如果 package.json
有一个 files
道具,and/or 一个 directories
道具:
"files": [
"./src/assets/fonts/"
],
"directories": {
"assets:": "./src/assets"
}
有哪些使用方法?文档没有提到指定它们后可以用它们做什么。
例如,files docs 表示:
The "files" field is an array of files to include in your project. If you name a folder in the array, then it will also include the files inside that folder.
"include in your project"是什么意思?包括在哪里?以前不可以访问,现在如何访问?
在 directories section 中,文档说:
In the future, this information may be used in other creative ways.
现有的创意使用方式有哪些?
"include in your project" 表示文件将在您 运行 npm publish
时创建的打包 tarball 中。您还可以 运行 npm pack
生成 tarball 以供检查,而无需实际触发发布。通过这种方式,您实际上可以打开生成的 tarball 并检查哪些文件 were/were 未包含。
虽然 .npmignore
(或 .gitignore
作为代理,如果没有 .npmignore
)作为要忽略的文件的黑名单(因此默认包括所有其他内容),files
数组是一个白名单。也就是说,如果指定了 files
数组,默认情况下将排除所有内容,而不是默认包含所有内容,并且只有那些明确列出的文件才会包含在打包的 tarball 中。
例如,假设您的包是一个供浏览器使用的库。您的代码在 lib/
中,您 运行 浏览器编译成浏览器兼容的库 dist/index.js
。您从 .gitignore
中列出的一堆文件开始,这些文件用作不存在的事实上的 .npmignore
。但是现在 dist/
充满了生成的文件,您希望从 git 存储库中忽略它们。如果将它们添加到 .gitignore
,它们将被排除在 git 存储库之外,但它们也会从包 tarball 中被忽略。因此,您有两个选择:将 .gitignore
复制为 .npmignore
,但仅在 .gitignore
中列出 dist/
。如果这样做,您将不得不使两个文件保持几乎但不完全同步。这种方式繁琐且容易出错。
另一种选择是不使用 .npmignore
,而只是在 files
数组中列出包中您实际需要的文件。 README.*
、package.json
、CHANGELOG.*
(可能还有其他几个)无论如何都会自动包含在 tarball 中。因此,您只需添加 "files": [ "dist" ]
即可。现在你的包 tarball 将不包含来自 lib
的原始源代码 JS,也不包含 tests/
等,而是只包含 dist/
.
至于 directories
,我通常列出 lib
(对于 es5),src
(对于 es6、coffeescript、typescript 等源),dist
(对于浏览器或 vm 特定的构建),test
,output
(用于临时生成的文件,如覆盖率报告等),doc
,等等。尽管未使用此 属性直接通过 npm 或其他工具,它使目录结构明确。此外,它使目录在 npm 脚本中可引用,如下所示:
"scripts": {
"clean": "rm -rf $npm_package_directories_dist $npm_package_directories_output",
"lint": "eslint $npm_package_directories_src",
"test": "teenytest $npm_package_directories_test",
}
这样,目录只指定一次,如果它们发生变化,只需在一个位置进行更改(而不是整个 package.json 中的许多)。