“!”字符在nodejs模块名称中的作用是什么?

时间:2022-11-26 20:18:49

I've started using the intern library to write functional tests in js, and I realized that I couldn't understand this syntax:

我已经开始使用实习库在js中编写功能测试,我意识到我无法理解这种语法:

var assert = require('intern/chai!assert');
var registerSuite = require('intern!object');

What is the purpose of the ! character in the argument of the require() method?

这是什么目的! require()方法的参数中的字符?

2 个解决方案

#1


6  

Short answer

简短的回答

It identifies a resource that is part of a plugin.
The structure of the identifier is: [plugin]![resource].

它标识了作为插件一部分的资源。标识符的结构是:[plugin]![resource]。

Long answer

答案很长

In the documentation, you can find that:

在文档中,您可以找到:

Intern is built on top of a standard amd loader, which means that its modules are also normally written in the AMD module format.

Intern构建在标准的amd加载器之上,这意味着它的模块通常也以AMD模块格式编写。

That's why and how the require function is actually injected, so it's clear that you are not using the require module provided along with Node.JS.

这就是为什么以及如何实际注入require函数,因此很明显你没有使用与Node.JS一起提供的require模块。

It states also that:

它还指出:

[AMD format] Allows modules and other assets to be asynchronously or conditionally resolved by writing simple loader plugins

[AMD格式]允许通过编写简单的加载程序插件来异步或有条件地解析模块和其他资产

If you follow the link provided with the documentation when it cites the loaders, you find that:

如果您在引用加载程序时按照文档提供的链接进行操作,则会发现:

Loader plugins extend an AMD implementation by allowing loading of resources that are not traditional JavaScript dependencies.

Loader插件通过允许加载非传统JavaScript依赖项的资源来扩展AMD实现。

In particular, you can find that a dependency has the following form:

特别是,您可以发现依赖项具有以下形式:

[Plugin Module ID]![resource ID]

It goes without saying that the default implementation of the loader you get by using intern adheres to the above mentioned standard.

不言而喻,使用实习生获得的加载器的默认实现遵循上述标准。

That said, it follows that, if we consider:

那就是说,如果我们考虑:

intern/chai!assert

You can read it as inter/chai as plugin module and assert as actually required resource.

您可以将其读作inter / chai作为插件模块并断言为实际所需的资源。

The purpose of the ! character in the argument of the require() method is to satisfy the requirements of the syntax used to identify a resource that is itself part of a plugin.

的目的! require()方法的参数中的字符是满足用于标识资源的语法要求,该资源本身是插件的一部分。

#2


4  

I got this answer for you extracted from this similiar question and very well explained by explunit:

我从这个类似的问题中提取了这个答案,并且很好地解释了explunit:

The exclamation point syntax is reserved for plugins. Typically there would be something else following the exclamation point which indicates the resource that the plugin would load, e.g. text!myTemplate.html, but in the case of domReady! the plugin exists just as a way to wait until DOM loaded before invoking your callback function, so nothing needs to follow the exclamation point.

感叹号语法是为插件保留的。通常,在感叹号之后会有其他东西指示插件将加载的资源,例如, text!myTemplate.html,但是在domReady的情况下!插件的存在只是一种在调用回调函数之前等待DOM加载的方式,所以没有什么需要遵循感叹号。

#1


6  

Short answer

简短的回答

It identifies a resource that is part of a plugin.
The structure of the identifier is: [plugin]![resource].

它标识了作为插件一部分的资源。标识符的结构是:[plugin]![resource]。

Long answer

答案很长

In the documentation, you can find that:

在文档中,您可以找到:

Intern is built on top of a standard amd loader, which means that its modules are also normally written in the AMD module format.

Intern构建在标准的amd加载器之上,这意味着它的模块通常也以AMD模块格式编写。

That's why and how the require function is actually injected, so it's clear that you are not using the require module provided along with Node.JS.

这就是为什么以及如何实际注入require函数,因此很明显你没有使用与Node.JS一起提供的require模块。

It states also that:

它还指出:

[AMD format] Allows modules and other assets to be asynchronously or conditionally resolved by writing simple loader plugins

[AMD格式]允许通过编写简单的加载程序插件来异步或有条件地解析模块和其他资产

If you follow the link provided with the documentation when it cites the loaders, you find that:

如果您在引用加载程序时按照文档提供的链接进行操作,则会发现:

Loader plugins extend an AMD implementation by allowing loading of resources that are not traditional JavaScript dependencies.

Loader插件通过允许加载非传统JavaScript依赖项的资源来扩展AMD实现。

In particular, you can find that a dependency has the following form:

特别是,您可以发现依赖项具有以下形式:

[Plugin Module ID]![resource ID]

It goes without saying that the default implementation of the loader you get by using intern adheres to the above mentioned standard.

不言而喻,使用实习生获得的加载器的默认实现遵循上述标准。

That said, it follows that, if we consider:

那就是说,如果我们考虑:

intern/chai!assert

You can read it as inter/chai as plugin module and assert as actually required resource.

您可以将其读作inter / chai作为插件模块并断言为实际所需的资源。

The purpose of the ! character in the argument of the require() method is to satisfy the requirements of the syntax used to identify a resource that is itself part of a plugin.

的目的! require()方法的参数中的字符是满足用于标识资源的语法要求,该资源本身是插件的一部分。

#2


4  

I got this answer for you extracted from this similiar question and very well explained by explunit:

我从这个类似的问题中提取了这个答案,并且很好地解释了explunit:

The exclamation point syntax is reserved for plugins. Typically there would be something else following the exclamation point which indicates the resource that the plugin would load, e.g. text!myTemplate.html, but in the case of domReady! the plugin exists just as a way to wait until DOM loaded before invoking your callback function, so nothing needs to follow the exclamation point.

感叹号语法是为插件保留的。通常,在感叹号之后会有其他东西指示插件将加载的资源,例如, text!myTemplate.html,但是在domReady的情况下!插件的存在只是一种在调用回调函数之前等待DOM加载的方式,所以没有什么需要遵循感叹号。