Removing jQuery from GitHub.com frontend
Removing jQuery from GitHub.com frontend
Web standards in the later years
Over the years, GitHub grew into a company with hundreds of engineers and a dedicated team gradually formed to take responsibility for the size and quality of JavaScript code that we serve to web browsers. One of the things that we’re constantly on the lookout for is technical debt, and sometimes technical debt grows around dependenices that once provided value, but whose value dropped over time.
When it came to jQuery, we compared it against the rapid evolution of supported web standard in modern browsers and realized:
- The
$(selector)pattern can easily be replaced withquerySelectorAll(); - CSS classname switching can now be achieved using Element.classList;
- CSS now supports defining visual animations in stylesheets rather than in JavaScript;
$.ajaxrequests can be performed using the Fetch Standard;- The
addEventListener()interface is stable enough for cross-platform use; - We could easily encapsulate the event delegation pattern with a lightweight library;
- Some syntactic sugar that jQuery provides has become reduntant with the evolution of JavaScript language.
Furthermore, the chaining syntax didn’t satisfy how we wanted to write code going forward. For example:
$('.js-widget')
.addClass('is-loading')
.show()
This syntax is simple to write, but to our standards, doesn’t communicate intent really well. Did the author expect one or more js-widget elements on this page? Also, if we update our page markup and accidentally leave out the js-widget classname, will an exception in the browser inform us that something went wrong? By default, jQuery silently skips the whole expresion when nothing matched the initial selector; but to us, such behavior was a bug rather than a feature.
Finally, we wanted to start annotating types with Flow to perform static type checking at build time, and we concluded推断 that the chaining syntax doesn’t lend itself well to static analysis, since almost every result of a jQuery method call is of the same type. We chose Flow over alternatives because, at the time, features such as @flow weak mode allowed us to progressively and efficiently start applying types to a codebase which was largely untyped.
All in all, decoupling from jQuery would mean that we could rely on web standards more, have MDN web docs be de-facto实际上的 default documentation for our frontend developers, maintain more resilient code in the future, and eventually drop a 30 kB dependency from our packaged bundles, speeding up page load times and JavaScript execution times.
Incremental decoupling
Even with an end goal in sight, we knew that it wouldn’t be feasible to just allocate all resources we had to rewriting everything from jQuery to vanilla JS. If anything, such a rushed endeavor would likely lead to many regressions in site functionality that we would later have to weed out. Instead, we:
- Set up metrics that tracked ratio of jQuery calls used per overall line of code and monitored that graph over time to make sure that it’s either staying constant or going down, not up.

