Skip to content

Commit f9516a8

Browse files
sandst1eddyerburgh
authored andcommitted
feat: support for extending pug templates using relative paths (#83)
1 parent 0981e65 commit f9516a8

File tree

6 files changed

+30
-4
lines changed

6 files changed

+30
-4
lines changed

lib/compilers/pug-compiler.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
var ensureRequire = require('../ensure-require.js')
22
const throwError = require('../throw-error')
33

4-
module.exports = function (raw, config) {
4+
module.exports = function (templatePart, config) {
55
const options = (config && config['pug']) || {}
6+
if (templatePart.filename) {
7+
options.filename = templatePart.filename
8+
}
69
var html
710
ensureRequire('pug', 'pug')
811
var jade = require('pug')
912
try {
10-
html = jade.compile(raw, options)()
13+
html = jade.compile(templatePart.content, options)()
1114
} catch (err) {
1215
throwError(err)
1316
}

lib/process.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,10 @@ module.exports = function (src, filePath, jestConfig) {
6868
': defaultExport)\n'
6969

7070
if (parts.template) {
71+
parts.template.filename = filePath
7172
if (parts.template.src) {
72-
parts.template.content = fs.readFileSync(join(filePath, '..', parts.template.src), 'utf8')
73+
parts.template.filename = join(filePath, '..', parts.template.src)
74+
parts.template.content = fs.readFileSync(parts.template.filename, 'utf8')
7375
}
7476

7577
const renderFunctions = compileTemplate(parts.template, config)

lib/template-compiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const throwError = require('./throw-error')
88

99
function getTemplateContent (templatePart, config) {
1010
if (templatePart.lang === 'pug') {
11-
return compilePug(templatePart.content, config)
11+
return compilePug(templatePart, config)
1212
}
1313
if (templatePart.lang === 'jade') {
1414
return compileJade(templatePart.content)

test/pug.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,11 @@ test('supports global pug options and extends templates correctly from .pug file
2626
expect(compiled.code).toContain('pug-base')
2727
expect(compiled.code).toContain('pug-extended')
2828
})
29+
30+
test('supports relative paths when extending templates from .pug files', () => {
31+
const filePath = resolve(__dirname, './resources/PugRelativeExtends.vue')
32+
const fileString = readFileSync(filePath, { encoding: 'utf8' })
33+
const compiled = jestVue.process(fileString, filePath)
34+
expect(compiled.code).toContain('pug-relative-base')
35+
expect(compiled.code).toContain('pug-extended')
36+
})

test/relative/PugRelativeBase.pug

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
div(class='pug-relative-base')
2+
block component

test/resources/PugRelativeExtends.vue

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<template lang="pug">
2+
extends ../relative/PugRelativeBase.pug
3+
block component
4+
div(class="pug-extended")
5+
</template>
6+
7+
<script>
8+
export default {
9+
name: 'pug'
10+
}
11+
</script>

0 commit comments

Comments
 (0)