From 76e513835313eee0ed81b3006766b6fa4d4cd7d2 Mon Sep 17 00:00:00 2001 From: Topi Santakivi Date: Tue, 10 Apr 2018 13:25:02 +0300 Subject: [PATCH] feat: configuring Pug compiler from vue-jest configuration --- README.md | 16 ++++++++++++++++ lib/compilers/pug-compiler.js | 5 +++-- lib/process.js | 2 +- lib/template-compiler.js | 8 ++++---- test/pug.spec.js | 20 ++++++++++++++++++++ test/resources/PugBase.pug | 2 ++ test/resources/PugExtends.vue | 11 +++++++++++ 7 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 test/resources/PugBase.pug create mode 100644 test/resources/PugExtends.vue diff --git a/README.md b/README.md index 699950de..7305f6b5 100644 --- a/README.md +++ b/README.md @@ -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"`) diff --git a/lib/compilers/pug-compiler.js b/lib/compilers/pug-compiler.js index f8bbafd1..c49da54e 100644 --- a/lib/compilers/pug-compiler.js +++ b/lib/compilers/pug-compiler.js @@ -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) } diff --git a/lib/process.js b/lib/process.js index 1e564438..c9802d0c 100644 --- a/lib/process.js +++ b/lib/process.js @@ -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' diff --git a/lib/template-compiler.js b/lib/template-compiler.js index ef5242db..a711f028 100644 --- a/lib/template-compiler.js +++ b/lib/template-compiler.js @@ -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) @@ -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) { diff --git a/test/pug.spec.js b/test/pug.spec.js index f0a13c42..03c19957 100644 --- a/test/pug.spec.js +++ b/test/pug.spec.js @@ -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') +}) diff --git a/test/resources/PugBase.pug b/test/resources/PugBase.pug new file mode 100644 index 00000000..93fdad14 --- /dev/null +++ b/test/resources/PugBase.pug @@ -0,0 +1,2 @@ +div(class='pug-base') + block component \ No newline at end of file diff --git a/test/resources/PugExtends.vue b/test/resources/PugExtends.vue new file mode 100644 index 00000000..012118d9 --- /dev/null +++ b/test/resources/PugExtends.vue @@ -0,0 +1,11 @@ + + + \ No newline at end of file