File tree 2 files changed +35
-2
lines changed
2 files changed +35
-2
lines changed Original file line number Diff line number Diff line change 2
2
3
3
import deindent from 'de-indent'
4
4
import { parseHTML } from 'compiler/parser/html-parser'
5
- import { makeMap } from 'shared/util'
5
+ import { makeMap , extend } from 'shared/util'
6
6
7
7
const splitRE = / \r ? \n / g
8
8
const replaceRE = / ./ g
@@ -28,6 +28,9 @@ export function parseComponent (
28
28
}
29
29
let depth = 0
30
30
let currentBlock : ?( SFCBlock | SFCCustomBlock ) = null
31
+ options = extend ( {
32
+ deindent : true
33
+ } , options )
31
34
32
35
function start (
33
36
tag : string ,
@@ -83,7 +86,10 @@ export function parseComponent (
83
86
function end ( tag : string , start : number , end : number ) {
84
87
if ( depth === 1 && currentBlock ) {
85
88
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
+ }
87
93
// pad content so that linters and pre-processors can output correct
88
94
// line numbers in errors and warnings
89
95
if ( currentBlock . type !== 'template' && options . pad ) {
Original file line number Diff line number Diff line change @@ -55,6 +55,33 @@ describe('Single File Component parser', () => {
55
55
expect ( res . template . content . trim ( ) ) . toBe ( '<div><template v-if="ok">hi</template></div>' )
56
56
} )
57
57
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
+
58
85
it ( 'pad content' , ( ) => {
59
86
const content = `
60
87
<template>
You can’t perform that action at this time.
0 commit comments