[npm不管尝试什么,都不会忽略我的TypeScript源代码文件

问题描述 投票:2回答:1

我有一个用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
typescript npm glob ignore npm-publish
1个回答
0
投票

简短回答:名为readme.*的文件(例如readme.fooreadme.quux等)将被npm包含,无论您如何尝试在.npmignore文件中取反。


NPM的测试文件:

在npm针对npm-cli的题为“某些文件无条件”]的测试中,从第#511行开始,它们包括对名为#511的文件的测试。

通过观察此测试,我们可以得出结论,即使readme.randomext文件中已指定,任何名为readme.randomext的文件都将包含在tarball(readme.*)文件中。

下面是上述NPM测试文件.tgz的摘录

.npmignore

解决方法:

您可能会考虑一些事情,例如:

  1. 最简单的方法是将文件重命名为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

  2. 或者,如果不可能的话,请考虑:

    • 向[[package.json

    • 的脚本部分添加*.ts脚本。例如:.npmignore
  3. 然后在read-me.ts中使用postpack包进行以下操作:

      解开压缩包(postpack)。
  4. [删除不需要的文件,即"scripts": { "postpack": "node ./fix-it.js", ... },
  5. 重新打包以创建新的tarball(fix-it.js)。
  6. 发布软件包的步骤将变为:

  1. 运行node-tar
  2. .tgz脚本随后将自动生成新的readme.ts文件,而没有不需要的文件。

    运行.tgz

  • © www.soinside.com 2019 - 2024. All rights reserved.