composer的archive命令默认打包全部源码文件;真正生效的排除方式只有两种:在composer.json中配置archive.exclude(作用于composer archive和packagist构建),或用.gitattributes的export-ignore(作用于所有基于git archive的分发,包括github zip下载)。

Composer 的 archive 命令默认打包全部源码文件,包括 tests/、docs/、.github/、phpunit.xml 等非运行必需内容;真正生效的排除方式只有两种:在 composer.json 中配置 archive.exclude(作用于 composer archive 和 Packagist 构建),或用 .gitattributes 的 export-ignore(作用于所有基于 git archive 的分发,包括 GitHub ZIP 下载)。
archive.exclude 只对 composer archive 和 Packagist 有效
这个配置不会影响 composer install、composer update 或自动加载行为,只控制最终生成的 zip/tar 包内容。它必须写在 composer.json 根级(与 require 同级),不是 autoload 下:
{
"name": "my/package",
"archive": {
"exclude": ["/tests", "/Tests", "/docs", "/phpunit.xml", "/.editorconfig"]
}
}
- 路径以
/开头表示精确匹配目录,避免误删src/MyTests - 不支持通配符(如
/tests/**或*.md),只能写具体路径或前缀模式 - 修改后需打新 Git tag,Packagist 才会重新构建 dist 包
- GitHub 点 “Download ZIP” 不认这个配置——它走的是
git archive,只看.gitattributes
.gitattributes 的 export-ignore 是实际最可靠的排除方式
几乎所有主流平台(GitHub、GitLab、Packagist、Satis)在构建 dist 包时都调用 git archive,而它原生支持 .gitattributes 中的 export-ignore 指令。在项目根目录创建该文件即可:
围绕关键发现、作用机制、临床相关性及研究局限性展开讨论。适用于撰写或优化任何生物医学论文的“讨论(Discussion)”部分——包括结果解读、与既往文献关联、阐释意外发现、界定研究局限性,以及撰写结论。当用户输入以下任一指令时也会自动触发该功能: - “write my discussion” - “help me discuss my findings” - “how do I compare to prior studies” - “write the limitations par
/tests export-ignore /Tests export-ignore /test export-ignore /phpunit.xml export-ignore /phpunit.xml.dist export-ignore /README.md export-ignore
- 路径开头必须加
/,否则tests会误匹配mytests.php - 不需要额外命令或重建,只要 commit 进仓库,下次打包就生效
- 对
composer install --no-dev无影响——它只管“别人下载你包时看到什么” - 如果你用
source安装("preferred-install": "source"),这个配置也无效,因为直接 clone 整个 repo
archive.excludes(旧写法)已弃用,别再用
早期文档里出现的 archive.excludes(数组形式,顶层字段)是 Composer 1.x 遗留写法,从 Composer 2.0 起已被废弃。现在唯一受支持的是 archive.exclude(对象内字段)。写错会导致配置完全被忽略:
- ❌ 错误:
"archive.excludes": ["tests/*"]—— 字段名错,Composer 直接跳过 - ✅ 正确:
"archive": {"exclude": ["/tests"]} - ⚠️ 注意:
archive.exclude不支持*或**,想排除tests/unit/和tests/integration/,必须分别写"/tests/unit"和"/tests/integration"
最容易被忽略的一点:archive.exclude 和 .gitattributes 是互补而非互斥的关系。如果你只配了前者,GitHub 用户仍能下载到完整源码;如果只配了后者,本地执行 composer archive --format=zip 时又可能把测试文件打进去。真正稳妥的做法是两者都配,并确保路径严格一致。










