diff --git a/index.js b/index.js index 4067aecc..a7e8d036 100644 --- a/index.js +++ b/index.js @@ -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) + ")"; diff --git a/tests/tests.js b/tests/tests.js index 6545e234..aaa3d922 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -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, "") + }; + } + })] }); });