扫描Javascript以获得浏览器兼容性 [英] Scan Javascript for browser compatibility

查看:220
本文介绍了扫描Javascript以获得浏览器兼容性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有工具可以扫描我的Javascript代码以查找可能并非在所有浏览器中都存在的功能?



我的库完全不是UI,所以我不关心某些内容是如何显示的。我正在寻找的是类似于Mozilla的Javascript MDN。例如,对于Array.prototype.indexOf,他们警告说这是最近的ECMAScript添加,并非在所有浏览器中都存在(并且通常提供存根)。
我正在寻找的工具列出了我的代码中属于此类别的函数。

解决方案

您可以使用



安装非常简单。你必须安装eslint和这个插件:

  npm install --save-dev eslint-plugin-compat 

  yarn add --dev eslint eslint-plugin-compat 

并添加一个ESlint配置文件:

  // .eslintrc 
{
extends:[plugin:compat / recommended]
}

将支持的浏览器添加到 package.json file:

  //示例配置(package.json)
{
// ...
browserslist:[最后2个Chrome版本,IE 11],
}

然后运行linter:

  eslint yourfile.js 

在我的情况下,这是输出:

  92:9错误IE 11中不支持Promise.all()compat / compat 
94:9错误IE 11中不支持Promise.all()compat / compat


Is there a tool out there to scan my Javascript code for functions that may not be present in all browsers?

My library is completely non-UI, so I don't care about how something is "displayed". What I'm looking for is something like in the Javascript MDN from Mozilla. For example, for Array.prototype.indexOf, they warn that it's a recent ECMAScript addition that is not present in all browsers (and typically provide a stub). What I'm looking for is a tool that'd list the functions in my code that would fall into this category.

解决方案

You can use eslint-plugin-compat, a plugin for the ESlint linting utility. You can even use Browserlist to configure the browsers you want to support.

Installation is very easy. You'll have to install eslint and this plugin:

npm install --save-dev eslint-plugin-compat

or

yarn add --dev eslint eslint-plugin-compat

And add a ESlint configuration file:

// .eslintrc
{
  "extends": ["plugin:compat/recommended"]
}

Add the supported browsers to your package.json file:

// sample configuration (package.json)
{
  // ...
  "browserslist": ["last 2 Chrome versions", "IE 11"],
}

And then run the linter:

eslint yourfile.js

In my case this was the output:

92:9   error  Promise.all() is not supported in IE 11  compat/compat
94:9   error  Promise.all() is not supported in IE 11  compat/compat

这篇关于扫描Javascript以获得浏览器兼容性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