- We discouraged importing jQuery in any new code. To facilitate that using automation, we created eslint-plugin-jquery which would fail CI checks if anyone tried to use jQuery features, for example
$.ajax. - There were now plenty of violations of eslint rules in old code, all of which we’ve annotated with specific
eslint-disablerules in code comments. To the reader of that code, those comments would serve as a clear signal that this code doesn’t represent our current coding practices. - We created a pull request bot that would leave a review comment on a pull request pinging our team whenever somebody tried to add a new
eslint-disablerule. This way we would get involved in code review early and suggest alternatives. - A lot of old code had explicit coupling to external interfaces of pjax and facebox jQuery plugins, so we’ve kept their interfaces relatively the same while we’ve internally replaced their implementation with vanilla JS. Having static type checking helped us have greater confidence around those refactorings.
- Plenty of old code interfaced with rails-behaviors, our adapter for the Ruby on Rails approach to “unobtrusive” JS, in a way that they would attach an AJAX lifecycle handler to certain forms:
Polyfills
These are the polyfills that helped us transition to using standard browser features. We try to serve most of these polyfills only when absolutely necessary, i.e. to outdated browsers as part of a separate “compatibility” JavaScript bundle.
- github/eventlistener-polyfill
- github/fetch
- github/form-data-entries
- iamdustan/smoothscroll
- javan/details-element-polyfill
- jonathantneal/closest
- kumarharsh/custom-event-polyfill
- marvinhagemeister/request-idle-polyfill
- mathiasbynens/Array.from
- mathiasbynens/String.prototype.codePointAt
- mathiasbynens/String.prototype.endsWith
- mathiasbynens/String.prototype.startsWith
- medikoo/es6-symbol
- nicjansma/usertiming.js
- rubennorte/es6-object-assign
- stefanpenner/es6-promise
- webcomponents/template
- webcomponents/URL
- webcomponents/webcomponentsjs
- WebReflection/url-search-params
- yola/classlist-polyfill
Removing jQuery from GitHub.com frontend的更多相关文章
- GitHub:我们是这样弃用jQuery的
摘要: 技术债清理流程指南. 原文:Removing jQuery from GitHub.com frontend 译文:GitHub:我们为什么会弃用jQuery? 作者:GitHub 前端工程团 ...
- 【原创】我们还需要学jQuery吗?
引言 最近撸Vue的项目,感觉的有点心累.恰巧近日,有读者来信,就是想咨询一下 烟哥,现在还有必要学习jQuery么? 我明白,现在MVVM框架逐渐占据了主要市场,很多老项目也逐渐的从jQuery转向 ...
- 2019 年了,为什么我还在用 jQuery?
译者按: 看来 jQuery 还是有一些用武之地的. 原文: Why I'm Still Using jQuery in 2019 译者: Fundebug 为了保证可读性,本文采用意译而非直译.翻译 ...
- 阅读jquery源码与js依赖加载的模块化!
阅读源码肯定是先下载有注释的源码 我也是醉了,10309 行代码,在陆续续的一个月之内,看完了,虽有收获但收获不大, 直到又一次看jquery的github,怎么会有cmd????没听过使用jquer ...
- jQuery tmpl用法总结
之前很是头疼循环数据的渲染,搞一大堆的命名,一点点的赋值,很是麻烦,今天学习了一下jQuery插件tmpl,下面抛出一些使用方法,供以后参考: 官方网址:http://web.archive.org/ ...
- seajs初尝 加载jquery返回null解决学习日志含示例下载
原文地址:http://www.tuicool.com/articles/bmuaEb 如需demo示例,请点击下方链接下载: http://yunpan.cn/cVEybKs8nV7CF 提取码 ...
- Downloading jQuery 3.2.1
Downloading jQuery Compressed and uncompressed copies of jQuery files are available. The uncompresse ...
- 01-老马jQuery教程-jQuery入口函数及选择器
前言 这套jQuery教程是老马专门为寒门子弟而录制,希望大家看到后能转发给更多的寒门子弟.视频都是免费,请参考课程地址:https://chuanke.baidu.com/s5508922.html ...
- 如何使用jquery.qrcode.js插件生成二维码
1.首先需要准备 jquery.qrcode.js 和 jquery.js github地址:https://github.com/lrsjng/jquery-qrcode 官方文档地址:http:/ ...
随机推荐
- linux CUDA安装
首先是安装依赖库 sudo apt-get install freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libgl1-m ...
- jquery 知识整理
大纲一.jQuery简介 二.jQuery 和Dom关系及jQuery版本 1.jQuery版本 2.jQuery和Dom转换 三.jQuery 选择器 1.1.基本 1.2.层级 2.基本筛选器 3 ...
- psutil:系统、进程,信息都在我的掌握之中
获取cpu的逻辑数量 import psutil print(psutil.cpu_count()) # 12 获取CPU的物理核心数 import psutil print(psutil.cpu_c ...
- wannacry分析--20199319
病毒概况 WannaCry病毒利用前阵子泄漏的方程式工具包中的"永恒之蓝"漏洞工具,进行网络端口扫描攻击,目标机器被成功攻陷后会从攻击机下载WannaCry病毒进行感染,并作为攻击 ...
- MXNetError: [05:53:50] src/operator/nn/./cudnn/cudnn_convolution-inl.h:287
insightface train.py 报错:mxnet.base.MXNetError: [05:53:50] src/operator/nn/./cudnn/cudnn_convolution- ...
- Java文档查看
对于Java学习者来说,阅读Java文档是必不可少的步骤,比如我现在想知道List接口的retianAll()方法,该怎么办呢? 当然是百度了!!! 皮一下,当然是查找Java文档了,以JDK1.7版 ...
- VAssistX 常用快捷键
函数跳转 Alt + G - 函数定义和声明的跳转Alt + O - 在.h与.cpp文件中实现相互转换Alt + M - 列出当前文件所有的函数Ctrl + Tab - 切换标签 查找 Ctrl + ...
- 在CentOS/Windows下配置Nginx(以及踩坑)
在CentOS/Windows下配置Nginx(以及踩坑) 1. 序言 因为这类文章网上比较多,实际操作起来也大同小异,所以我并不会着重于详细配置方面,而是将我配置时踩的坑写出来. 2. CentOS ...
- Unknown or unsupported command 'install'
由于电脑中存在多个python,导致pip install numpy出现标题这样的错误 结局方案; 在想要的python文件夹中的Scripts,shift右键点开命令行,pip.exe insta ...
- setAttribute()方法和 getAttribute() 方法
一.setAttribute() 方法 setAttribute() 方法为一个或一组元素添加指定的属性,并且为其赋指定的值.(主要针对自定义属性) 如果这个属性已经存在,仅仅设置或是修改属性值. 浏 ...