简体   繁体   English

检查 Angular 项目中的浏览器可选链支持

[英]Check for browser optional chaining support in Angular project

In an Angular app, I want to check client's browser native support to optional chaining (es2020) in order to load a library which have a modern ES version and a legacy one.在 Angular 应用程序中,我想检查客户端的浏览器对可选链 (es2020) 的本机支持,以便加载具有现代 ES 版本和旧版本的库。

The problem is: Angular compiler (I believe it's actually tsc genera behaviour) transpiles everything to targeted ES version (es6 in my case), breaking the code I would use to check for optional chaining support (breaking in the sense that it won't work to check optional chaining support during runtime):问题是:Angular 编译器(我相信它实际上是 tsc 属的行为)将所有内容转换为目标 ES 版本(在我的情况下为 es6),破坏了我用来检查可选链支持的代码(破坏了它不会在运行时检查可选链接支持的工作):

 export function isOpChainingSupported(): boolean { const opChainingChecker = { support: true }; try { // CODE BEFORE TRANSPILING: // return opChainingChecker?.support; // // TURNS INTO return opChainingChecker === null || opChainingChecker === void 0 ? void 0 : opChainingChecker.support; } catch { return false; } }

I tried using moving this code to a function in a plain JS file, using the 'allowJs' TS config in order to import it without errors...no go: tsc ends up transpiling it too.我尝试将此代码移动到普通 JS 文件中的函数,使用“allowJs”TS 配置以便导入它而不会出错……不行:tsc 最终也将其转译。

Now I can see three options I don't really like:现在我可以看到三个我不太喜欢的选项:

  • Using eval('constObj?.prop'), which is not a good option according to most sources I've checked for security reasons;使用 eval('constObj?.prop'),根据我出于安全原因检查过的大多数来源,这不是一个好的选择;
  • Using my plain JS file as an asset, loading it dynamically;使用我的纯 JS 文件作为资产,动态加载它;
  • Creating a ES utility library for this function (my choice right now, even though I see it as an overkill for my problem).为这个函数创建一个 ES 实用程序库(我现在的选择,尽管我认为它对我的问题来说太过分了)。

What I wanted to ask is: is there something I'm not seeing?我想问的是:有什么我没有看到的吗? A simpler solution that I've missed?我错过了一个更简单的解决方案?

Sorry in advance if this is a bad question, but I feel like when we "tinker" too much with a problem, it's always better to ask people with a fresh view of the picture!如果这是一个不好的问题,请提前抱歉,但我觉得当我们对一个问题“修补”太多时,最好以全新的视角询问人们!

Cheers!干杯!

As I've already went with one of the options described in the question, I thought it would be good to answer it.由于我已经使用了问题中描述的选项之一,因此我认为回答它会很好。

In my first attempt, I've tried the "third party library" solution, but it didn't work: when there's a syntax error (which is the case if we use optional chaining in a browser without support), the exception happens before the browser actually runs that code, leading to two results:在我的第一次尝试中,我尝试了“第三方库”解决方案,但没有奏效:当出现语法错误时(如果我们在不支持的浏览器中使用可选链接就是这种情况),则异常发生在之前浏览器实际运行该代码,导致两个结果:

  • The exception won't be caught in a try/catch block;异常不会在 try/catch 块中捕获;
  • The current "execution context" will end abruptly.当前的“执行上下文”将突然结束。

So, I needed a way to run the "checker code" in a separate context, in order to keep my main thread (and consequently, my app) from dying.因此,我需要一种在单独的上下文中运行“检查器代码”的方法,以防止我的主线程(以及我的应用程序)死亡。 Also, I needed a way to check if the code ran successfully or not.另外,我需要一种方法来检查代码是否成功运行。

The final solution I've come up with is a mix between two of the options: I've created a library which loads a simple script testing for optional chaining support, returning a Promise with a boolean indicating the browser's support.我想出的最终解决方案是两种选项的混合:我创建了一个库,它加载一个简单的脚本测试以获取可选链支持,返回一个带有布尔值的 Promise 来指示浏览器的支持。

The script is loaded dinamically, by adding a temporary script tag (which isolates the execution from the main context, preventing the SyntaxError from killing the app).通过添加临时脚本标签(将执行与主上下文隔离,防止 SyntaxError 终止应用程序),脚本动态加载。 In order to improve security, I've used the integrity attribute.为了提高安全性,我使用了完整性属性。

If anyone is going through the same issue, you can use my solution by installing the npm library optional-chaining-checker and following the instructions.如果有人遇到同样的问题,您可以通过安装 npm 库optional-chaining-checker并按照说明使用我的解决方案。 I've tried to make it as customizable as I could, but if it doesn't attend your needs, feel free to check out my repository and adapt the code at will!我已尝试尽可能地使其可定制,但如果它不符合您的需求,请随时查看我的存储库并随意调整代码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM