Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorporate cache keys. #15

Merged
merged 2 commits into from
Aug 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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