Skip to content

Commit

Permalink
Merge pull request #15 from rwjblue/cache-keys
Browse files Browse the repository at this point in the history
Incorporate cache keys.
  • Loading branch information
rwjblue committed Aug 10, 2016
2 parents 67ba667 + 4f61342 commit fc98616
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,13 @@ test("inline templates ftw", function(assert) {
var HTMLBarsCompiler = require('./bower_components/ember/ember-template-compiler');
var HTMLBarsInlinePrecompile = require('babel-plugin-htmlbars-inline-precompile');

var pluginConfiguredWithCompiler = HTMLBarsInlinePrecompile(HTMLBarsCompiler.precompile);
var pluginConfiguredWithCompiler = HTMLBarsInlinePrecompile(HTMLBarsCompiler.precompile, {
cacheKey: checksumOfTemplateCompilerContents
});

require('babel').transform("code", {
plugins: [ pluginConfiguredWithCompiler ]
});
```

The provided `cacheKey` option is used to invalidate the broccoli-babel-transpiler cache.
22 changes: 20 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module.exports = function(precompile) {
return function(babel) {
module.exports = function(precompile, _options) {
var options = _options || {};

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

var replaceNodeWithPrecompiledTemplate = function(node, template) {
Expand Down Expand Up @@ -73,4 +75,20 @@ module.exports = function(precompile) {
}
});
}

// used by broccoli-babel-transpiler to use this packages
// files and deps as part of the cache key (when deps or
// implementation changes it will bust the cache for already
// transpiled files)
htmlbarsInlineCompilerPlugin.baseDir = function() {
return __dirname;
};

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

return htmlbarsInlineCompilerPlugin;
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "babel-plugin-htmlbars-inline-precompile",
"version": "0.0.5",
"version": "0.1.0",
"description": "Babel plugin to replace tagged template strings with precompiled HTMLBars templates",
"scripts": {
"test": "mocha tests"
Expand Down
15 changes: 15 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var assert = require('assert');
var path = require('path');

var babel = require('babel-core');
var HTMLBarsInlinePrecompile = require('../index');
Expand Down Expand Up @@ -59,6 +60,20 @@ describe("htmlbars-inline-precompile", function() {
}, /placeholders inside a tagged template string are not supported/);
});

describe('caching', function() {
it('passes through second argument as `cacheKey`', function() {
var plugin = HTMLBarsInlinePrecompile(function() {}, { cacheKey: 'asdfasdf' });

assert.equal(plugin.cacheKey(), 'asdfasdf');
});

it('include `baseDir` function for caching', function() {
var plugin = HTMLBarsInlinePrecompile(function() {}, 'asdfasdf');

assert.equal(plugin.baseDir(), path.resolve(__dirname, '..'));
});
});

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) {
Expand Down

0 comments on commit fc98616

Please sign in to comment.