Skip to content

Commit 0981e65

Browse files
sandst1eddyerburgh
authored andcommitted
feat: configuring Pug compiler from vue-jest configuration (#81)
1 parent 6ff4cb5 commit 0981e65

File tree

7 files changed

+57
-7
lines changed

7 files changed

+57
-7
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,22 @@ vue-jest compiles the script and template of SFCs into a JavaScript file that Je
5757
### Supported template languages
5858

5959
- **pug** (`lang="pug"`)
60+
- To give options for the Pug compiler, enter them into the Jest configuration.
61+
The options will be passed to pug.compile().
62+
```js
63+
// package.json
64+
{
65+
"jest": {
66+
"globals": {
67+
"vue-jest": {
68+
"pug": {
69+
"basedir": "mybasedir"
70+
}
71+
}
72+
}
73+
}
74+
}
75+
```
6076
- **jade** (`lang="jade"`)
6177
- **haml** (`lang="haml"`)
6278

lib/compilers/pug-compiler.js

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

4-
module.exports = function (raw) {
4+
module.exports = function (raw, config) {
5+
const options = (config && config['pug']) || {}
56
var html
67
ensureRequire('pug', 'pug')
78
var jade = require('pug')
89
try {
9-
html = jade.compile(raw)()
10+
html = jade.compile(raw, options)()
1011
} catch (err) {
1112
throwError(err)
1213
}

lib/process.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ module.exports = function (src, filePath, jestConfig) {
7272
parts.template.content = fs.readFileSync(join(filePath, '..', parts.template.src), 'utf8')
7373
}
7474

75-
const renderFunctions = compileTemplate(parts.template)
75+
const renderFunctions = compileTemplate(parts.template, config)
7676

7777
output += '__vue__options__.render = ' + renderFunctions.render + '\n' +
7878
'__vue__options__.staticRenderFns = ' + renderFunctions.staticRenderFns + '\n'

lib/template-compiler.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ var compileJade = require('./compilers/jade-compiler')
66
var compileHaml = require('./compilers/haml-compiler')
77
const throwError = require('./throw-error')
88

9-
function getTemplateContent (templatePart) {
9+
function getTemplateContent (templatePart, config) {
1010
if (templatePart.lang === 'pug') {
11-
return compilePug(templatePart.content)
11+
return compilePug(templatePart.content, config)
1212
}
1313
if (templatePart.lang === 'jade') {
1414
return compileJade(templatePart.content)
@@ -19,8 +19,8 @@ function getTemplateContent (templatePart) {
1919
return templatePart.content
2020
}
2121

22-
module.exports = function compileTemplate (templatePart) {
23-
var templateContent = getTemplateContent(templatePart)
22+
module.exports = function compileTemplate (templatePart, config) {
23+
var templateContent = getTemplateContent(templatePart, config)
2424

2525
var compiled = vueCompiler.compile(templateContent)
2626
if (compiled.errors.length) {

test/pug.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
11
import { shallow } from 'vue-test-utils'
2+
import { resolve } from 'path'
23
import Pug from './resources/Pug.vue'
4+
import jestVue from '../vue-jest'
5+
import { readFileSync } from 'fs'
36

47
test('processes .vue file with pug template', () => {
58
const wrapper = shallow(Pug)
69
expect(wrapper.is('div')).toBe(true)
710
expect(wrapper.hasClass('pug')).toBe(true)
811
})
12+
13+
test('supports global pug options and extends templates correctly from .pug files', () => {
14+
const filePath = resolve(__dirname, './resources/PugExtends.vue')
15+
const fileString = readFileSync(filePath, { encoding: 'utf8' })
16+
const compiled = jestVue.process(fileString, filePath, {
17+
globals: {
18+
'vue-jest': {
19+
pug: {
20+
basedir: 'test'
21+
}
22+
}
23+
}
24+
})
25+
26+
expect(compiled.code).toContain('pug-base')
27+
expect(compiled.code).toContain('pug-extended')
28+
})

test/resources/PugBase.pug

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

test/resources/PugExtends.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 /resources/PugBase.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)