我有一个用TypeScript编写的开源项目,然后将其编译为JavaScript,然后通过npm publish
作为CLI工具发布。
我已经无法来排除 npm包中的TypeScript源代码,并且似乎随机包含了其他*.ts
文件(但我们将重点放在一个文件上)此处)。
我没有包含TypeScript源文件的问题,但这只是向程序包中添加了多余的字节,以供用户使用。
我可以通过很小的设置重现问题
package.json
{
"name": "example",
"version": "1.0.0",
"main": "readme.js"
}
我尝试了一个非常激进的忽略列表:
。npmignore
**/*
*.ts
**
*
*.*
readme.ts
创建一些源代码文件
touch readme.ts
touch readme.js
现在,如果我尝试生成一个npm软件包,它将包含readme.ts
文件。
$ npm pack
npm notice
npm notice package: [email protected]
npm notice === Tarball Contents ===
npm notice 0 readme.js
npm notice 76B package.json
npm notice 0 readme.ts
npm notice === Tarball Details ===
npm notice name: example
npm notice version: 1.0.0
npm notice filename: example-1.0.0.tgz
npm notice package size: 195 B
npm notice unpacked size: 76 B
npm notice shasum: a1ee7cc6b7b0d1e2eccf870e95d7df38c5dcb609
npm notice integrity: sha512-ppNEfKEvT9DEP[...]+d16IWbrj7OdA==
npm notice total files: 3
npm notice
example-1.0.0.tgz
所以为什么包含readme.ts
?
我排除了所有文件,并且在readme.js
文件中仅引用了package.json
。
$ npm --version
6.13.1
简短回答:名为readme.*
的文件(例如readme.foo
,readme.quux
等)将被npm包含,无论您如何尝试在.npmignore文件中取反。
在npm针对npm-cli的题为“某些文件无条件”]的测试中,从第 通过观察此测试,我们可以得出结论,即使#511
行开始,它们包括对名为#511
的文件的测试。readme.randomext
文件中已指定,任何名为readme.randomext
的文件都将包含在tarball(readme.*
)文件中。
下面是上述NPM测试文件.tgz
的摘录
.npmignore
解决方法:
您可能会考虑一些事情,例如:
最简单的方法是将文件重命名为pack-files-and-ignores.js
,然后在pack-files-and-ignores.js
文件中指定test('certain files included unconditionally', function (t) {
var fixture = new Tacks(
Dir({
'package.json': File({
name: 'npm-test-files',
version: '1.2.5'
}),
'.npmignore': File(
'package.json',
'README',
'Readme',
'readme.md',
'readme.randomext', // <----
'changelog',
'CHAngelog',
'ChangeLOG.txt',
'history',
'HistorY',
'HistorY.md',
'license',
'licence',
'LICENSE',
'LICENCE'
),
'README': File(''),
'Readme': File(''),
'readme.md': File(''),
'readme.randomext': File(''), // <----
'changelog': File(''),
'CHAngelog': File(''),
'ChangeLOG.txt': File(''),
'history': File(''),
'HistorY': File(''),
'HistorY.md': File(''),
'license': File(''),
'licence': File(''),
'LICENSE': File(''),
'LICENCE': File('')
})
)
withFixture(t, fixture, function (done) {
t.ok(fileExists('package.json'), 'package.json included')
t.ok(fileExists('README'), 'README included')
t.ok(fileExists('Readme'), 'Readme included')
t.ok(fileExists('readme.md'), 'readme.md included')
t.ok(fileExists('readme.randomext'), 'readme.randomext included') // <----
t.ok(fileExists('changelog'), 'changelog included')
t.ok(fileExists('CHAngelog'), 'CHAngelog included')
t.ok(fileExists('ChangeLOG.txt'), 'ChangeLOG.txt included')
t.ok(fileExists('license'), 'license included')
t.ok(fileExists('licence'), 'licence included')
t.ok(fileExists('LICENSE'), 'LICENSE included')
t.ok(fileExists('LICENCE'), 'LICENCE included')
done()
})
})
。但是,由于当前在read-me.ts
中指定了read-me.ts
,因此实际上实际上也不必额外指定.npmignore
。
或者,如果不可能的话,请考虑:
向[[package.json
*.ts
脚本。例如:.npmignore
read-me.ts
中使用postpack
包进行以下操作:postpack
)。"scripts": {
"postpack": "node ./fix-it.js",
...
},
。fix-it.js
)。.tgz
脚本随后将自动生成新的readme.ts
文件,而没有不需要的文件。
运行.tgz