diff --git a/steal-stache.js b/steal-stache.js index ea228f8..9ad06eb 100644 --- a/steal-stache.js +++ b/steal-stache.js @@ -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" + "});"; @@ -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"); diff --git a/test/test.js b/test/test.js index b3ce913..b2615aa 100644 --- a/test/test.js +++ b/test/test.js @@ -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"); @@ -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(); @@ -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(); }); @@ -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(); }); diff --git a/test/tests/baz.stache b/test/tests/baz.stache new file mode 100644 index 0000000..9d6dd64 --- /dev/null +++ b/test/tests/baz.stache @@ -0,0 +1,2 @@ + +{{ test(bar, scope) }} \ No newline at end of file