Skip to content
This repository has been archived by the owner on Dec 8, 2024. It is now read-only.

Commit

Permalink
Add support for moduleName
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmit committed May 9, 2016
1 parent 67ba667 commit 9898662
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ module.exports = function(precompile) {
return function(babel) {
var t = babel.types;

var replaceNodeWithPrecompiledTemplate = function(node, template) {
var compiledTemplateString = "Ember.HTMLBars.template(" + precompile(template) + ")";
var replaceNodeWithPrecompiledTemplate = function(node, template, file) {
var moduleName = file.opts && file.opts.filenameRelative;

var compiledTemplateString = "Ember.HTMLBars.template(" + precompile(template, {
moduleName: moduleName
}) + ")";

// Prefer calling replaceWithSourceString if it is present.
// this prevents a deprecation warning in Babel 5.6.7+.
Expand Down Expand Up @@ -54,7 +58,7 @@ module.exports = function(precompile) {
throw file.errorWithNode(node, argumentErrorMsg);
}

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

Expand All @@ -68,7 +72,7 @@ module.exports = function(precompile) {
return quasi.value.cooked;
}).join("");

return replaceNodeWithPrecompiledTemplate(this, template);
return replaceNodeWithPrecompiledTemplate(this, template, file);
}
}
});
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"version": "0.0.5",
"description": "Babel plugin to replace tagged template strings with precompiled HTMLBars templates",
"scripts": {
"test": "mocha tests"
"test": "mocha tests",
"test:watch": "mocha -w tests"
},
"repository": "https://github.com/pangratz/babel-plugin-htmlbars-inline-precompile",
"author": "Clemens Müller <[email protected]>",
Expand Down
24 changes: 24 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,30 @@ describe("htmlbars-inline-precompile", function() {
assert.equal(transformed, "var compiled = Ember.HTMLBars.template(precompiled(hello));", "tagged template is replaced");
});

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("moduleName (filenameRelative) is passed through to precompiler", function() {
var code = "import hbs from 'htmlbars-inline-precompile'; var compiled = hbs('hello');";

var precompile = function(template, options) {
assert.equal(options.moduleName, 'module-name-test.js');

return "precompiled(" + template + ")";
};

babel.transform(code, {
blacklist: ['strict', 'es6.modules'],
plugins: [HTMLBarsInlinePrecompile(precompile)],
filenameRelative: 'module-name-test.js'
});
});

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');");
Expand Down

0 comments on commit 9898662

Please sign in to comment.