简体   繁体   English

使用SystemJS加载捆绑的AMD模块

[英]Loading bundled AMD modules with SystemJS

I have a couple of AMD modules compiled using TypeScript's --outFile option into a single file: 我有几个使用TypeScript的--outFile选项编译成单个文件的AMD模块:

define("partA", ["require", "exports"], function (require, exports) {
    "use strict";
    function partAFunc() {
        console.log('partAFunc');
        return 'partAFunc';
    }
    exports.partAFunc = partAFunc;
});
define("partB", ["require", "exports"], function (require, exports) {
    "use strict";
    exports.partB = 42;
});
define("partC", ["require", "exports"], function (require, exports) {
    ...
});

Now I want to load only the partA module and call its partAfunc() so I can do the following in Node.js: 现在,我只想加载partA模块并调用其partAfunc()以便可以在Node.js中执行以下操作:

SystemJS.config({
  map: {
    'main': 'my-bundle.js',
  },
});

SystemJS.import('main').then((m) => {
  SystemJS.import('partA').then((m) => {
    m.partAFunc();
  });
});

The first import SystemJS.import('main') just registers all the modules and then SystemJS.import('partA') works because module partA is already registered (or at least I guess that's what it does). 第一个导入SystemJS.import('main')仅注册所有模块,然后SystemJS.import('partA')工作,因为模块partA已被注册(或者至少我想是这样做的)。

However, why I can't just use SystemJS.import('partA') and set the bundle as a dependency: 但是,为什么我不能只使用SystemJS.import('partA')并将捆绑软件设置为依赖项:

SystemJS.config({
  meta: {
    'partA': {
      deps: [ 'my-bundle.js' ],
    }
  }
});

SystemJS.import('partA').then((m) => {
  m.partAFunc();
});

The meta is completely ignored. meta被完全忽略。 The doc at https://github.com/systemjs/systemjs/blob/master/docs/config-api.md#meta says: https://github.com/systemjs/systemjs/blob/master/docs/config-api.md#meta上的文档说:

Dependencies to load before this module. 加载此模块之前的依赖项。 Goes through regular paths and map normalization. 经历常规路径并进行地图归一化。 Only supported for the cjs, amd and global formats. 仅支持cjs,amd和全局格式。

It looks like SystemJS first checks whether the file partA exists (which obviously doesn't) and throws an error (I tested it with an existing file and the meta config worked): 看起来SystemJS首先检查文件partA是否存在(显然不存在)并引发错误(我使用现有文件进行了测试,并且meta配置起作用):

(node:60981) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: ENOENT: no such file or directory, open '/Users/.../partA'
  Instantiating /Users/.../partA
  Loading partA

I'd expect that also the following should work when the first variant works with two nested SystemJS.import calls. 我希望当第一个变量与两个嵌套的SystemJS.import调用一起使用时,以下内容也应该起作用。

SystemJS.config({
  map: {
    'partA': 'my-bundle.js',
  },
});

SystemJS.import('partA').then((m) => {
  // m.partAFunc();
  console.log(m)
});

This prints an empty object. 这将打印一个空对象。 It looks like when there're more than one module in a single file it just registers them and doesn't load any of them? 看起来当一个文件中有多个模块时,它只是注册它们而没有加载它们中的任何一个?

I read all the documents in https://github.com/systemjs/systemjs/tree/master/docs but I think I'm still lost. 我阅读了https://github.com/systemjs/systemjs/tree/master/docs中的所有文档,但是我仍然迷路了。

What you need to do is use the bundles setting and set your bundle like this: 您需要做的是使用bundles设置,并按以下方式设置bundle:

    bundles: {
      'my-bundle.js': ['partA', 'partB', 'partC'],
    }, 

Roughly, this tells SystemJS "when you look for module partA , fetch and execute the module named my-bundle.js and you'll find partA there." 粗略地讲,这告诉SystemJS“当您寻找模块partA ,获取并执行名为my-bundle.js的模块,您将在其中找到partA 。”


Your approach using meta cannot work. 您使用meta方法行不通。 Your meta setting does not say "don't try to fetch a module named partA and instead fetch my-bundle.js " it says "when you process partA , in addition to the dependencies it already has , add my-bundle.js to the list of dependencies." 您的meta设置不会说“不要尝试获取名为partA的模块, 而是要获取my-bundle.js ”,而是说“当您处理partA除了已经拥有的依赖项之外,还要my-bundle.js添加到依赖项列表。” SystemJS will still fetch partA . SystemJS仍将获取partA There's no reason for it to wait until it has executed my-bundle.js before it tries to fetch it, so it launches the fetch immediately and it fails. 它没有理由等待它执行my-bundle.js才尝试获取它,因此它会立即启动获取并失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM