|
|
@@ -253,19 +253,31 @@ export function parse(
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // dedent pug/jade templates
|
|
|
+ let templateColumnOffset = 0
|
|
|
+ if (
|
|
|
+ descriptor.template &&
|
|
|
+ (descriptor.template.lang === 'pug' || descriptor.template.lang === 'jade')
|
|
|
+ ) {
|
|
|
+ ;[descriptor.template.content, templateColumnOffset] = dedent(
|
|
|
+ descriptor.template.content
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
if (sourceMap) {
|
|
|
- const genMap = (block: SFCBlock | null) => {
|
|
|
+ const genMap = (block: SFCBlock | null, columnOffset = 0) => {
|
|
|
if (block && !block.src) {
|
|
|
block.map = generateSourceMap(
|
|
|
filename,
|
|
|
source,
|
|
|
block.content,
|
|
|
sourceRoot,
|
|
|
- !pad || block.type === 'template' ? block.loc.start.line - 1 : 0
|
|
|
+ !pad || block.type === 'template' ? block.loc.start.line - 1 : 0,
|
|
|
+ columnOffset
|
|
|
)
|
|
|
}
|
|
|
}
|
|
|
- genMap(descriptor.template)
|
|
|
+ genMap(descriptor.template, templateColumnOffset)
|
|
|
genMap(descriptor.script)
|
|
|
descriptor.styles.forEach(genMap)
|
|
|
descriptor.customBlocks.forEach(genMap)
|
|
|
@@ -369,7 +381,8 @@ function generateSourceMap(
|
|
|
source: string,
|
|
|
generated: string,
|
|
|
sourceRoot: string,
|
|
|
- lineOffset: number
|
|
|
+ lineOffset: number,
|
|
|
+ columnOffset: number
|
|
|
): RawSourceMap {
|
|
|
const map = new SourceMapGenerator({
|
|
|
file: filename.replace(/\\/g, '/'),
|
|
|
@@ -386,7 +399,7 @@ function generateSourceMap(
|
|
|
source: filename,
|
|
|
original: {
|
|
|
line: originalLine,
|
|
|
- column: i
|
|
|
+ column: i + columnOffset
|
|
|
},
|
|
|
generated: {
|
|
|
line: generatedLine,
|
|
|
@@ -466,3 +479,31 @@ export function hmrShouldReload(
|
|
|
|
|
|
return false
|
|
|
}
|
|
|
+
|
|
|
+/**
|
|
|
+ * Dedent a string.
|
|
|
+ *
|
|
|
+ * This removes any whitespace that is common to all lines in the string from
|
|
|
+ * each line in the string.
|
|
|
+ */
|
|
|
+function dedent(s: string): [string, number] {
|
|
|
+ const lines = s.split('\n')
|
|
|
+ const minIndent = lines.reduce(function (minIndent, line) {
|
|
|
+ if (line.trim() === '') {
|
|
|
+ return minIndent
|
|
|
+ }
|
|
|
+ const indent = line.match(/^\s*/)?.[0]?.length || 0
|
|
|
+ return Math.min(indent, minIndent)
|
|
|
+ }, Infinity)
|
|
|
+ if (minIndent === 0) {
|
|
|
+ return [s, minIndent]
|
|
|
+ }
|
|
|
+ return [
|
|
|
+ lines
|
|
|
+ .map(function (line) {
|
|
|
+ return line.slice(minIndent)
|
|
|
+ })
|
|
|
+ .join('\n'),
|
|
|
+ minIndent
|
|
|
+ ]
|
|
|
+}
|