Skip to content

Commit

Permalink
feat(compiler-sfc): support module string names syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Mar 28, 2023
1 parent 3a7572c commit 2704d80
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 20 deletions.
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 @@ -245,6 +248,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 @@ -991,6 +991,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 @@ -471,6 +471,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
20 changes: 6 additions & 14 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
UNREF,
SimpleExpressionNode,
isFunctionType,
walkIdentifiers
walkIdentifiers,
getImportedName
} from '@vue/compiler-dom'
import { DEFAULT_FILENAME, SFCDescriptor, SFCScriptBlock } from './parse'
import {
Expand Down Expand Up @@ -374,7 +375,7 @@ export function compileScript(
function registerUserImport(
source: string,
local: string,
imported: string | false,
imported: string,
isType: boolean,
isFromSetup: boolean,
needTemplateUsageCheck: boolean
Expand All @@ -394,7 +395,7 @@ export function compileScript(

userImports[local] = {
isType,
imported: imported || 'default',
imported,
local,
source,
isFromSetup,
Expand Down Expand Up @@ -996,10 +997,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 @@ -1041,13 +1039,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
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,15 @@ const props = defineProps<{msg: string; ids?: string[]}>()
let ids = _ref([])"
`;
exports[`should support module string names syntax 1`] = `
"
let a = (ref(0));
console.log((a))
"
`;
exports[`using ref binding in property shorthand 1`] = `
"import { ref as _ref } from 'vue'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,22 @@ test('macro import alias and removal', () => {
assertCode(code)
})

test('should support module string names syntax', () => {
const { code } = transform(
`
import { "$" as fromRefs, "$$" as escapeRefs } from 'vue/macros'
let a = fromRefs(ref(0));
console.log(escapeRefs(a))
`
)
// should remove imports
expect(code).not.toMatch(`from 'vue/macros'`)
expect(code).toMatch(`let a = (ref(0))`)
expect(code).toMatch(`console.log((a))`)
assertCode(code)
})

// #6838
test('should not overwrite importing', () => {
const { code } = transform(
Expand Down
7 changes: 2 additions & 5 deletions packages/reactivity-transform/src/reactivityTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import MagicString, { SourceMap } from 'magic-string'
import { walk } from 'estree-walker'
import {
extractIdentifiers,
getImportedName,
isFunctionType,
isInDestructureAssignment,
isReferencedIdentifier,
Expand Down Expand Up @@ -198,11 +199,7 @@ export function transformAST(

for (const specifier of node.specifiers) {
const local = specifier.local.name
const imported =
(specifier.type === 'ImportSpecifier' &&
specifier.imported.type === 'Identifier' &&
specifier.imported.name) ||
'default'
const imported = getImportedName(specifier)
userImports[local] = {
source,
local,
Expand Down

0 comments on commit 2704d80

Please sign in to comment.