Home >Web Front-end >JS Tutorial >GitHub: Why are we deprecating jQuery?

GitHub: Why are we deprecating jQuery?

yulia
yuliaOriginal
2018-09-10 15:24:561930browse

Recently, we completely removed jQuery from the front-end code of GitHub.com, which marks the end of our gradual process of gradually removing jQuery over several years. This is a milestone for us. event. This article will describe how we relied on jQuery in the past, and over time we realized that we no longer needed it, but in the end we did not replace it with another library or framework, but used a standard browser The API implements everything we need.
JQuery means a lot to us in the early days

GitHub.com started using jQuery 1.2.1 in late 2007, a year before Google released the Chrome browser. There was no standard way to query DOM elements via CSS selectors, nor to dynamically render an element's style, and Internet Explorer's XMLHttpRequest interface, like many other APIs, suffered from inconsistencies between browsers.

jQuery makes it easy to manipulate DOM, create animations and "AJAX" requests, basically, it allows web developers to create more modern and dynamic web experiences. Best of all, code developed with jQuery for one browser will also work in other browsers. In GitHub's early days, jQuery enabled small development teams to quickly prototype and develop new features without having to tailor code specifically to each web browser.

The extension library built based on jQuery’s simple interface has also become the basic building block of the GitHub.com front-end: pjax (https://github.com/defunkt/jquery-pjax) and facebox (https:// github.com/defunkt/facebox).

We will never forget John Resig and the jQuery contributors for creating and maintaining such a useful and essential library.

Later Web Standards

Over the years, GitHub grew into a company with hundreds of engineers, and gradually established a dedicated team responsible for the scale and quality of JavaScript code. We've been excluding technical debt, and sometimes technical debt grows with dependencies that give us some value in the beginning, but that value also decreases over time.

We can compare jQuery to the rapid evolution of web standards supported by modern browsers:
$(selector) pattern can be replaced using querySelectorAll();
can now be replaced using Element.classList Implement CSS class name switching;
CSS now supports defining visual animations in style sheets instead of JavaScript;
You can now use Fetch Standard to perform $.ajax requests;
addEventListener() interface is stable enough , can be used across platforms;
We can use lightweight libraries to encapsulate event delegation patterns;
With the development of the JavaScript language, some of the syntax sugar provided by jQuery has become redundant.
Also, chain syntax does not satisfy the way we want to write code. For example:

$('.js-widget')
  .addClass('is-loading')
  .show()

This syntax is simple to write, but according to our standards, it does not convey our intention well. Does the author expect one or more js-widget elements on the current page? Also, if we update the page markup and accidentally omit the js-widget class name, will the browser throw an exception that will tell us what went wrong? By default, jQuery skips the entire expression when nothing matches the selector, but for us, this was a bug.

Finally, we started using Flow to annotate types to perform static type checking at build time, and we found that chain syntax is not suitable for static analysis because almost all jQuery methods return the same result. type. We chose Flow at the time because features like the @flow weak mode allowed us to gradually apply types to our untyped code base.

All in all, removing jQuery means we can rely more on web standards, make the MDN web documentation the de facto default documentation for front-end developers, maintain more resilient code in the future, and reduce the 30KB dependencies are removed from our bundles, speeding up page loading and JavaScript execution.

Gradual decoupling
Although we have set the ultimate goal, we also know that it is not feasible to allocate all resources to remove jQuery at once. This hasty approach may lead to regressions in website functionality. Instead, we adopted the following strategy:

1. 设定指标,跟踪整一行代码调用 jQuery 的比率,并监控指标走势随时间变化的情况,确保它保持不变或下降,而不是上升。
GitHub: Why are we deprecating jQuery?2. 我们不鼓励在任何新代码中导入 jQuery。为了方便自动化,我们创建了 eslint-plugin-jquery(https://github.com/dgraham/eslint-plugin-jquery),如果有人试图使用 jQuery 功能,例如 $.ajax,CI 检查将会失败。
3. 旧代码中存在大量违反 eslint 规则的情况,我们在代码注释中使用特定的 eslint-disable 规则进行了注解。看到这些代码的读者,他们都该知道,这些代码不符合我们当前的编码实践。
4. 我们创建了一个拉请求机器人,当有人试图添加新的 eslint-disable 规则时,会对拉取请求留下评论。这样我们就可以尽早参与代码评审,并提出替代方案。
5. 很多旧代码使用了 pjax 和 facebox 插件,所以我们在保持它们的接口几乎不变的同时,在内部使用 JS 重新实现它们的逻辑。静态类型检查有助于提升我们进行重构的信心。
6. 很多旧代码与 rails-behavior 发生交互,我们的 Ruby on Rails 适配器几乎是“不显眼的”JS,它们将 AJAX 生命周期处理器附加到某些表单上:

// 旧方法
  $(document).on('ajaxSuccess', 'form.js-widget', function(event, xhr, settings, data) {
    // 将响应数据插入到 DOM 中
  })

7. 我们选择触发假的 ajax* 生命周期事件,并保持这些表单像以前一样异步提交内容,而不是立即重写所有调用,只是会在内部使用 fetch()。
8. 我们自己维护了 jQuery 的一个版本,每当发现我们不再需要 jQuery 的某个模块的时候,就会将它从自定义版本中删除,并发布更轻量的版本。例如,在移除了 jQuery 的 CSS 伪选择器之后(如:visible 或:checkbox)我们就可以移除 Sizzle 模块了,当所有的 $.ajax 调用都被 fetch() 替换时,就可以移除 AJAX 模块。
这样做有两个目的:加快 JavaScript 执行速度,同时确保不会有新代码试图使用已移除的功能。
9. 我们根据网站的分析结果尽快放弃对旧版本 Internet Explorer 的支持。每当某个 IE 版本的使用率低于某个阈值时,我们就会停止向它提供 JavaScript 支持,并专注支持更现代的浏览器。尽早放弃对 IE 8 和 IE 9 的支持对于我们来说意味着可以采用很多原生的浏览器功能,否则的话有些功能很难通过 polyfill 来实现。
10. 作为 GitHub.com 前端功能开发新方法的一部分,我们专注于尽可能多地使用常规 HTML,并且逐步添加 JavaScript 行为作为渐进式增强。因此,那些使用 JS 增强的 Web 表单和其他 UI 元素通常也可以在禁用 JavaScript 的浏览器上正常运行。在某些情况下,我们可以完全删除某些遗留的代码,而不需要使用 JS 重写它们。

经过多年的努力,我们逐渐减少对 jQuery 的依赖,直到没有一行代码引用它为止。

自定义元素

近年来一直在炒作一项新技术,即自定义元素——浏览器原生的组件库,这意味着用户无需下载、解析和编译额外的字节。

从 2014 年开始,我们已经基于 v0 规范创建了一些自定义元素。然而,由于标准仍然在不断变化,我们并没有投入太多精力。直到 2017 年,Web Components v1 规范发布,并且 Chrome 和 Safari 实现了这一规范,我们才开始更广泛地采用自定义元素。

在移除 jQuery 期间,我们也在寻找用于提取自定义元素的模式。例如,我们将用于显示模态对话框的 facebox 转换为元素(https://github.com/github/details-dialog-element)。

我们的渐进式增强理念也延伸到了自定义元素上。这意味着我们将尽可能多地保留标记内容,然后再标记上添加行为。例如,默认显示原始时间戳,它被升级成可以将时间转换为本地时区,而对于,当它被嵌在

元素中时,可以在不使用 JavaScript 的情况下具备交互性,它被升级成具有辅助增强功能。

以下是实现自定义元素的示例:

// local-time 根据用户的当前时区显示时间。
//
// 例如:
//   <local-time datetime="2018-09-06T08:22:49Z">Sep 6, 2018</local-time>
//
class LocalTimeElement extends HTMLElement {
  static get observedAttributes() {
    return [&#39;datetime&#39;]
  }
  attributeChangedCallback(attrName, oldValue, newValue) {
    if (attrName === &#39;datetime&#39;) {
      const date = new Date(newValue)
      this.textContent = date.toLocaleString()
    }
  }
}
if (!window.customElements.get(&#39;local-time&#39;)) {
  window.LocalTimeElement = LocalTimeElement
  window.customElements.define(&#39;local-time&#39;, LocalTimeElement)
}

我们很期待 Web 组件的 Shadow DOM。Shadow DOM 的强大功能为 Web 带来了很多可能性,但也让 polyfill 变得更加困难。因为使用 polyfill 会导致性能损失,因此在生产环境中使用它们是不可行的。

The above is the detailed content of GitHub: Why are we deprecating jQuery?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn