Skip to content

Commit

Permalink
Syncronous import via tags (#85)
Browse files Browse the repository at this point in the history
pass `<can-import>`ed modules to stache in scope so they can be available synchronously
  • Loading branch information
nlundquist authored Jul 26, 2019
1 parent c7a718e commit 17469ed
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
23 changes: 17 additions & 6 deletions steal-stache.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,32 @@ var loader = require("@loader");
var addImportSpecifiers = require("steal-config-utils/import-specifiers").addImportSpecifiers;

function template(imports, intermediate, filename){
var tagImportNames = JSON.stringify(imports.slice(7));
imports = JSON.stringify(imports);
intermediate = JSON.stringify(intermediate);

return "define("+imports+",function(module, assign, stache, mustacheCore, viewImport, bindings){ \n" +
"\tstache.addBindings(bindings);\n"+
return "define("+imports+",function(module, assign, stache, mustacheCore, Scope, viewImport, bindings){ \n" +
"\tstache.addBindings(bindings);\n"+
(filename ?
"\tvar renderer = stache(" + JSON.stringify(filename) + ", " + intermediate + ");\n" :
"\tvar renderer = stache(" + intermediate + ");\n"
) +
"\tvar tagImports = Array.prototype.slice.call(arguments, 7);\n" +
"\treturn function(scope, options, nodeList){\n" +
"\t\tvar moduleOptions = assign({}, options);\n" +
"\t\tif(moduleOptions.helpers) {\n" +
"\t\t\tmoduleOptions.helpers = assign({ module: module }, moduleOptions.helpers);\n" +
"\t\t} else {\n" +
"\t\t\tmoduleOptions.module = module;\n" +
"\t\tvar tagImportMap = " + tagImportNames + ".reduce(function(map, name, index) {\n" +
"\t\t\tmap[name] = tagImports[index];\n" +
"\t\t\treturn map;\n" +
"\t\t}, {});\n" +
"\n"+
"\t\tif (!(scope instanceof Scope)) { scope = new Scope(scope); }\n" +
"\t\tvar variableScope = scope.getScope(function(s) { return s._meta.variable === true });\n" +
"\t\tif (!variableScope) {\n" +
"\t\t\tscope = scope.addLetContext();\n" +
"\t\t\tvariableScope = scope;\n" +
"\t\t}\n" +
"\t\tassign(variableScope._context, { module: module, tagImportMap: tagImportMap });\n" +
"\n" +
"\t\treturn renderer(scope, moduleOptions, nodeList);\n" +
"\t};\n" +
"});";
Expand Down Expand Up @@ -75,6 +85,7 @@ function translate(load) {
ast.imports, imports
);

ast.imports.unshift("can-view-scope");
ast.imports.unshift("can-stache/src/mustache_core");
ast.imports.unshift("can-stache");
ast.imports.unshift("can-assign");
Expand Down
21 changes: 17 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
var template = require("./template.stache!");
var stache = require("can-stache");
var QUnit = require("steal-qunit");
var loader = require("@loader");
var clone = require("steal-clone");
Expand All @@ -25,6 +23,21 @@ QUnit.test("can-import works", function(assert) {
});
});

QUnit.test("`can-import`ed modules are available synchronously, and in a LetContext", function(assert){
var done = assert.async();

loader["import"]("test/tests/baz.stache").then(function(template) {
template({
test: function(value, scope) {
assert.equal(value, "works", "Initial render occurs with can-import already completed.");
assert.ok(scope._meta.variable, "Bottom scope is a LetContext.");
assert.equal(scope._context._data.bar, "works", "`bar` variable is in the LetContext.");
done();
}
});
});
});

QUnit.test("error messages includes the source", function(assert) {
var done = assert.async();

Expand Down Expand Up @@ -60,7 +73,7 @@ QUnit.test("module info is set when 'options' is missing", function(assert) {
var done = assert.async(2);

tag("fake-import", function fakeImport(el, tagData) {
var m = tagData.scope.get("scope.helpers.module");
var m = tagData.scope.get("module");
assert.ok(m.id.includes("test/module-meta/index"));
done();
});
Expand All @@ -75,7 +88,7 @@ QUnit.test("module info is set when 'options.helpers' exists", function(assert)
var done = assert.async(2);

tag("fake-import", function fakeImport(el, tagData) {
var m = tagData.scope.get("scope.helpers.module");
var m = tagData.scope.get("module");
assert.ok(m.id.includes("test/module-meta/index"));
done();
});
Expand Down
2 changes: 2 additions & 0 deletions test/tests/baz.stache
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<can-import from='test/tests/bar' module:to="bar" />
{{ test(bar, scope) }}

0 comments on commit 17469ed

Please sign in to comment.