forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlebarsPartials.js
35 lines (31 loc) · 1.45 KB
/
handlebarsPartials.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// We specify partials with an arrow and expect to simply find them
// because out old build would register them manually in a rewrite.
// We should be able to find them by referencing them relative to the jst directory
var addPartialLeader = function(fullName){
var refPieces = fullName.split("/");
refPieces[refPieces.length - 1] = "_" + refPieces[refPieces.length - 1];
return refPieces.join("/");
}
module.exports = function(input){
this.cacheable();
var partialsRegexp = /\{\{>(.+)( |})/g;
// search for all things that look like partial references {{>partial}},
// replace them with {{> $jst/_partial}}
var newInput = input.replace(partialsRegexp, function(partialInvocation){
var fixedInvocation = partialInvocation.replace(/([^\{\}> ]+) ?/, function(partialName){
// replace the name of the partial with a reference webpack can resolve
var newPartialName = addPartialLeader("$jst/" + partialName);
return newPartialName;
});
return fixedInvocation
});
//search for all sub-partial references like {{>[assignments/partial],
// replace them with {{> $jst/assignments/_partial}}
var subPartialsRegexp = /\{\{> ?\[.+?\]/g;
newInput = newInput.replace(subPartialsRegexp, function(partialInvocation){
var absoluteReferencedPartial = partialInvocation.replace("[", " $jst/").replace("]", "");
var fixedInvocation = addPartialLeader(absoluteReferencedPartial)
return fixedInvocation;
});
return newInput;
}