当导入的文件正在导入一个文件,而导入它的文件也在导入时会发生什么?

What happens when a file that is imported is importing a file that the file importing it is also importing?

我有三个文件: - main.js <- 依赖项:module.js、helper.js - module.js <- 依赖项:helper.js - helper.js <- 无依赖关系

main.js 和 module.js 都从 helper.js 导入; main.js 从 module.js

导入

main.js 从 module.js 导入的函数使用了 helper.js

的函数

当我运行 main.js: 将helper.js导入两次,一次由main.js导入,一次由module.js导入,或者helper.js ] 仅被 main.js?

导入一次

main.js:

    import {someFunction} from "./helper.js";
    import {someOtherFunction} from "./module.js";
    someFunction();
    someOtherFunction();

module.js:

    import {someFunction} from "./helper.js";
    function someOtherFunction(){
        // do something using someFunction();
        someFunction();
    };
    export {someOtherFunction};

helper.js:

   function someFunction(){
       // do something
   }; 
   export {someFunction};

一个模块总是加载一次。

ES 262 规范在第 413 页指出:

[Importing a module] must be idempotent if it completes normally. Each time it is called with a specific referencingModule, specifier pair as arguments it must return the same Module Record instance.

Multiple different referencingModule, specifier pairs may map to the same Module Record instance. The actual mapping semantic is implementation-defined but typically a normalization process is applied to specifier as part of the mapping process. A typical normalization process would include actions such as alphabetic case folding and expansion of relative and abbreviated path specifiers.