Skip to content

Commit 98cbf25

Browse files
committed
feat(pug): add support for pug template lang
1 parent f196304 commit 98cbf25

File tree

8 files changed

+268
-14
lines changed

8 files changed

+268
-14
lines changed

jest-vue.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function processScript (scriptPart) {
1919
module.exports = {
2020
process (src, path) {
2121
var parts = vueCompiler.parseComponent(src, { pad: true })
22-
const renderFunctions = compileTemplate(parts.template.content)
22+
const renderFunctions = compileTemplate(parts.template)
2323

2424
const result = processScript(parts.script)
2525

lib/compilers/pug-compiler.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var ensureRequire = require('../ensure-require.js')
2+
3+
module.exports = function (raw) {
4+
var html;
5+
ensureRequire('pug', 'pug')
6+
var jade = require('pug')
7+
try {
8+
var html = jade.compile(raw)()
9+
} catch (err) {
10+
throw Error(err)
11+
}
12+
return html
13+
}

lib/ensure-require.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module.exports = function (name, deps) {
2+
var i, len
3+
var missing = []
4+
if (typeof deps === 'string') {
5+
deps = [deps]
6+
}
7+
for (i = 0, len = deps.length; i < len; i++) {
8+
var mis
9+
var req = deps[i]
10+
if (typeof req === 'string') {
11+
mis = req
12+
} else {
13+
mis = req[1]
14+
req = req[0]
15+
}
16+
try {
17+
// hack for babel-runtime because it does not expose "main" field
18+
if (req === 'babel-runtime') {
19+
req = 'babel-runtime/core-js'
20+
}
21+
require.resolve(req)
22+
} catch (e) {
23+
missing.push(mis)
24+
}
25+
}
26+
if (missing.length > 0) {
27+
var message = 'You are trying to use "' + name + '". '
28+
var npmInstall = 'npm install --save-dev ' + missing.join(' ')
29+
if (missing.length > 1) {
30+
var last = missing.pop()
31+
message += missing.join(', ') + ' and ' + last + ' are '
32+
} else {
33+
message += missing[0] + ' is '
34+
}
35+
message += 'missing.\n\nTo install run:\n' + npmInstall
36+
throw new Error(message)
37+
}
38+
}

lib/template-compiler.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
var chalk = require('chalk')
22
var vueCompiler = require('vue-template-compiler')
33
var transpile = require('vue-template-es2015-compiler')
4+
var compilePug = require('./compilers/pug-compiler')
45

5-
module.exports = function compileTemplate (template, compiler) {
6-
var compiled = vueCompiler.compile(template)
6+
function getTemplateContent(templatePart) {
7+
if(templatePart.lang === 'pug') {
8+
return compilePug(templatePart.content)
9+
}
10+
return templatePart.content
11+
}
12+
13+
module.exports = function compileTemplate (templatePart, compiler) {
14+
var templateContent = getTemplateContent(templatePart)
15+
16+
var compiled = vueCompiler.compile(templateContent)
717
if (compiled.errors.length) {
818
compiled.errors.forEach(function (msg) {
919
console.error('\n' + chalk.red(msg) + '\n')

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
"eslint-plugin-vue": "^2.1.0",
2525
"eslint-plugin-vue-libs": "^1.2.0",
2626
"jest": "^20.0.4",
27+
"pug": "^2.0.0-rc.3",
2728
"vue": "^2.4.2",
2829
"vue-template-compiler": "^2.4.2",
29-
"vue-template-es2015-compiler": "^1.5.3"
30+
"vue-template-es2015-compiler": "^1.5.3",
31+
"vue-test-utils": "git+https://github.com/vuejs/vue-test-utils.git"
3032
},
3133
"peerDependencies": {
3234
"vue": "^2.x",

test/pug.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { shallow } from 'vue-test-utils'
2+
import Pug from './resources/Pug.vue'
3+
4+
test('processes .vue file with pug template', () => {
5+
const wrapper = shallow(Pug)
6+
expect(wrapper.is('div')).toBe(true)
7+
expect(wrapper.hasClass('pug')).toBe(true)
8+
})

test/resources/Pug.vue

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<template lang="pug">
2+
.pug
3+
</template>
4+
5+
<script>
6+
export default {
7+
name: 'pug'
8+
};
9+
</script>

0 commit comments

Comments
 (0)