Skip to content

feat(compiler-sfc): [BREAKING] default <script> lang option for parser #7176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions packages/compiler-sfc/__tests__/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,60 @@ h1 { color: red }
)
})
})

describe('default script lang', () => {
test('default lang for script', () => {
const { descriptor, errors } = parse(
`<script>console.log(0x0f)</script>`,
{
defaultScriptLang: 'ts'
}
)
expect(errors.length).toBe(0)
expect(descriptor.script?.lang).toBe('ts')
})

test('default lang for script setup', () => {
const { descriptor, errors } = parse(
`<script setup>console.log(0x03)</script>`,
{
defaultScriptLang: 'ts'
}
)
expect(errors.length).toBe(0)
expect(descriptor.scriptSetup?.lang).toBe('ts')
})

test('default lang for script & script setup', () => {
const { descriptor, errors } = parse(
`
<script>console.log(0x06)</script>
<script setup>console.log(0x07)</script>
`,
{
defaultScriptLang: 'ts'
}
)
expect(errors.length).toBe(0)
expect(descriptor.script?.lang).toBe('ts')
expect(descriptor.scriptSetup?.lang).toBe('ts')
})

// This only tests if the "source to sfc" cache has "defaultScriptLang" as part of the cache key.
test('source cache key', () => {
// The same SFC as previous test
const { descriptor, errors } = parse(
`
<script>console.log(0x06)</script>
<script setup>console.log(0x07)</script>
`,
{
defaultScriptLang: 'tsx'
}
)
expect(errors.length).toBe(0)
expect(descriptor.script?.lang).toBe('tsx')
expect(descriptor.scriptSetup?.lang).toBe('tsx')
})
})
})
14 changes: 12 additions & 2 deletions packages/compiler-sfc/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface SFCParseOptions {
pad?: boolean | 'line' | 'space'
ignoreEmpty?: boolean
compiler?: TemplateCompiler
defaultScriptLang?: string
}

export interface SFCBlock {
Expand Down Expand Up @@ -95,11 +96,18 @@ export function parse(
sourceRoot = '',
pad = false,
ignoreEmpty = true,
compiler = CompilerDOM
compiler = CompilerDOM,
defaultScriptLang
}: SFCParseOptions = {}
): SFCParseResult {
const sourceKey =
source + sourceMap + filename + sourceRoot + pad + compiler.parse
source +
sourceMap +
filename +
sourceRoot +
pad +
compiler.parse +
(defaultScriptLang ? '@' + defaultScriptLang : '')
const cache = sourceToSFC.get(sourceKey)
if (cache) {
return cache
Expand Down Expand Up @@ -191,10 +199,12 @@ export function parse(
const scriptBlock = createBlock(node, source, pad) as SFCScriptBlock
const isSetup = !!scriptBlock.attrs.setup
if (isSetup && !descriptor.scriptSetup) {
if (!scriptBlock.lang) scriptBlock.lang = defaultScriptLang
descriptor.scriptSetup = scriptBlock
break
}
if (!isSetup && !descriptor.script) {
if (!scriptBlock.lang) scriptBlock.lang = defaultScriptLang
descriptor.script = scriptBlock
break
}
Expand Down