Skip to content
This repository was archived by the owner on Dec 26, 2018. It is now read-only.

Commit 1a660df

Browse files
committed
extract tempalte as raw string when lang is present (fix #72)
1 parent b4c131d commit 1a660df

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

lib/compiler.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,13 @@ function isScoped (node) {
211211
*/
212212

213213
function processTemplate (node, filePath, id, hasScopedStyle, fullSource) {
214-
var template = checkSrc(node, filePath) || parse5.serialize(node.content)
215-
template = deindent(template)
216214
var lang = checkLang(node)
215+
var template = checkSrc(node, filePath) || (
216+
lang
217+
? getRawTemplate(node, fullSource) // custom template, extract as raw string
218+
: parse5.serialize(node.content) // normal HTML, use serialization
219+
)
220+
template = deindent(template)
217221
if (!lang) {
218222
var warnings = validateTemplate(node.content, fullSource)
219223
if (warnings) {
@@ -239,6 +243,25 @@ function processTemplate (node, filePath, id, hasScopedStyle, fullSource) {
239243
})
240244
}
241245

246+
/**
247+
* Extract the raw content of a template node.
248+
* This is more reliable because if the user uses a template language
249+
* that would confuse parse5 (e.g. ejs), the serialization result
250+
* would be different from original.
251+
*
252+
* @param {Node} node
253+
* @param {String} source
254+
*/
255+
256+
function getRawTemplate (node, source) {
257+
var content = node.content
258+
var l = content.childNodes.length
259+
if (!l) return ''
260+
var start = content.childNodes[0].__location.startOffset
261+
var end = content.childNodes[l - 1].__location.endOffset
262+
return source.slice(start, end)
263+
}
264+
242265
/**
243266
* Process a style node
244267
*
@@ -355,7 +378,7 @@ function compileAsPromise (type, source, lang, filePath) {
355378
// report babel error codeframe
356379
if (err.codeFrame) {
357380
process.nextTick(function () {
358-
console.error(err.codeFrame)
381+
console.error(err.codeFrame)
359382
})
360383
}
361384
return reject(err)
@@ -392,6 +415,10 @@ function extract (parts, type) {
392415
.join('\n')
393416
}
394417

418+
/**
419+
* Pad content into empty lines.
420+
*/
421+
395422
function padContent (content, lang) {
396423
return content
397424
.split(/\r?\n/g)

0 commit comments

Comments
 (0)