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

Commit

Permalink
Implement example of precompileOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmit committed May 12, 2016
1 parent c2ea0c5 commit 467c4fc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
module.exports = function(precompile) {
module.exports = function(precompile, pluginOptions) {
return function(babel) {
var t = babel.types;

var replaceNodeWithPrecompiledTemplate = function(node, template, file) {
var moduleName = file.opts && file.opts.filenameRelative;
var opts = {};

if (moduleName && moduleName !== 'unknown') {
opts.moduleName = moduleName;
if (pluginOptions && typeof pluginOptions.precompileOptions === 'function') {
var ret = pluginOptions.precompileOptions(file.opts);
if (typeof ret === 'object') {
opts = ret;
}
}

var compiledTemplateString = "Ember.HTMLBars.template(" + precompile(template, opts) + ")";
Expand Down
22 changes: 17 additions & 5 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,30 @@ describe("htmlbars-inline-precompile", function() {
}, /placeholders inside a tagged template string are not supported/);
});

it("moduleName is supplies to the precompiler", function() {
var fullpath = path.join(__dirname, 'fixtures', 'hello-world.js');
it("precompileOptions can return options to the precompiler", function() {
var sourceRoot = path.join(__dirname, 'fixtures');
var filename = 'hello-world.js';
var fullpath = path.join(sourceRoot, filename);

var precompile = function(template, options) {
assert.equal(options.moduleName, fullpath);
assert.equal(options.moduleName, filename);

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

babel.transformFileSync(fullpath, {
var transpiled = babel.transformFileSync(fullpath, {
blacklist: ['strict', 'es6.modules'],
plugins: [HTMLBarsInlinePrecompile(precompile)]
sourceRoot: sourceRoot,
plugins: [HTMLBarsInlinePrecompile(precompile, {
precompileOptions(opts) {
// example of how someone might use it to construct their own moduleName
var sourceRootRegEx = new RegExp("^" + opts.sourceRoot + "/?");

return {
moduleName: opts.filenameRelative.replace(sourceRootRegEx, "")
};
}
})]
});
});

Expand Down

0 comments on commit 467c4fc

Please sign in to comment.