Skip to content

Commit

Permalink
feat(magic-sfc): support magic sfc from magicstring (vue + tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tahul committed Mar 26, 2023
1 parent 707e3fb commit 9c94417
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/vue/sfc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { parse as vueParse } from 'vue/compiler-sfc'
import type { SFCBlock, SFCParseOptions, SFCParseResult, SFCScriptBlock, SFCStyleBlock, SFCTemplateBlock } from 'vue/compiler-sfc'
import type MagicString from 'magic-string'
import type { MagicBlock } from '../proxy'
import { proxyBlock } from '../proxy'
import type { MagicSFCOptions } from '../index'
Expand All @@ -18,7 +19,7 @@ export class MagicVueSFC<T extends MagicVueSFCOptions = MagicVueSFCOptions> exte
public styles: MagicBlock<SFCStyleBlock>[] = []

constructor(
source: string,
source: string | MagicString,
options?: T,
) {
super(source, options)
Expand Down
10 changes: 9 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SourceMap } from 'magic-string'
import MagicString, { SourceMap } from 'magic-string'
import { describe, expect, it } from 'vitest'
import { MagicSFC } from '../src'

Expand All @@ -9,6 +9,14 @@ describe('Magic SFC', () => {
expect(sfc.toString()).toBe('<script setup>let test: string</script>')
})

it('Can create the class from a MagicString', () => {
const ms = new MagicString('<script setup>let test: string</script>')

const sfc = new MagicSFC(ms)

expect(sfc.toString()).toBe('<script setup>let test: string</script>')
})

it('Can get a sourcemap', () => {
const sfc = new MagicSFC('<script setup>let test: string</script>')

Expand Down
14 changes: 13 additions & 1 deletion test/vue.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SourceMap } from 'magic-string'
import MagicString, { SourceMap } from 'magic-string'
import { describe, expect, it } from 'vitest'
import { MagicVueSFC } from '../src/vue/sfc'

Expand All @@ -22,6 +22,18 @@ describe('Magic Vue SFC', () => {
expect(sfc.toString()).toBe(scriptSetup)
})

it('Can create the class from a MagicString', () => {
const ms = new MagicString(scriptSetup)

const sfc = new MagicVueSFC(ms)

const appended = '\nlet secondTest: string'

sfc.scriptSetup.append(appended)

expect(sfc.toString()).toBe(`<script setup>let scriptSetup: string${appended}</script>`)
})

it('Can get a sourcemap', () => {
const sfc = new MagicVueSFC(scriptSetup)

Expand Down

0 comments on commit 9c94417

Please sign in to comment.