diff --git a/packages/compiler-sfc/__tests__/compileScript.spec.ts b/packages/compiler-sfc/__tests__/compileScript.spec.ts
index 178c5922..90dc31bd 100644
--- a/packages/compiler-sfc/__tests__/compileScript.spec.ts
+++ b/packages/compiler-sfc/__tests__/compileScript.spec.ts
@@ -1,10 +1,9 @@
-import { parse, compileScriptSetup, SFCScriptCompileOptions } from '../src'
+import { parse, SFCScriptCompileOptions } from '../src'
import { parse as babelParse } from '@babel/parser'
import { babelParserDefautPlugins } from '@vue/shared'
function compile(src: string, options?: SFCScriptCompileOptions) {
- const { descriptor } = parse(src)
- return compileScriptSetup(descriptor, options)
+ return parse(src, options).descriptor.scriptTransformed!
}
function assertCode(code: string) {
@@ -23,17 +22,19 @@ function assertCode(code: string) {
describe('SFC compile `).code)
+ assertCode(
+ compile(``).content
+ )
})
test('explicit setup signature', () => {
assertCode(
- compile(``).code
+ compile(``).content
)
})
test('import dedupe between
@@ -41,30 +42,30 @@ describe('SFC compile
- `).code
- assertCode(code)
- expect(code.indexOf(`import { x }`)).toEqual(
- code.lastIndexOf(`import { x }`)
+ `)
+ assertCode(content)
+ expect(content.indexOf(`import { x }`)).toEqual(
+ content.lastIndexOf(`import { x }`)
)
})
describe('exports', () => {
test('export const x = ...', () => {
- const { code, bindings } = compile(
+ const { content, bindings } = compile(
``
)
- assertCode(code)
+ assertCode(content)
expect(bindings).toStrictEqual({
x: 'setup'
})
})
test('export const { x } = ... (destructuring)', () => {
- const { code, bindings } = compile(``)
- assertCode(code)
+ assertCode(content)
expect(bindings).toStrictEqual({
a: 'setup',
b: 'setup',
@@ -76,34 +77,34 @@ describe('SFC compile `
)
- assertCode(code)
+ assertCode(content)
expect(bindings).toStrictEqual({
x: 'setup'
})
})
test('export class X() {}', () => {
- const { code, bindings } = compile(
+ const { content, bindings } = compile(
``
)
- assertCode(code)
+ assertCode(content)
expect(bindings).toStrictEqual({
X: 'setup'
})
})
test('export { x }', () => {
- const { code, bindings } = compile(
+ const { content, bindings } = compile(
``
)
- assertCode(code)
+ assertCode(content)
expect(bindings).toStrictEqual({
x: 'setup',
y: 'setup'
@@ -111,12 +112,12 @@ describe('SFC compile `
)
- assertCode(code)
+ assertCode(content)
expect(bindings).toStrictEqual({
x: 'setup',
y: 'setup'
@@ -124,52 +125,52 @@ describe('SFC compile `,
{
- parserPlugins: ['exportDefaultFrom']
+ babelParserPlugins: ['exportDefaultFrom']
}
)
- assertCode(code)
+ assertCode(content)
expect(bindings).toStrictEqual({})
})
test(`export { x as default }`, () => {
- const { code, bindings } = compile(
+ const { content, bindings } = compile(
``
)
- assertCode(code)
+ assertCode(content)
expect(bindings).toStrictEqual({
y: 'setup'
})
})
test(`export { x as default } from './x'`, () => {
- const { code, bindings } = compile(
+ const { content, bindings } = compile(
``
)
- assertCode(code)
+ assertCode(content)
expect(bindings).toStrictEqual({
y: 'setup'
})
})
test(`export * from './x'`, () => {
- const { code, bindings } = compile(
+ const { content, bindings } = compile(
``
)
- assertCode(code)
+ assertCode(content)
expect(bindings).toStrictEqual({
y: 'setup'
// in this case we cannot extract bindings from ./x so it falls back
@@ -178,7 +179,7 @@ describe('SFC compile `
)
- assertCode(code)
+ assertCode(content)
expect(bindings).toStrictEqual({
y: 'setup'
})
@@ -195,18 +196,18 @@ describe('SFC compile `)
- assertCode(code)
+ assertCode(content)
expect(bindings).toStrictEqual({ a: 'setup' })
})
test('extract props', () => {
- const { code } = compile(`
+ const { content } = compile(`
`)
- assertCode(code)
- expect(code).toMatch(`string: { type: String, required: true }`)
- expect(code).toMatch(`number: { type: Number, required: true }`)
- expect(code).toMatch(`boolean: { type: Boolean, required: true }`)
- expect(code).toMatch(`object: { type: Object, required: true }`)
- expect(code).toMatch(`objectLiteral: { type: Object, required: true }`)
- expect(code).toMatch(`fn: { type: Function, required: true }`)
- expect(code).toMatch(`functionRef: { type: Function, required: true }`)
- expect(code).toMatch(`objectRef: { type: Object, required: true }`)
- expect(code).toMatch(`array: { type: Array, required: true }`)
- expect(code).toMatch(`arrayRef: { type: Array, required: true }`)
- expect(code).toMatch(`tuple: { type: Array, required: true }`)
- expect(code).toMatch(`set: { type: Set, required: true }`)
- expect(code).toMatch(`literal: { type: String, required: true }`)
- expect(code).toMatch(`optional: { type: null, required: false }`)
- expect(code).toMatch(`recordRef: { type: Object, required: true }`)
- expect(code).toMatch(`interface: { type: Object, required: true }`)
- expect(code).toMatch(`alias: { type: Array, required: true }`)
- expect(code).toMatch(`union: { type: [String, Number], required: true }`)
- expect(code).toMatch(
+ assertCode(content)
+ expect(content).toMatch(`string: { type: String, required: true }`)
+ expect(content).toMatch(`number: { type: Number, required: true }`)
+ expect(content).toMatch(`boolean: { type: Boolean, required: true }`)
+ expect(content).toMatch(`object: { type: Object, required: true }`)
+ expect(content).toMatch(`objectLiteral: { type: Object, required: true }`)
+ expect(content).toMatch(`fn: { type: Function, required: true }`)
+ expect(content).toMatch(`functionRef: { type: Function, required: true }`)
+ expect(content).toMatch(`objectRef: { type: Object, required: true }`)
+ expect(content).toMatch(`array: { type: Array, required: true }`)
+ expect(content).toMatch(`arrayRef: { type: Array, required: true }`)
+ expect(content).toMatch(`tuple: { type: Array, required: true }`)
+ expect(content).toMatch(`set: { type: Set, required: true }`)
+ expect(content).toMatch(`literal: { type: String, required: true }`)
+ expect(content).toMatch(`optional: { type: null, required: false }`)
+ expect(content).toMatch(`recordRef: { type: Object, required: true }`)
+ expect(content).toMatch(`interface: { type: Object, required: true }`)
+ expect(content).toMatch(`alias: { type: Array, required: true }`)
+ expect(content).toMatch(
+ `union: { type: [String, Number], required: true }`
+ )
+ expect(content).toMatch(
`literalUnion: { type: [String, String], required: true }`
)
- expect(code).toMatch(
+ expect(content).toMatch(
`literalUnionMixed: { type: [String, Number, Boolean], required: true }`
)
- expect(code).toMatch(`intersection: { type: Object, required: true }`)
+ expect(content).toMatch(`intersection: { type: Object, required: true }`)
})
test('extract emits', () => {
- const { code } = compile(`
+ const { content } = compile(`
`)
- assertCode(code)
- expect(code).toMatch(`declare function __emit__(e: 'foo' | 'bar'): void`)
- expect(code).toMatch(
+ assertCode(content)
+ expect(content).toMatch(
+ `declare function __emit__(e: 'foo' | 'bar'): void`
+ )
+ expect(content).toMatch(
`declare function __emit__(e: 'baz', id: number): void`
)
- expect(code).toMatch(
+ expect(content).toMatch(
`emits: ["foo", "bar", "baz"] as unknown as undefined`
)
})
})
describe('errors', () => {
- test('must have `)).toThrow(
- `SFC has no `)
@@ -342,7 +341,7 @@ describe('SFC compile `).code
+ `).content
)
})
@@ -358,7 +357,7 @@ describe('SFC compile `).code
+ `).content
)
})
@@ -373,7 +372,7 @@ describe('SFC compile `).code
+ `).content
)
})
diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts
index 42ee651c..2f8fbe40 100644
--- a/packages/compiler-sfc/src/compileScript.ts
+++ b/packages/compiler-sfc/src/compileScript.ts
@@ -1,4 +1,4 @@
-import MagicString, { SourceMap } from 'magic-string'
+import MagicString from 'magic-string'
import { SFCDescriptor, SFCScriptBlock } from './parse'
import { parse, ParserPlugin } from '@babel/parser'
import { babelParserDefautPlugins, generateCodeFrame } from '@vue/shared'
@@ -17,15 +17,16 @@ import {
TSDeclareFunction
} from '@babel/types'
import { walk } from 'estree-walker'
+import { RawSourceMap } from 'source-map'
+
+export interface SFCScriptCompileOptions {
+ babelParserPlugins?: ParserPlugin[]
+}
export interface BindingMetadata {
[key: string]: 'data' | 'props' | 'setup' | 'ctx'
}
-export interface SFCScriptCompileOptions {
- parserPlugins?: ParserPlugin[]
-}
-
let hasWarned = false
/**
@@ -33,10 +34,10 @@ let hasWarned = false
* It requires the whole SFC descriptor because we need to handle and merge
* normal `