import { parse } from '../src' import { mockWarn } from '@vue/shared' import { baseParse, baseCompile } from '@vue/compiler-core' import { SourceMapConsumer } from 'source-map' describe('compiler:sfc', () => { mockWarn() describe('source map', () => { test('style block', () => { // Padding determines how many blank lines will there be before the style block const padding = Math.round(Math.random() * 10) const style = parse( `${'\n'.repeat(padding)}\n` ).descriptor.styles[0] expect(style.map).not.toBeUndefined() const consumer = new SourceMapConsumer(style.map!) consumer.eachMapping(mapping => { expect(mapping.originalLine - mapping.generatedLine).toBe(padding) }) }) test('script block', () => { // Padding determines how many blank lines will there be before the style block const padding = Math.round(Math.random() * 10) const script = parse( `${'\n'.repeat(padding)}\n` ).descriptor.script expect(script!.map).not.toBeUndefined() const consumer = new SourceMapConsumer(script!.map!) consumer.eachMapping(mapping => { expect(mapping.originalLine - mapping.generatedLine).toBe(padding) }) }) }) test('pad content', () => { const content = ` ` const padFalse = parse(content.trim(), { pad: false }).descriptor expect(padFalse.template!.content).toBe('\n
\n') expect(padFalse.script!.content).toBe('\nexport default {}\n') expect(padFalse.styles[0].content).toBe('\nh1 { color: red }\n') const padTrue = parse(content.trim(), { pad: true }).descriptor expect(padTrue.script!.content).toBe( Array(3 + 1).join('//\n') + '\nexport default {}\n' ) expect(padTrue.styles[0].content).toBe( Array(6 + 1).join('\n') + '\nh1 { color: red }\n' ) const padLine = parse(content.trim(), { pad: 'line' }).descriptor expect(padLine.script!.content).toBe( Array(3 + 1).join('//\n') + '\nexport default {}\n' ) expect(padLine.styles[0].content).toBe( Array(6 + 1).join('\n') + '\nh1 { color: red }\n' ) const padSpace = parse(content.trim(), { pad: 'space' }).descriptor expect(padSpace.script!.content).toBe( `\n\n