forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlebarsPartials.js
More file actions
35 lines (31 loc) · 1.44 KB
/
Copy pathhandlebarsPartials.js
File metadata and controls
35 lines (31 loc) · 1.44 KB
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
function addPartialLeader (fullName) {
const refPieces = fullName.split('/')
refPieces[refPieces.length - 1] = `_${refPieces[refPieces.length - 1]}`
return refPieces.join('/')
}
module.exports = function (input) {
this.cacheable()
const partialsRegexp = /\{\{>(.+)( |})/g
// search for all things that look like partial references {{>partial}},
// replace them with {{> $jst/_partial}}
let newInput = input.replace(partialsRegexp, partialInvocation => {
const fixedInvocation = partialInvocation.replace(/([^\{\}> ]+) ?/, partialName => {
// replace the name of the partial with a reference webpack can resolve
const newPartialName = addPartialLeader(`$jst/${partialName}.handlebars`)
return newPartialName
})
return fixedInvocation
})
// search for all sub-partial references like {{>[assignments/partial],
// replace them with {{> $jst/assignments/_partial}}
const subPartialsRegexp = /\{\{> ?\[.+?\]/g
newInput = newInput.replace(subPartialsRegexp, partialInvocation => {
const absoluteReferencedPartial = partialInvocation.replace('[', ' $jst/').replace(']', '')
const fixedInvocation = addPartialLeader(absoluteReferencedPartial)
return fixedInvocation
})
return newInput
}