Skip to content

Commit b70a258

Browse files
committed
fix(compiler-sfc): use safer deindent default for compatibility with previous behavior
1 parent ca7daef commit b70a258

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

packages/compiler-sfc/src/parseComponent.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export const DEFAULT_FILENAME = 'anonymous.vue'
1010
const splitRE = /\r?\n/g
1111
const replaceRE = /./g
1212
const isSpecialTag = makeMap('script,style,template', true)
13-
const isNeedIndentLang = makeMap('pug,jade')
1413

1514
export interface SFCCustomBlock {
1615
type: string
@@ -179,9 +178,13 @@ export function parseComponent(
179178
currentBlock.end = start
180179
let text = source.slice(currentBlock.start, currentBlock.end)
181180
if (
182-
options.deindent ||
183-
// certain langs like pug are indent sensitive, preserve old behavior
184-
(currentBlock.lang && isNeedIndentLang(currentBlock.lang))
181+
options.deindent === true ||
182+
// by default, deindent unless it's script with default lang or ts
183+
(options.deindent !== false &&
184+
!(
185+
currentBlock.type === 'script' &&
186+
(!currentBlock.lang || currentBlock.lang === 'ts')
187+
))
185188
) {
186189
text = deindent(text)
187190
}

packages/compiler-sfc/test/parseComponent.spec.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ describe('Single File Component parser', () => {
2525
<div>
2626
<style>nested should be ignored</style>
2727
</div>
28-
`,
29-
{ deindent: true }
28+
`
3029
)
3130
expect(res.template!.content.trim()).toBe('<div>hi</div>')
3231
expect(res.styles.length).toBe(4)
@@ -76,8 +75,7 @@ describe('Single File Component parser', () => {
7675
</style>
7776
`
7877
const deindentDefault = parseComponent(content.trim(), {
79-
pad: false,
80-
deindent: true
78+
pad: false
8179
})
8280
const deindentEnabled = parseComponent(content.trim(), {
8381
pad: false,
@@ -89,7 +87,9 @@ describe('Single File Component parser', () => {
8987
})
9088

9189
expect(deindentDefault.template!.content).toBe('\n<div></div>\n')
92-
expect(deindentDefault.script!.content).toBe('\nexport default {}\n')
90+
expect(deindentDefault.script!.content).toBe(
91+
'\n export default {}\n '
92+
)
9393
expect(deindentDefault.styles[0].content).toBe('\nh1 { color: red }\n')
9494
expect(deindentEnabled.template!.content).toBe('\n<div></div>\n')
9595
expect(deindentEnabled.script!.content).toBe('\nexport default {}\n')
@@ -203,8 +203,7 @@ describe('Single File Component parser', () => {
203203
}
204204
</test>
205205
<custom src="./x.json"></custom>
206-
`,
207-
{ deindent: true }
206+
`
208207
)
209208
expect(res.customBlocks.length).toBe(4)
210209

0 commit comments

Comments
 (0)