dust helper 依赖项是否捆绑在已编译的模板中?

are dust helper dependencies bundled within compiled templates?

我想知道如何在已编译的 dust 模板中管理帮助程序依赖项,特别是与在客户端上使用相关的帮助程序依赖项 - 已编译的 dust 模板是否捆绑了帮助程序方法?客户端可能不支持的依赖项怎么办?或者如果该依赖项有多个其他依赖项?

这是我希望能够在客户端上使用的灰尘模板的一个简单示例:

// foo.dust
{@myHelper}
  <div>{foo}{bar}</div>
{/myHelper}


// my-helper.js
const isomorphicDep = require('isomorphic-dep');
const nodeDep = require('node-dep');

module.exports = function(dust) {
  dust.helpers.myHelper = function(chunk, context, bodies, params) {
    // do some stuff using deps
    let foo = nodeDep.getFoo();
    let bar = isomorphicDep.getBar(params.someInput);

    return chunk.render(bodies.block, context.push({ foo, bar });
  };
};

谢谢

编译后的模板仅包含有关如何呈现的说明 -- 它本身不包含任何客户端代码。

例如,像这样的简单模板:

{@helper}foo{/helper}

编译成这两个指令集:

function body_0(chk, ctx) {
  return chk.h("helper", ctx, {
    "block": body_1
  }, {}, "h");
}

function body_1(chk, ctx) {
  return chk.w("foo");
}

当模板被渲染时,它要求 Dust 寻找名为 helper 的助手并执行它(在 body_0 函数中)。 helper 的代码未包含在模板中。

因此在客户端上,您需要包含一个文件,其中包含加载正确同构 dep 的帮助程序(例如 node-fetch 与 whatwg-fetch)。