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

[英]What does the “!” character do in nodejs module names?


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加載的方式,所以沒有什么需要遵循感嘆號。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/01/21/7298b30dbc5f3c9c001f30abc9ce6a57.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com