如何对es6模块进行特征检测 [英] How to feature-detect es6 modules

查看:130
本文介绍了如何对es6模块进行特征检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想在当前运行时支持使用ES6语法导出模块。目标是开发一个支持不同模块管理系统的库。



由于 export 是一个关键字,将抛出语法错误:

  typeof export 


解决方案

修订版本27 具有可用于模块反射的 Reflect.Loader API。



虽然这不是导出关键字本身的直接特征检测,但是可能会从数据uri加载使用导出关键字的模块,然后检查是否引发解析错误。 / p>

但是,它已被删除,修订版本28具有以下更改日志条目:








因此,据我所知,该规范似乎没有提供任何方式的功能



将来可能会使用 Reflect.Loader 或其替换。



编辑:装载机规格似乎是由whatwg管理的,但它尚未处于可以从中导出特征检测的状态。


I'd like to export a module using the ES6 syntax only if it is supported in the current runtime. The goal is to develop a library that support different module management systems.

As export is a keyword, something like will throw a syntax error :

typeof export

解决方案

Revision 27 of the spec had a Reflect.Loader API that could be used for module reflection.

While that isn't direct feature detection for the export keyword in itself, it might have been possible to load a module that uses export keywords from a data-uri and then check whether it throws parse errors or not.

But it has been removed with revision 28, with the following changelog entry:

Removed loader pipeline and Reflect.Loader API (functionality being transferred to separate specification)


Thus, as far as i can tell, the spec does not seem to provide any way of feature detection at the time of writing.

In the future it might be possible with Reflect.Loader or its replacements.

Edit: The loader spec seems to be developed managed by the whatwg, but it's not yet in a state from which we could derive feature detection.

这篇关于如何对es6模块进行特征检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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