Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit 2a1f18b

Browse files
committed
Add support for pug & jade
Fix #43
1 parent 084c874 commit 2a1f18b

File tree

11 files changed

+227
-27
lines changed

11 files changed

+227
-27
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"postcss-modules": "^0.6.4",
4848
"posthtml": "^0.9.2",
4949
"posthtml-attrs-parser": "^0.1.1",
50+
"pug": "^2.0.0-beta10",
5051
"rollup-pluginutils": "^2.0.1",
5152
"vue-template-compiler": "^2.1.10",
5253
"vue-template-es2015-compiler": "^1.5.0",

src/options.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@ export default {
4040
modules: {
4141
generateScopedName: '[name]__[local]___[hash:base64:5]'
4242
},
43-
scss: {}
43+
scss: {},
44+
pug: {}
4445
}

src/style/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import compileCSS from './css'
33
import compileSCSS from './scss'
44

55
const compilers = {
6-
scss: compileSCSS
6+
scss: compileSCSS,
7+
sass: compileSCSS
78
}
89

910
export async function compile (style, options) {

src/template/index.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import compileHTML from './html'
2+
import compilePug from './pug'
23

34
const compilers = {
4-
html: compileHTML
5+
html: compileHTML,
6+
pug: compilePug,
7+
jade: compilePug
58
}
69

710
export default async function (template, extras, options) {
8-
const lang = template.lang || 'html'
9-
10-
return await compilers[lang].call(null, template, extras, options)
11+
return await compilers[extras.lang || 'html'].call(null, template, extras, options)
1112
}

src/template/pug.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import pug from 'pug'
2+
3+
export default async function (template, extras, options) {
4+
const compiler = pug.compile(template, { filename: extras.id, ...options.pug })
5+
6+
return compiler({css: extras.modules || {}})
7+
}

src/vueTransform.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import deIndent from 'de-indent'
22
import htmlMinifier from 'html-minifier'
33
import parse5 from 'parse5'
4-
import validateTemplate from 'vue-template-validator'
4+
import templateValidator from 'vue-template-validator'
55
import transpileVueTemplate from 'vue-template-es2015-compiler'
66
import { compile } from './style/index'
77
import templateProcessor from './template/index'
@@ -109,27 +109,31 @@ function injectTemplate (script, template, lang, options, modules) {
109109
throw new Error('[rollup-plugin-vue] could not find place to inject template in script.')
110110
}
111111

112+
var validateTemplate = function (code, content, id) {
113+
const warnings = templateValidator(code, content)
114+
if (warnings) {
115+
const relativePath = relative(process.cwd(), id)
116+
warnings.forEach((msg) => {
117+
console.warn(`\n Warning in ${relativePath}:\n ${msg}`)
118+
})
119+
}
120+
}
112121
/**
113122
* Compile template: DeIndent and minify html.
114123
*/
115124
async function processTemplate (source, id, content, options, nodes, modules) {
116125
if (source === undefined) return undefined
117126

127+
const extras = { modules, id, lang: source.attrs.lang }
118128
const { code } = source
119129
const template = deIndent(
120130
await (options.disableCssModuleStaticReplacement !== true
121-
? templateProcessor(code, { modules }, options)
131+
? templateProcessor(code, extras, options)
122132
: code)
123133
)
124134

125135
if (!options.compileTemplate) {
126-
const warnings = validateTemplate(code, content)
127-
if (warnings) {
128-
const relativePath = relative(process.cwd(), id)
129-
warnings.forEach((msg) => {
130-
console.warn(`\n Warning in ${relativePath}:\n ${msg}`)
131-
})
132-
}
136+
validateTemplate(code, content, id)
133137
}
134138

135139
return htmlMinifier.minify(template, options.htmlMinifier)

test/expects/pug.css

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.pug__test {
2+
color: red; }

test/expects/pug.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var pug = { template: "<div class=\"pug__test keep-me\">foo</div>",cssModules: {"test":"pug__test"},};
2+
3+
export default pug;

test/fixtures/pug.vue

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<template lang="pug">
2+
div(class=css.test class='keep-me') foo
3+
</template>
4+
5+
<script lang="babel">
6+
export default {}
7+
</script>
8+
9+
10+
<style lang="scss" module>
11+
$var: red;
12+
13+
.test {
14+
color: $var;
15+
}
16+
</style>

test/test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function test(name) {
1919
var expected = read('expects/' + name + '.js').replace(/\r/g, '')
2020
var actualCss
2121
var cssHandler = function (css, styles) {
22-
if (['scss'].indexOf(name) > -1) {
22+
if (['scss', 'pug'].indexOf(name) > -1) {
2323
actualCss = styles[0].$compiled.code
2424
} else {
2525
actualCss = css
@@ -41,7 +41,7 @@ function test(name) {
4141
assert.equal(code.trim(), expected.trim(), 'should compile code correctly')
4242

4343
// Check css output
44-
if (['style', 'css-modules', 'css-modules-static', 'scss'].indexOf(name) > -1) {
44+
if (['style', 'css-modules', 'css-modules-static', 'scss', 'pug'].indexOf(name) > -1) {
4545
var css = read('expects/' + name + '.css')
4646
assert.equal(css.trim(), actualCss.trim(), 'should output style tag content')
4747
} else {

0 commit comments

Comments
 (0)