Skip to content
This repository has been archived by the owner on Sep 6, 2022. It is now read-only.

Commit

Permalink
Re-use code generation functions of Meteor-core
Browse files Browse the repository at this point in the history
Meteor now exports the functions that generate the runtime code for
templates and the page body, so let's use that so that we don't break
in case these functions change in the future.
  • Loading branch information
mquandalle committed Dec 26, 2015
1 parent b990acc commit be24a1b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"Package": false,
"SpacebarsCompiler": false,
"Template": false,
"TemplatingTools": false,
"Tinytest": false,

// Our globals variables
Expand Down
1 change: 1 addition & 0 deletions packages/jade/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Package.registerBuildPlugin({
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"mquandalle:[email protected]",
],
sources: [
Expand Down
25 changes: 9 additions & 16 deletions packages/jade/plugin/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,22 @@ class JadeCompilerPlugin extends CachingHtmlCompiler {
}

// XXX Handle body attributes
_bodyGen(tpl, attrs) {
const renderFunction = SpacebarsCompiler.codeGen(tpl, {
_bodyGen(body) {
const renderFuncCode = SpacebarsCompiler.codeGen(body, {
isBody: true,
sourceName: "<body>"
});

return `
Meteor.startup(function() { $('body').attr(${JSON.stringify(attrs)}); });
Template.body.addContent(${renderFunction});
Meteor.startup(Template.body.renderToDocument);
`;
return TemplatingTools.generateBodyJS(renderFuncCode);
}

_templateGen(tree, tplName) {
const nameLiteral = JSON.stringify(tplName);
const templateDotNameLiteral = JSON.stringify(`Template.${tplName}`);
const renderFunction = SpacebarsCompiler.codeGen(tree, {
const renderFuncCode = SpacebarsCompiler.codeGen(tree, {
isTemplate: true,
sourceName: `Template "${tplName}"`
});

return `
Template.__checkName(${nameLiteral});
Template[${nameLiteral}] =
new Template(${templateDotNameLiteral}, ${renderFunction});
`;
return TemplatingTools.generateTemplateJS(tplName, renderFuncCode);
}

_getCompilerResult(mode, file) {
Expand All @@ -84,11 +74,14 @@ class JadeCompilerPlugin extends CachingHtmlCompiler {
}

if (results.body !== null) {
js += this._bodyGen(results.body, results.bodyAttrs);
js += this._bodyGen(results.body);
}
if (! _.isEmpty(results.templates)) {
js += _.map(results.templates, this._templateGen).join("");
}
if (! _.isEmpty(results.bodyAttrs)) {
bodyAttrs = results.bodyAttrs;
}

return { head, body, js, bodyAttrs };
}
Expand Down

0 comments on commit be24a1b

Please sign in to comment.