Skip to content

Commit 8243661

Browse files
committed
feat: vue-template-compiler support switch deindent
1 parent bd4819e commit 8243661

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/sfc/parser.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import deindent from 'de-indent'
44
import { parseHTML } from 'compiler/parser/html-parser'
5-
import { makeMap } from 'shared/util'
5+
import { makeMap, extend } from 'shared/util'
66

77
const splitRE = /\r?\n/g
88
const replaceRE = /./g
@@ -28,6 +28,9 @@ export function parseComponent (
2828
}
2929
let depth = 0
3030
let currentBlock: ?(SFCBlock | SFCCustomBlock) = null
31+
options = extend({
32+
deindent: true
33+
}, options)
3134

3235
function start (
3336
tag: string,
@@ -83,7 +86,10 @@ export function parseComponent (
8386
function end (tag: string, start: number, end: number) {
8487
if (depth === 1 && currentBlock) {
8588
currentBlock.end = start
86-
let text = deindent(content.slice(currentBlock.start, currentBlock.end))
89+
let text = content.slice(currentBlock.start, currentBlock.end)
90+
if (options.deindent) {
91+
text = deindent(text)
92+
}
8793
// pad content so that linters and pre-processors can output correct
8894
// line numbers in errors and warnings
8995
if (currentBlock.type !== 'template' && options.pad) {

test/unit/modules/sfc/sfc-parser.spec.js

+27
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,33 @@ describe('Single File Component parser', () => {
5555
expect(res.template.content.trim()).toBe('<div><template v-if="ok">hi</template></div>')
5656
})
5757

58+
it('deindent content', () => {
59+
const content = `
60+
<template>
61+
<div></div>
62+
</template>
63+
<script>
64+
export default {}
65+
</script>
66+
<style>
67+
h1 { color: red }
68+
</style>
69+
`
70+
const deindentDefault = parseComponent(content.trim(), { pad: false })
71+
const deindentEnabled = parseComponent(content.trim(), { pad: false, deindent: true })
72+
const deindentDisabled = parseComponent(content.trim(), { pad: false, deindent: false })
73+
74+
expect(deindentDefault.template.content).toBe('\n<div></div>\n')
75+
expect(deindentDefault.script.content).toBe('\nexport default {}\n')
76+
expect(deindentDefault.styles[0].content).toBe('\nh1 { color: red }\n')
77+
expect(deindentEnabled.template.content).toBe('\n<div></div>\n')
78+
expect(deindentEnabled.script.content).toBe('\nexport default {}\n')
79+
expect(deindentEnabled.styles[0].content).toBe('\nh1 { color: red }\n')
80+
expect(deindentDisabled.template.content).toBe('\n <div></div>\n ')
81+
expect(deindentDisabled.script.content).toBe('\n export default {}\n ')
82+
expect(deindentDisabled.styles[0].content).toBe('\n h1 { color: red }\n ')
83+
})
84+
5885
it('pad content', () => {
5986
const content = `
6087
<template>

0 commit comments

Comments
 (0)