Skip to content

Commit

Permalink
Embed the template text into the layer file rather than the AST.
Browse files Browse the repository at this point in the history
Turns out that it's problematic even to create the AST at build time, because then
(during the build) handlebars.js will create dummy custom elements (ex: <d-button>)
for introspection.  They are used to find out when to call element.foo = false
rather than element.foo = "false".

Creating the dummy elements first requires running the code to define the custom elements.
That wasn't working right during builds, because the javascript files were
referencing symbols that don't exist in Node, such as HTMLElement.

Rolls back more of (and most of) 2c0633f.

Fixes #438 for real, and refs #432.
  • Loading branch information
wkeese committed Mar 11, 2016
1 parent fc33d98 commit b27e3e8
Showing 1 changed file with 21 additions and 26 deletions.
47 changes: 21 additions & 26 deletions handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
* Both functions are meant
* to be run in the context of the widget, so that properties are available through `this`.
*
* Could also theoretically be used by a build-tool to precompile templates, assuming you loaded
* [jsdom](https://github.com/tmpvar/jsdom) to provide methods like `document.createElement()`.
* But the problem is that the build tool has to load the definitions for the custom elements
* referenced in the templates, in order to get the types of their properties.
*
* Template has a format like:
*
* ```html
Expand Down Expand Up @@ -90,7 +95,6 @@ define([
* @private
*/
parse: function (templateNode, xmlns) {
/* jshint maxcomplexity:15 */
// Get tag name, reversing the tag renaming done in toDom()
var tag = templateNode.hasAttribute("is") ? templateNode.getAttribute("is") :
templateNode.tagName.replace(/^template-/i, "").toLowerCase(),
Expand Down Expand Up @@ -124,24 +128,14 @@ define([
}
}

// Process children.
var children = handlebars.parseChildren(templateNode, xmlns);

// Return AST structure pruning empty arrays etc. Minimizes size of serialized object.
function hasMembers(obj) {
/* jshint unused: false */
for (var key in obj) {
return true;
}
return false;
}
var ret = { tag: tag };
if (xmlns) { ret.xmlns = xmlns; }
if (hasMembers(attributes)) { ret.attributes = attributes; }
if (hasMembers(connects)) { ret.connects = connects; }
if (children.length) { ret.children = children; }
if (hasMembers(attachPoints)) { ret.attachPoints = attachPoints; }
return ret;
return {
tag: tag,
xmlns: xmlns,
attributes: attributes,
connects: connects,
children: handlebars.parseChildren(templateNode, xmlns),
attachPoints: attachPoints
};
},

/**
Expand Down Expand Up @@ -218,6 +212,7 @@ define([
return children.slice(0, lastRealNode + 1); // slice() removes trailing whitespace nodes
},


/**
* Neutralize custom element tags.
* Rename all the elements in the template so that:
Expand Down Expand Up @@ -347,26 +342,26 @@ define([
onload();
};

// Inline the template AST and list of dependencies into the layer file.
// Inline the template text and list of dependencies into the layer file.
handlebars.write = function (pluginName, moduleName, write) {
var dom = jsdom(handlebars.neutralizeTags(templateText)),
template = dom.querySelector("template-template"),
var dom = jsdom(templateText),
template = dom.querySelector("template"),
requiresAttr = template.getAttribute("requires") || template.getAttribute("data-requires");
templateRequires = requiresAttr ? requiresAttr.split(/,\s*/) : [];
template.removeAttribute("requires");
template.removeAttribute("data-requires");

var moduleRequires = ["delite/Template"].concat(templateRequires);
var tree = handlebars.parse(template);
var moduleText = "define(" + JSON.stringify(moduleRequires) + ", function(Template){\n" +
"\treturn (new Template(" + JSON.stringify(tree) + ")).func;\n" +
var moduleRequires = [module.id].concat(templateRequires);
var moduleText = "define(" + JSON.stringify(moduleRequires) + ", function(handlebars){\n" +
"\treturn handlebars.compile(" + JSON.stringify(template.outerHTML) + ");\n" +
"});";
write.asModule(pluginName + "!" + moduleName, moduleText);
};

// Notify builder to take dependencies specified in requires="..." attribute, and inline them into the layer.
handlebars.addModules = function (pluginName, resource, addModules) {
addModules(templateRequires);

};
}

Expand Down

0 comments on commit b27e3e8

Please sign in to comment.