美文网首页Web 前端开发 我爱编程让前端飞
yarn.lock是否要放入代码仓库中

yarn.lock是否要放入代码仓库中

作者: buyu007 | 来源:发表于2018-03-21 22:54 被阅读0次

中午闲逛github, 看到某个项目的依赖不是最新的,就随手提了一个pull request (当然100%确保是兼容的). 不一会,收到反馈, 要求同步更新yarn.lock文件.
突然想到一个问题, yarn.lock是否要放到仓库中呢? 印象中这种文件, 需要的时候重新生成一份就行了, 感觉没必要放进仓库.

google了一下, 找到一篇, 直接翻译如下
Always commit your yarn.lock file into the source code repo


确保你的yarn.lock文件在代码仓库中

我用Yarn package manage来管理我所有的Angular项目. 因为它确实比npm要快. Yarn在运行时会创建一个yarn.lock文件用来保存所安装的依赖包的确切版本(ps, 代码中就怕这种不确定性, 这个锅npm得背). 去年, 我写了一篇博客, 里面描述了一个特定的场景, 造成我的一个Angular项目运行失败

I use the Yarn package manager for all my Angular projects. Besides being faster than npm, yarn creates a file yarn.lock that stores the exact versions of installed dependencies. Last year, I wrote about a specific use case that caused a breaking change during one of my Angular workshops.

昨天, 我不得不用Angular CLI创建一个新的项目. 项目创建成功, 启动也成功了. 然后, 我准备用ng build -prod 构建一个产线build. 然而失败了, 错误信息Cannot find module ‘uglifyjs-webpack-plugin’. 我知道, Webpack用UglifyJS来优化包的大小, 这很棒; 但是我的产线build构建失败了, 这很糟糕.

Yesterday, I had to create a new project using Angular CLI. The project was successfully generated and started with ng serve. Then, I wanted to create a production build with ng build –prod, but the build failed with the error Cannot find module ‘uglifyjs-webpack-plugin’. I know that Webpack uses UglifyJS for optimizing sizes of the bundles, which is great, but my production build fails, which is not great at all.

自动生成的package.json文件在我项目的根目录, 里面没有直接引用uglifyjs-webpack-plugin. 它静悄悄的呆在node_modules中, 是Angular CLI项目成千上万个引用中的一个.

The generated package.json file located in the root of my project has no direct dependency on uglifyjs-webpack-plugin, but there is one deep inside node_modules in one of the thousand (literally) dependencies used in Angular CLI projects.

长话短说, Webpack团队在npmjs.org中发布了这个插件的1.1.7版, 但是, 里面确实空的(ps, 算是发布事故吧). Angular团队跟进了这个版本. 一小时后, 发布了1.1.8版本修复了这个bug

To make the long story short, the WebPack team pushed to npmjs.org the new version 1.1.7 this plugin, which was empty. The Angular team got in touch with them, and an hour later, the fixed version 1.1.8 of the Webpack UglifyJS plugin was pushed to the npm central repo.

我项目中的yarn.lock文件锁定的是错误版本的插件. 我删除了它, 重新yarn. 这次 , 使用了1.1.8版本, *ng build –prod 成功了

My project already had the yarn.lock file with an erroneous version of this plugin. I deleted it and ran yarn install again. This time it picked up the fixed 1.1.8 version and the ng build –prod command started working again. Thank you, the Webpack team for the quick turnaround! Happy end!

这个问题的根本问题是在错误的时间创建生成了项目. 但想象一下, 在团队在同一个项目协作的过程中, 他们的代码仓库中, 没有包含引用1.1.6版本yarn.lock文件. 当QA决定构建工程时, 一分钟后, 你收到一封"Who broke the build?" 的邮件

This was a specific issue with the newly generated project created at the “wrong time”. But imagine a team working on a project for some time, and their github repo didn’t include the yarn.lock file that stored the reference to the working version 1.1.6 of that plugin, and Mary (the QA engineer) decided to make a production build. A minute later, the entire team got an email titled “Who broke the build?”

底线: 将可构建的yarn.lock文件push到代码仓库中, 来确保整个团队都有一个可返回构建运行的版本. 这样, 你的项目就不会因为npmjs.org中某个错误的提交导致构建失败

The bottom line: Push the yarn.lock of the project that works and builds correctly into your version control system to ensure that the entire team has a reproducible and working build. This way, your project won’t depend on someone from across the globe submitting erroneous packages to the npmjs.org.

P.S. npm5同样生成了一个package-lock.json文件来注册所有的安装的依赖. 但它不能保证每次npm install 都会安装里面的指定的包的版本

P.S. While npm 5 also creates the file package-lock.json with the registry of all installed dependencies, it doesn’t guarantee that each npm install will install exact versions of the packages listed there.

相关文章

  • yarn.lock是否要放入代码仓库中

    中午闲逛github, 看到某个项目的依赖不是最新的,就随手提了一个pull request (当然100%确保是...

  • 项目放入仓库 删除仓库

    查看项目状态(是否放入仓库) 添加要放入仓库的文件 添加到仓库 不要放到仓库的文件 删除仓库里的文件 更改名称

  • git基础命令

    理解: git的四个文件区: 代码区(工作区) 缓存区 本地仓库 远程仓库 常用命令: 将数据从从代码区 放入 缓...

  • IDEA的GIT代码管理

    1.将代码放入GIT仓库 1.1 编写.gitignore文件 将不需要加入git管理的文件信息放入.gitign...

  • 上传代码到CocoaPods库时遇到的问题总结

    上传cocoapods引用第三方时遇到的错误 本地检测代码仓库是否有问题 远程检测代码仓库是否有问题 向远程代码索...

  • pod 组件

    含第三方库的组件 本地检测代码仓库是否有问题 远程检测代码仓库是否有问题 向远程代码索引库提交spec 不含第三方...

  • Maven 版本和快照的区别 RELEASE/SNAPSHOT

    版本RELEASE Maven要求你每次更改了代码都要发布到仓库,然后其他人去仓库下载你的代码。 为了区分代码是否...

  • 搭建远程私有库

    步骤: 1.创建本地仓库,放入想要公开的代码; 2.修改.podspec文件中的信息 3.创建远程私有库,托管我们...

  • github- 常用的git的命令记录

    commit ——提交至本地代码仓库,不会提交到远程仓库 push——将本地代码仓库的变动同步到远程仓库中

  • 《Rust权威指南》书中样例代码

    最近在看《Rust权威指南》。 手动编写书中样例代码,现收集放入如下仓库:地址: https://gitee.co...

网友评论

    本文标题:yarn.lock是否要放入代码仓库中

    本文链接:https://www.haomeiwen.com/subject/pcynqftx.html