From be24a1bc4d7bea92847a59dcd06089dc654d14ac Mon Sep 17 00:00:00 2001 From: Maxime Quandalle Date: Sat, 26 Dec 2015 23:20:40 +0100 Subject: [PATCH] Re-use code generation functions of Meteor-core 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. --- .eslintrc | 1 + packages/jade/package.js | 1 + packages/jade/plugin/handler.js | 25 +++++++++---------------- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/.eslintrc b/.eslintrc index eb3efd4..c6876af 100644 --- a/.eslintrc +++ b/.eslintrc @@ -17,6 +17,7 @@ "Package": false, "SpacebarsCompiler": false, "Template": false, + "TemplatingTools": false, "Tinytest": false, // Our globals variables diff --git a/packages/jade/package.js b/packages/jade/package.js index 313ff42..fd60e22 100644 --- a/packages/jade/package.js +++ b/packages/jade/package.js @@ -15,6 +15,7 @@ Package.registerBuildPlugin({ "htmljs@1.0.0", "minifiers@1.0.0", "spacebars-compiler@1.0.0", + "templating-tools@1.0.0", "mquandalle:jade-compiler@0.4.5", ], sources: [ diff --git a/packages/jade/plugin/handler.js b/packages/jade/plugin/handler.js index 243db23..317aa42 100644 --- a/packages/jade/plugin/handler.js +++ b/packages/jade/plugin/handler.js @@ -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: "" }); - 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) { @@ -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 }; }