babel-plugin-htmlbars-inline-precompile
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -0,1 +1,5 @@ | ||
## 0.0.5 (2015-07-11) | ||
- [Allow single string parameter - #7](https://github.com/pangratz/babel-plugin-htmlbars-inline-precompile/commit/109b5f5c19e1c61654581b33e12f886b39e16412) | ||
## 0.0.4 (2015-06-30) | ||
@@ -2,0 +6,0 @@ |
43
index.js
@@ -5,2 +5,17 @@ module.exports = function(precompile) { | ||
var replaceNodeWithPrecompiledTemplate = function(node, template) { | ||
var compiledTemplateString = "Ember.HTMLBars.template(" + precompile(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', { | ||
@@ -31,2 +46,18 @@ ImportDeclaration: function(node, parent, scope, file) { | ||
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) { | ||
@@ -42,13 +73,3 @@ if (t.isIdentifier(node.tag, { name: file.importSpecifier })) { | ||
var compiledTemplateString = "Ember.HTMLBars.template(" + precompile(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 (this.replaceWithSourceString) { | ||
this.replaceWithSourceString(compiledTemplateString); | ||
} else { | ||
return compiledTemplateString; | ||
} | ||
return replaceNodeWithPrecompiledTemplate(this, template); | ||
} | ||
@@ -55,0 +76,0 @@ } |
{ | ||
"name": "babel-plugin-htmlbars-inline-precompile", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Babel plugin to replace tagged template strings with precompiled HTMLBars templates", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -43,2 +43,22 @@ # babel-plugin-htmlbars-inline-precompile [![Build Status](https://travis-ci.org/pangratz/babel-plugin-htmlbars-inline-precompile.svg?branch=master)](https://travis-ci.org/pangratz/babel-plugin-htmlbars-inline-precompile) | ||
If the template is compact, a normal string can be passed as argument as well: | ||
``` js | ||
import hbs from 'htmlbars-inline-precompile'; | ||
module("my view"); | ||
test("inline templates ftw", function(assert) { | ||
var view = Ember.View.create({ | ||
greeting: "inline template world", | ||
template: hbs('<h1>{{view.greeting}}</h1>') | ||
}); | ||
view.appendTo('#testing'); | ||
assert.equal(view.$().html().trim(), "<h1>inline template world</h1>"); | ||
}); | ||
``` | ||
## Usage | ||
@@ -45,0 +65,0 @@ |
@@ -61,2 +61,30 @@ var assert = require('assert'); | ||
}); | ||
describe('single string argument', function() { | ||
it("works with a plain string as parameter hbs('string')", function() { | ||
var transformed = transform("import hbs from 'htmlbars-inline-precompile'; var compiled = hbs('hello');", function(template) { | ||
return "precompiled(" + template + ")"; | ||
}); | ||
assert.equal(transformed, "var compiled = Ember.HTMLBars.template(precompiled(hello));", "tagged template is replaced"); | ||
}); | ||
it("warns when more than one argument is passed", function() { | ||
assert.throws(function() { | ||
transform("import hbs from 'htmlbars-inline-precompile'; var compiled = hbs('first', 'second');"); | ||
}, /hbs should be invoked with a single argument: the template string/); | ||
}); | ||
it("warns when argument is not a string", function() { | ||
assert.throws(function() { | ||
transform("import hbs from 'htmlbars-inline-precompile'; var compiled = hbs(123);"); | ||
}, /hbs should be invoked with a single argument: the template string/); | ||
}); | ||
it("warns when no argument is passed", function() { | ||
assert.throws(function() { | ||
transform("import hbs from 'htmlbars-inline-precompile'; var compiled = hbs();"); | ||
}, /hbs should be invoked with a single argument: the template string/); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10057
135
75