refactor(compiler-sfc): adjust sfc compileScript usage
This commit is contained in:
parent
03f924e48a
commit
11727b9e1e
@ -1,9 +1,10 @@
|
|||||||
import { parse, SFCScriptCompileOptions } from '../src'
|
import { parse, SFCScriptCompileOptions, compileScript } from '../src'
|
||||||
import { parse as babelParse } from '@babel/parser'
|
import { parse as babelParse } from '@babel/parser'
|
||||||
import { babelParserDefautPlugins } from '@vue/shared'
|
import { babelParserDefautPlugins } from '@vue/shared'
|
||||||
|
|
||||||
function compile(src: string, options?: SFCScriptCompileOptions) {
|
function compile(src: string, options?: SFCScriptCompileOptions) {
|
||||||
return parse(src, options).descriptor.scriptTransformed!
|
const { descriptor } = parse(src)
|
||||||
|
return compileScript(descriptor, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
function assertCode(code: string) {
|
function assertCode(code: string) {
|
||||||
@ -370,24 +371,23 @@ describe('SFC compile <script setup>', () => {
|
|||||||
|
|
||||||
describe('errors', () => {
|
describe('errors', () => {
|
||||||
test('<script> and <script setup> must have same lang', () => {
|
test('<script> and <script setup> must have same lang', () => {
|
||||||
expect(
|
expect(() =>
|
||||||
parse(`<script>foo()</script><script setup lang="ts">bar()</script>`)
|
compile(`<script>foo()</script><script setup lang="ts">bar()</script>`)
|
||||||
.errors[0].message
|
).toThrow(`<script> and <script setup> must have the same language type`)
|
||||||
).toMatch(`<script> and <script setup> must have the same language type`)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
test('export local as default', () => {
|
test('export local as default', () => {
|
||||||
expect(
|
expect(() =>
|
||||||
parse(`<script setup>
|
compile(`<script setup>
|
||||||
const bar = 1
|
const bar = 1
|
||||||
export { bar as default }
|
export { bar as default }
|
||||||
</script>`).errors[0].message
|
</script>`)
|
||||||
).toMatch(`Cannot export locally defined variable as default`)
|
).toThrow(`Cannot export locally defined variable as default`)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('export default referencing local var', () => {
|
test('export default referencing local var', () => {
|
||||||
expect(
|
expect(() =>
|
||||||
parse(`<script setup>
|
compile(`<script setup>
|
||||||
const bar = 1
|
const bar = 1
|
||||||
export default {
|
export default {
|
||||||
props: {
|
props: {
|
||||||
@ -396,19 +396,19 @@ describe('SFC compile <script setup>', () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>`).errors[0].message
|
</script>`)
|
||||||
).toMatch(`cannot reference locally declared variables`)
|
).toThrow(`cannot reference locally declared variables`)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('export default referencing exports', () => {
|
test('export default referencing exports', () => {
|
||||||
expect(
|
expect(() =>
|
||||||
parse(`<script setup>
|
compile(`<script setup>
|
||||||
export const bar = 1
|
export const bar = 1
|
||||||
export default {
|
export default {
|
||||||
props: bar
|
props: bar
|
||||||
}
|
}
|
||||||
</script>`).errors[0].message
|
</script>`)
|
||||||
).toMatch(`cannot reference locally declared variables`)
|
).toThrow(`cannot reference locally declared variables`)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('should allow export default referencing scope var', () => {
|
test('should allow export default referencing scope var', () => {
|
||||||
@ -458,19 +458,19 @@ describe('SFC compile <script setup>', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
test('error on duplicated default export', () => {
|
test('error on duplicated default export', () => {
|
||||||
expect(
|
expect(() =>
|
||||||
parse(`
|
compile(`
|
||||||
<script>
|
<script>
|
||||||
export default {}
|
export default {}
|
||||||
</script>
|
</script>
|
||||||
<script setup>
|
<script setup>
|
||||||
export default {}
|
export default {}
|
||||||
</script>
|
</script>
|
||||||
`).errors[0].message
|
`)
|
||||||
).toMatch(`Default export is already declared`)
|
).toThrow(`Default export is already declared`)
|
||||||
|
|
||||||
expect(
|
expect(() =>
|
||||||
parse(`
|
compile(`
|
||||||
<script>
|
<script>
|
||||||
export default {}
|
export default {}
|
||||||
</script>
|
</script>
|
||||||
@ -478,33 +478,33 @@ describe('SFC compile <script setup>', () => {
|
|||||||
const x = {}
|
const x = {}
|
||||||
export { x as default }
|
export { x as default }
|
||||||
</script>
|
</script>
|
||||||
`).errors[0].message
|
`)
|
||||||
).toMatch(`Default export is already declared`)
|
).toThrow(`Default export is already declared`)
|
||||||
|
|
||||||
expect(
|
expect(() =>
|
||||||
parse(`
|
compile(`
|
||||||
<script>
|
<script>
|
||||||
export default {}
|
export default {}
|
||||||
</script>
|
</script>
|
||||||
<script setup>
|
<script setup>
|
||||||
export { x as default } from './y'
|
export { x as default } from './y'
|
||||||
</script>
|
</script>
|
||||||
`).errors[0].message
|
`)
|
||||||
).toMatch(`Default export is already declared`)
|
).toThrow(`Default export is already declared`)
|
||||||
|
|
||||||
expect(
|
expect(() =>
|
||||||
parse(`
|
compile(`
|
||||||
<script>
|
<script>
|
||||||
export { x as default } from './y'
|
export { x as default } from './y'
|
||||||
</script>
|
</script>
|
||||||
<script setup>
|
<script setup>
|
||||||
export default {}
|
export default {}
|
||||||
</script>
|
</script>
|
||||||
`).errors[0].message
|
`)
|
||||||
).toMatch(`Default export is already declared`)
|
).toThrow(`Default export is already declared`)
|
||||||
|
|
||||||
expect(
|
expect(() =>
|
||||||
parse(`
|
compile(`
|
||||||
<script>
|
<script>
|
||||||
const x = {}
|
const x = {}
|
||||||
export { x as default }
|
export { x as default }
|
||||||
@ -512,8 +512,8 @@ describe('SFC compile <script setup>', () => {
|
|||||||
<script setup>
|
<script setup>
|
||||||
export default {}
|
export default {}
|
||||||
</script>
|
</script>
|
||||||
`).errors[0].message
|
`)
|
||||||
).toMatch(`Default export is already declared`)
|
).toThrow(`Default export is already declared`)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -9,9 +9,8 @@ import {
|
|||||||
import * as CompilerDOM from '@vue/compiler-dom'
|
import * as CompilerDOM from '@vue/compiler-dom'
|
||||||
import { RawSourceMap, SourceMapGenerator } from 'source-map'
|
import { RawSourceMap, SourceMapGenerator } from 'source-map'
|
||||||
import { TemplateCompiler } from './compileTemplate'
|
import { TemplateCompiler } from './compileTemplate'
|
||||||
import { compileScript, SFCScriptCompileOptions } from './compileScript'
|
|
||||||
|
|
||||||
export interface SFCParseOptions extends SFCScriptCompileOptions {
|
export interface SFCParseOptions {
|
||||||
filename?: string
|
filename?: string
|
||||||
sourceMap?: boolean
|
sourceMap?: boolean
|
||||||
sourceRoot?: string
|
sourceRoot?: string
|
||||||
@ -53,7 +52,6 @@ export interface SFCDescriptor {
|
|||||||
template: SFCTemplateBlock | null
|
template: SFCTemplateBlock | null
|
||||||
script: SFCScriptBlock | null
|
script: SFCScriptBlock | null
|
||||||
scriptSetup: SFCScriptBlock | null
|
scriptSetup: SFCScriptBlock | null
|
||||||
scriptTransformed: SFCScriptBlock | null
|
|
||||||
styles: SFCStyleBlock[]
|
styles: SFCStyleBlock[]
|
||||||
customBlocks: SFCBlock[]
|
customBlocks: SFCBlock[]
|
||||||
}
|
}
|
||||||
@ -79,8 +77,7 @@ export function parse(
|
|||||||
filename = 'component.vue',
|
filename = 'component.vue',
|
||||||
sourceRoot = '',
|
sourceRoot = '',
|
||||||
pad = false,
|
pad = false,
|
||||||
compiler = CompilerDOM,
|
compiler = CompilerDOM
|
||||||
babelParserPlugins
|
|
||||||
}: SFCParseOptions = {}
|
}: SFCParseOptions = {}
|
||||||
): SFCParseResult {
|
): SFCParseResult {
|
||||||
const sourceKey =
|
const sourceKey =
|
||||||
@ -96,7 +93,6 @@ export function parse(
|
|||||||
template: null,
|
template: null,
|
||||||
script: null,
|
script: null,
|
||||||
scriptSetup: null,
|
scriptSetup: null,
|
||||||
scriptTransformed: null,
|
|
||||||
styles: [],
|
styles: [],
|
||||||
customBlocks: []
|
customBlocks: []
|
||||||
}
|
}
|
||||||
@ -198,16 +194,6 @@ export function parse(
|
|||||||
descriptor.styles.forEach(genMap)
|
descriptor.styles.forEach(genMap)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (descriptor.script || descriptor.scriptSetup) {
|
|
||||||
try {
|
|
||||||
descriptor.scriptTransformed = compileScript(descriptor, {
|
|
||||||
babelParserPlugins
|
|
||||||
})
|
|
||||||
} catch (e) {
|
|
||||||
errors.push(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = {
|
const result = {
|
||||||
descriptor,
|
descriptor,
|
||||||
errors
|
errors
|
||||||
|
Loading…
x
Reference in New Issue
Block a user