diff --git a/packages/compiler-sfc/src/parseComponent.ts b/packages/compiler-sfc/src/parseComponent.ts index 39322a3efd4..65b858c9fc0 100644 --- a/packages/compiler-sfc/src/parseComponent.ts +++ b/packages/compiler-sfc/src/parseComponent.ts @@ -10,7 +10,6 @@ export const DEFAULT_FILENAME = 'anonymous.vue' const splitRE = /\r?\n/g const replaceRE = /./g const isSpecialTag = makeMap('script,style,template', true) -const isNeedIndentLang = makeMap('pug,jade') export interface SFCCustomBlock { type: string @@ -179,9 +178,13 @@ export function parseComponent( currentBlock.end = start let text = source.slice(currentBlock.start, currentBlock.end) if ( - options.deindent || - // certain langs like pug are indent sensitive, preserve old behavior - (currentBlock.lang && isNeedIndentLang(currentBlock.lang)) + options.deindent === true || + // by default, deindent unless it's script with default lang or ts + (options.deindent !== false && + !( + currentBlock.type === 'script' && + (!currentBlock.lang || currentBlock.lang === 'ts') + )) ) { text = deindent(text) } diff --git a/packages/compiler-sfc/test/parseComponent.spec.ts b/packages/compiler-sfc/test/parseComponent.spec.ts index 4624bb2040d..83f73483e0c 100644 --- a/packages/compiler-sfc/test/parseComponent.spec.ts +++ b/packages/compiler-sfc/test/parseComponent.spec.ts @@ -25,8 +25,7 @@ describe('Single File Component parser', () => {
- `, - { deindent: true } + ` ) expect(res.template!.content.trim()).toBe('
hi
') expect(res.styles.length).toBe(4) @@ -76,8 +75,7 @@ describe('Single File Component parser', () => { ` const deindentDefault = parseComponent(content.trim(), { - pad: false, - deindent: true + pad: false }) const deindentEnabled = parseComponent(content.trim(), { pad: false, @@ -89,7 +87,9 @@ describe('Single File Component parser', () => { }) expect(deindentDefault.template!.content).toBe('\n
\n') - expect(deindentDefault.script!.content).toBe('\nexport default {}\n') + expect(deindentDefault.script!.content).toBe( + '\n export default {}\n ' + ) expect(deindentDefault.styles[0].content).toBe('\nh1 { color: red }\n') expect(deindentEnabled.template!.content).toBe('\n
\n') expect(deindentEnabled.script!.content).toBe('\nexport default {}\n') @@ -203,8 +203,7 @@ describe('Single File Component parser', () => { } - `, - { deindent: true } + ` ) expect(res.customBlocks.length).toBe(4)