SystemJS:命名的 AMD 模块有时默认为空

SystemJS: Named AMD modules sometimes have empty defaults

这是基于this functional demo

我看到未命名的模块被 SystemJS 0.21.4 作为空对象导入。

// define('a', [], function () { return 'A'; });
SystemJS.import('a.js')
  .then(m => console.log(m.default, '<- a')) // "A"

// define('b', [], function () { return 'B'; });
SystemJS.import('b.js')
  .then(m => console.log(m.default, '<- b')) // {}

// define([], function () { return 'A'; });
SystemJS.import('a-anon.js')
  .then(m => console.log(m.default, '<- a-anon'))  // "A"

// define([], function () { return 'B'; });
SystemJS.import('b-anon.js')
  .then(m => console.log(m.default, '<- b-anon')) // "B"

出于某种原因,a.js 正确导入为 "A",但 b.js 导入为空对象(而不是 "B")。如果我重新排序它们以便首先导入 b.js,我会看到相反的情况——b 正确导入而 a 不正确。

这是怎么回事?这是一个错误吗?我使用 SystemJS/AMD 不正确吗?

交叉发布from Github

这里的问题是第一个参数,"id",它必须是顶级的:

The first argument, id, is a string literal. It specifies the id of the module being defined. This argument is optional, and if it is not present, the module id should default to the id of the module that the loader was requesting for the given response script. When present, the module id MUST be a "top-level" or absolute id (relative ids are not allowed).

More details on the AMD-page on github.

B 的情况下,只返回模块的对象。此处的模块是进行定义的功能。

还有一个有趣的地方,即使它可能并不太令人惊讶:
如果第2个URL先加载(b.js)即使脚本中的排序是a.js、b.js,那么B也是赋值的Symbol。这意味着第一个传输的定义文件获胜。你可以通过从cdn加载一个文件,另一个从本地加载一个文件来测试它。

此外,如果您将 C 和 D 分配为与 A 和 B 相同的类型,那么对象也会显示出来。所以行为与 B.

相同

就像您在最后两个示例中展示的那样,当省略作为可选参数的 id 时,问题永远不会发生。