Skip to content

feat: configuring Pug compiler from vue-jest configuration #81

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 10, 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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ vue-jest compiles the script and template of SFCs into a JavaScript file that Je
### Supported template languages

- **pug** (`lang="pug"`)
- To give options for the Pug compiler, enter them into the Jest configuration.
The options will be passed to pug.compile().
```js
// package.json
{
"jest": {
"globals": {
"vue-jest": {
"pug": {
"basedir": "mybasedir"
}
}
}
}
}
```
- **jade** (`lang="jade"`)
- **haml** (`lang="haml"`)

Expand Down
5 changes: 3 additions & 2 deletions lib/compilers/pug-compiler.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
var ensureRequire = require('../ensure-require.js')
const throwError = require('../throw-error')

module.exports = function (raw) {
module.exports = function (raw, config) {
const options = (config && config['pug']) || {}
var html
ensureRequire('pug', 'pug')
var jade = require('pug')
try {
html = jade.compile(raw)()
html = jade.compile(raw, options)()
} catch (err) {
throwError(err)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ module.exports = function (src, filePath, jestConfig) {
parts.template.content = fs.readFileSync(join(filePath, '..', parts.template.src), 'utf8')
}

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

output += '__vue__options__.render = ' + renderFunctions.render + '\n' +
'__vue__options__.staticRenderFns = ' + renderFunctions.staticRenderFns + '\n'
Expand Down
8 changes: 4 additions & 4 deletions lib/template-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ var compileJade = require('./compilers/jade-compiler')
var compileHaml = require('./compilers/haml-compiler')
const throwError = require('./throw-error')

function getTemplateContent (templatePart) {
function getTemplateContent (templatePart, config) {
if (templatePart.lang === 'pug') {
return compilePug(templatePart.content)
return compilePug(templatePart.content, config)
}
if (templatePart.lang === 'jade') {
return compileJade(templatePart.content)
Expand All @@ -19,8 +19,8 @@ function getTemplateContent (templatePart) {
return templatePart.content
}

module.exports = function compileTemplate (templatePart) {
var templateContent = getTemplateContent(templatePart)
module.exports = function compileTemplate (templatePart, config) {
var templateContent = getTemplateContent(templatePart, config)

var compiled = vueCompiler.compile(templateContent)
if (compiled.errors.length) {
Expand Down
20 changes: 20 additions & 0 deletions test/pug.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
import { shallow } from 'vue-test-utils'
import { resolve } from 'path'
import Pug from './resources/Pug.vue'
import jestVue from '../vue-jest'
import { readFileSync } from 'fs'

test('processes .vue file with pug template', () => {
const wrapper = shallow(Pug)
expect(wrapper.is('div')).toBe(true)
expect(wrapper.hasClass('pug')).toBe(true)
})

test('supports global pug options and extends templates correctly from .pug files', () => {
const filePath = resolve(__dirname, './resources/PugExtends.vue')
const fileString = readFileSync(filePath, { encoding: 'utf8' })
const compiled = jestVue.process(fileString, filePath, {
globals: {
'vue-jest': {
pug: {
basedir: 'test'
}
}
}
})

expect(compiled.code).toContain('pug-base')
expect(compiled.code).toContain('pug-extended')
})
2 changes: 2 additions & 0 deletions test/resources/PugBase.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
div(class='pug-base')
block component
11 changes: 11 additions & 0 deletions test/resources/PugExtends.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<template lang="pug">
extends /resources/PugBase.pug
block component
div(class="pug-extended")
</template>

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