Skip to content

Commit

Permalink
Customize hbs plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaurav0 committed Jan 9, 2017
1 parent ad1c499 commit d0ec148
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 7 deletions.
86 changes: 86 additions & 0 deletions app/plugins/hbs-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
export default function(babel, _options) {
var options = _options || {};

function htmlbarsInlineCompilerPlugin(babel) {
var t = babel.types;

var replaceNodeWithPrecompiledTemplate = function(node, template) {
var compiledTemplateString = "Ember.HTMLBars.compile(`" + template + "`)";

// Prefer calling replaceWithSourceString if it is present.
// this prevents a deprecation warning in Babel 5.6.7+.
//
// TODO: delete the fallback once we only support babel >= 5.6.7.
if (node.replaceWithSourceString) {
node.replaceWithSourceString(compiledTemplateString);
} else {
return compiledTemplateString;
}
};


return new babel.Transformer('htmlbars-inline-precompile', {
ImportDeclaration: function(node, parent, scope, file) {
if (t.isLiteral(node.source, { value: "htmlbars-inline-precompile" })) {
var first = node.specifiers && node.specifiers[0];
if (t.isImportDefaultSpecifier(first)) {
file.importSpecifier = first.local.name;
} else {
var input = file.code;
var usedImportStatement = input.slice(node.start, node.end);
var msg = "Only `import hbs from 'htmlbars-inline-precompile'` is supported. You used: `" + usedImportStatement + "`";
throw file.errorWithNode(node, msg);
}

// Prefer calling dangerouslyRemove instead of remove (if present) to
// suppress a deprecation warning.
//
// TODO: delete the fallback once we only support babel >= 5.5.0.
if (typeof this.dangerouslyRemove === 'function') {
this.dangerouslyRemove();
} else {
this.remove();
}
}
},

CallExpression: function(node, parent, scope, file) {
if (t.isIdentifier(node.callee, { name: file.importSpecifier })) {
var argumentErrorMsg = "hbs should be invoked with a single argument: the template string";
if (node.arguments.length !== 1) {
throw file.errorWithNode(node, argumentErrorMsg);
}

var template = node.arguments[0].value;
if (typeof template !== "string") {
throw file.errorWithNode(node, argumentErrorMsg);
}

return replaceNodeWithPrecompiledTemplate(this, template);
}
},

TaggedTemplateExpression: function(node, parent, scope, file) {
if (t.isIdentifier(node.tag, { name: file.importSpecifier })) {
if (node.quasi.expressions.length) {
throw file.errorWithNode(node, "placeholders inside a tagged template string are not supported");
}

var template = node.quasi.quasis.map(function(quasi) {
return quasi.value.cooked;
}).join("");

return replaceNodeWithPrecompiledTemplate(this, template);
}
}
});
}

// used by broccoli-babel-transpiler to bust the cache when
// the template compiler being used changes
htmlbarsInlineCompilerPlugin.cacheKey = function() {
return options.cacheKey;
};

return htmlbarsInlineCompilerPlugin;
}
5 changes: 2 additions & 3 deletions app/services/ember-cli.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import Babel from "npm:babel-core";
import Path from 'npm:path';
import HtmlbarsInlinePrecompile from 'npm:babel-plugin-htmlbars-inline-precompile';
import HbsPlugin from '../plugins/hbs-plugin';
import blueprints from '../lib/blueprints';
import config from '../config/environment';
import Ember from 'ember';
import moment from 'moment';
import _template from "lodash/string/template";

const hbsPlugin = new HtmlbarsInlinePrecompile(Ember.HTMLBars.precompile);

const { computed, inject, RSVP, $ } = Ember;
const twiddleAppName = 'twiddle';
const oldTwiddleAppNames = ['demo-app', 'app'];
const hbsPlugin = new HbsPlugin(Babel);

// These files will be included if not present
const boilerPlateJs = [
Expand Down
2 changes: 1 addition & 1 deletion blueprints/twiddle.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.10.7",
"version": "0.11.0",
"EmberENV": {
"FEATURES": {}
},
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ember-twiddle",
"version": "0.10.7",
"version": "0.11.0",
"description": "https://ember-twiddle.com",
"private": true,
"directories": {
Expand Down Expand Up @@ -79,7 +79,6 @@
"torii": "0.8.0"
},
"dependencies": {
"babel-core": "^5.8.38",
"babel-plugin-htmlbars-inline-precompile": "0.1.0"
"babel-core": "^5.8.38"
}
}

0 comments on commit d0ec148

Please sign in to comment.