Skip to content

Commit

Permalink
Merge pull request #151 from andreyvolokitin/internal-partials-resolving
Browse files Browse the repository at this point in the history
Make loader aware of "internal" partials
  • Loading branch information
pcardune committed Jan 21, 2018
2 parents 67657e2 + f984f28 commit a8a9773
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 11 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Expand Up @@ -2,8 +2,8 @@

## [Unreleased]

### Added
- Your feature here!
### Fixed
- Fixed resolving of inline partials and partial blocks with failover content (#106, #135)

## [1.6.0] - 2017-09-01 ##

Expand Down
57 changes: 48 additions & 9 deletions index.js
Expand Up @@ -136,6 +136,26 @@ module.exports = function(source) {

hb.JavaScriptCompiler = MyJavaScriptCompiler;

// Define custom visitor for further template AST parsing
var Visitor = handlebars.Visitor;
function InternalBlocksVisitor() {
this.partialBlocks = [];
this.inlineBlocks = [];
}

InternalBlocksVisitor.prototype = new Visitor();
InternalBlocksVisitor.prototype.PartialBlockStatement = function(partial) {
this.partialBlocks.push(partial.name.original);
Visitor.prototype.PartialBlockStatement.call(this, partial);
};
InternalBlocksVisitor.prototype.DecoratorBlock = function(partial) {
if (partial.path.original === 'inline') {
this.inlineBlocks.push(partial.params[0].value);
}

Visitor.prototype.DecoratorBlock.call(this, partial);
};

// This is an async loader
var loaderAsyncCallback = this.async();

Expand Down Expand Up @@ -167,16 +187,23 @@ module.exports = function(source) {
// Precompile template
var template = '';

// AST holder for current template
var ast = null;

// Compile options
var opts = assign({
knownHelpersOnly: !firstCompile,
// TODO: Remove these in next major release
preventIndent: !!query.preventIndent,
compat: !!query.compat
}, precompileOptions, {
knownHelpers: knownHelpers,
});

try {
if (source) {
template = hb.precompile(source, assign({
knownHelpersOnly: !firstCompile,
// TODO: Remove these in next major release
preventIndent: !!query.preventIndent,
compat: !!query.compat
}, precompileOptions, {
knownHelpers: knownHelpers,
}));
ast = hb.parse(source, opts);
template = hb.precompile(ast, opts);
}
} catch (err) {
return loaderAsyncCallback(err);
Expand Down Expand Up @@ -286,7 +313,19 @@ module.exports = function(source) {
} else {
partialResolver(request, function(err, resolved){
if(err) {
return partialCallback(err);
var visitor = new InternalBlocksVisitor();

visitor.accept(ast);

if (
visitor.inlineBlocks.indexOf(request) !== -1 ||
visitor.partialBlocks.indexOf(request) !== -1
) {
return partialCallback();
} else {
return partialCallback(err);
}

}
foundPartials[partial] = resolved;
needRecompile = true;
Expand Down
14 changes: 14 additions & 0 deletions test/test.js
Expand Up @@ -542,4 +542,18 @@ describe('handlebars-loader', function () {
});
});

it('should use failover content of the partial block if it refers to non-existent partial', function (done) {
testTemplate(loader, './with-partial-block.handlebars', {}, function (err, output, require) {
assert.ok(output, 'generated output');
done();
});
});

it('should recognize and render inline partials', function (done) {
testTemplate(loader, './with-inline-partial.handlebars', {}, function (err, output, require) {
assert.ok(output, 'generated output');
done();
});
});

});
5 changes: 5 additions & 0 deletions test/with-inline-partial.handlebars
@@ -0,0 +1,5 @@
{{#*inline "printFoo"}}
Foo
{{/inline}}

{{> printFoo}}
3 changes: 3 additions & 0 deletions test/with-partial-block.handlebars
@@ -0,0 +1,3 @@
{{#> non-existent}}
<div>Failover</div>
{{/non-existent}}

0 comments on commit a8a9773

Please sign in to comment.