Skip to content

Commit 5b2bd1d

Browse files
authoredDec 8, 2023
feat(compiler-sfc): support import attributes and using syntax (#8786)
1 parent 0dc875d commit 5b2bd1d

File tree

5 files changed

+81
-4
lines changed

5 files changed

+81
-4
lines changed
 

‎packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap

+28
Original file line numberDiff line numberDiff line change
@@ -1483,3 +1483,31 @@ _sfc_.setup = __setup__
14831483
: __injectCSSVars__
14841484
"
14851485
`;
1486+
1487+
exports[`SFC genDefaultAs > parser plugins > import attributes (user override for deprecated syntax) 1`] = `
1488+
"import { foo } from './foo.js' assert { type: 'foobar' }
1489+
1490+
export default {
1491+
setup(__props, { expose: __expose }) {
1492+
__expose();
1493+
1494+
1495+
return { get foo() { return foo } }
1496+
}
1497+
1498+
}"
1499+
`;
1500+
1501+
exports[`SFC genDefaultAs > parser plugins > import attributes 1`] = `
1502+
"import { foo } from './foo.js' with { type: 'foobar' }
1503+
1504+
export default {
1505+
setup(__props, { expose: __expose }) {
1506+
__expose();
1507+
1508+
1509+
return { get foo() { return foo } }
1510+
}
1511+
1512+
}"
1513+
`;

‎packages/compiler-sfc/__tests__/compileScript.spec.ts

+34
Original file line numberDiff line numberDiff line change
@@ -1600,4 +1600,38 @@ describe('SFC genDefaultAs', () => {
16001600
foo: BindingTypes.SETUP_REF
16011601
})
16021602
})
1603+
1604+
describe('parser plugins', () => {
1605+
test('import attributes', () => {
1606+
const { content } = compile(`
1607+
<script setup>
1608+
import { foo } from './foo.js' with { type: 'foobar' }
1609+
</script>
1610+
`)
1611+
assertCode(content)
1612+
1613+
expect(() =>
1614+
compile(`
1615+
<script setup>
1616+
import { foo } from './foo.js' assert { type: 'foobar' }
1617+
</script>`)
1618+
).toThrow()
1619+
})
1620+
1621+
test('import attributes (user override for deprecated syntax)', () => {
1622+
const { content } = compile(
1623+
`
1624+
<script setup>
1625+
import { foo } from './foo.js' assert { type: 'foobar' }
1626+
</script>
1627+
`,
1628+
{
1629+
babelParserPlugins: [
1630+
['importAttributes', { deprecatedAssertSyntax: true }]
1631+
]
1632+
}
1633+
)
1634+
assertCode(content)
1635+
})
1636+
})
16031637
})

‎packages/compiler-sfc/__tests__/utils.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ export function assertCode(code: string) {
2828
try {
2929
babelParse(code, {
3030
sourceType: 'module',
31-
plugins: ['typescript']
31+
plugins: [
32+
'typescript',
33+
['importAttributes', { deprecatedAssertSyntax: true }]
34+
]
3235
})
3336
} catch (e: any) {
3437
console.log(code)

‎packages/compiler-sfc/src/rewriteDefault.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { parse } from '@babel/parser'
22
import MagicString from 'magic-string'
33
import type { ParserPlugin } from '@babel/parser'
44
import type { Identifier, Statement } from '@babel/types'
5+
import { resolveParserPlugins } from './script/context'
56

67
export function rewriteDefault(
78
input: string,
@@ -10,7 +11,7 @@ export function rewriteDefault(
1011
): string {
1112
const ast = parse(input, {
1213
sourceType: 'module',
13-
plugins: parserPlugins
14+
plugins: resolveParserPlugins('js', parserPlugins)
1415
}).program.body
1516
const s = new MagicString(input)
1617

‎packages/compiler-sfc/src/script/context.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { CallExpression, Node, ObjectPattern, Program } from '@babel/types'
22
import { SFCDescriptor } from '../parse'
3-
import { generateCodeFrame } from '@vue/shared'
3+
import { generateCodeFrame, isArray } from '@vue/shared'
44
import { parse as babelParse, ParserPlugin } from '@babel/parser'
55
import { ImportBinding, SFCScriptCompileOptions } from '../compileScript'
66
import { PropsDestructureBindings } from './defineProps'
@@ -155,6 +155,17 @@ export function resolveParserPlugins(
155155
dts = false
156156
) {
157157
const plugins: ParserPlugin[] = []
158+
if (
159+
!userPlugins ||
160+
!userPlugins.some(
161+
p =>
162+
p === 'importAssertions' ||
163+
p === 'importAttributes' ||
164+
(isArray(p) && p[0] === 'importAttributes')
165+
)
166+
) {
167+
plugins.push('importAttributes')
168+
}
158169
if (lang === 'jsx' || lang === 'tsx') {
159170
plugins.push('jsx')
160171
} else if (userPlugins) {
@@ -163,7 +174,7 @@ export function resolveParserPlugins(
163174
userPlugins = userPlugins.filter(p => p !== 'jsx')
164175
}
165176
if (lang === 'ts' || lang === 'tsx') {
166-
plugins.push(['typescript', { dts }])
177+
plugins.push(['typescript', { dts }], 'explicitResourceManagement')
167178
if (!userPlugins || !userPlugins.includes('decorators')) {
168179
plugins.push('decorators-legacy')
169180
}

0 commit comments

Comments
 (0)
Please sign in to comment.