Skip to content
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

feat(compiler-sfc): support module string names syntax #7428

Merged
merged 3 commits into from
Mar 28, 2023
Merged
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
16 changes: 15 additions & 1 deletion packages/compiler-core/src/babelUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import type {
Function,
ObjectProperty,
BlockStatement,
Program
Program,
ImportDefaultSpecifier,
ImportNamespaceSpecifier,
ImportSpecifier
} from '@babel/types'
import { walk } from 'estree-walker'

Expand Down Expand Up @@ -243,6 +246,17 @@ export const isStaticProperty = (node: Node): node is ObjectProperty =>
export const isStaticPropertyKey = (node: Node, parent: Node) =>
isStaticProperty(parent) && parent.key === node

export function getImportedName(
specifier: ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier
) {
if (specifier.type === 'ImportSpecifier')
return specifier.imported.type === 'Identifier'
? specifier.imported.name
: specifier.imported.value
else if (specifier.type === 'ImportNamespaceSpecifier') return '*'
return 'default'
}

/**
* Copied from https://github.com/babel/babel/blob/main/packages/babel-types/src/validators/isReferenced.ts
* To avoid runtime dependency on @babel/types (which includes process references)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,20 @@ return { ref }
}"
`;

exports[`SFC compile <script setup> > imports > should support module string names syntax 1`] = `
"import { \\"😏\\" as foo } from './foo'

export default {
setup(__props, { expose }) {
expose();


return { get foo() { return foo } }
}

}"
`;

exports[`SFC compile <script setup> > inlineTemplate mode > avoid unref() when necessary 1`] = `
"import { unref as _unref, toDisplayString as _toDisplayString, createTextVNode as _createTextVNode, withCtx as _withCtx, createVNode as _createVNode, createElementVNode as _createElementVNode, Fragment as _Fragment, openBlock as _openBlock, createElementBlock as _createElementBlock } from \\"vue\\"

Expand Down
15 changes: 15 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,21 @@ defineExpose({ foo: 123 })
})
})
})

test('should support module string names syntax', () => {
const { content, bindings } = compile(`
<script>
import { "😏" as foo } from './foo'
</script>
<script setup>
import { "😏" as foo } from './foo'
</script>
`)
assertCode(content)
expect(bindings).toStrictEqual({
foo: BindingTypes.SETUP_MAYBE_REF
})
})
})

// in dev mode, declared bindings are returned as an object from setup()
Expand Down
18 changes: 5 additions & 13 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
SimpleExpressionNode,
isFunctionType,
walkIdentifiers,
getImportedName,
unwrapTSNode
} from '@vue/compiler-dom'
import { DEFAULT_FILENAME, SFCDescriptor, SFCScriptBlock } from './parse'
Expand Down Expand Up @@ -380,7 +381,7 @@ export function compileScript(
function registerUserImport(
source: string,
local: string,
imported: string | false,
imported: string,
isType: boolean,
isFromSetup: boolean,
needTemplateUsageCheck: boolean
Expand All @@ -400,7 +401,7 @@ export function compileScript(

userImports[local] = {
isType,
imported: imported || 'default',
imported,
local,
source,
isFromSetup,
Expand Down Expand Up @@ -1002,10 +1003,7 @@ export function compileScript(
if (node.type === 'ImportDeclaration') {
// record imports for dedupe
for (const specifier of node.specifiers) {
const imported =
specifier.type === 'ImportSpecifier' &&
specifier.imported.type === 'Identifier' &&
specifier.imported.name
const imported = getImportedName(specifier)
registerUserImport(
node.source.value,
specifier.local.name,
Expand Down Expand Up @@ -1047,13 +1045,7 @@ export function compileScript(
for (let i = 0; i < node.specifiers.length; i++) {
const specifier = node.specifiers[i]
const local = specifier.local.name
let imported =
specifier.type === 'ImportSpecifier' &&
specifier.imported.type === 'Identifier' &&
specifier.imported.name
if (specifier.type === 'ImportNamespaceSpecifier') {
imported = '*'
}
const imported = getImportedName(specifier)
const source = node.source.value
const existing = userImports[local]
if (
Expand Down