Skip to content

feat: Support for extending pug templates using relative paths #83

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

Merged
merged 1 commit into from
Apr 11, 2018
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
7 changes: 5 additions & 2 deletions lib/compilers/pug-compiler.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
var ensureRequire = require('../ensure-require.js')
const throwError = require('../throw-error')

module.exports = function (raw, config) {
module.exports = function (templatePart, config) {
const options = (config && config['pug']) || {}
if (templatePart.filename) {
options.filename = templatePart.filename
}
var html
ensureRequire('pug', 'pug')
var jade = require('pug')
try {
html = jade.compile(raw, options)()
html = jade.compile(templatePart.content, options)()
} catch (err) {
throwError(err)
}
Expand Down
4 changes: 3 additions & 1 deletion lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ module.exports = function (src, filePath, jestConfig) {
': defaultExport)\n'

if (parts.template) {
parts.template.filename = filePath
if (parts.template.src) {
parts.template.content = fs.readFileSync(join(filePath, '..', parts.template.src), 'utf8')
parts.template.filename = join(filePath, '..', parts.template.src)
parts.template.content = fs.readFileSync(parts.template.filename, 'utf8')
}

const renderFunctions = compileTemplate(parts.template, config)
Expand Down
2 changes: 1 addition & 1 deletion lib/template-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const throwError = require('./throw-error')

function getTemplateContent (templatePart, config) {
if (templatePart.lang === 'pug') {
return compilePug(templatePart.content, config)
return compilePug(templatePart, config)
}
if (templatePart.lang === 'jade') {
return compileJade(templatePart.content)
Expand Down
8 changes: 8 additions & 0 deletions test/pug.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,11 @@ test('supports global pug options and extends templates correctly from .pug file
expect(compiled.code).toContain('pug-base')
expect(compiled.code).toContain('pug-extended')
})

test('supports relative paths when extending templates from .pug files', () => {
const filePath = resolve(__dirname, './resources/PugRelativeExtends.vue')
const fileString = readFileSync(filePath, { encoding: 'utf8' })
const compiled = jestVue.process(fileString, filePath)
expect(compiled.code).toContain('pug-relative-base')
expect(compiled.code).toContain('pug-extended')
})
2 changes: 2 additions & 0 deletions test/relative/PugRelativeBase.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
div(class='pug-relative-base')
block component
11 changes: 11 additions & 0 deletions test/resources/PugRelativeExtends.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<template lang="pug">
extends ../relative/PugRelativeBase.pug
block component
div(class="pug-extended")
</template>

<script>
export default {
name: 'pug'
}
</script>