Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Commit 4a631e4

Browse files
authored
fix: isolatedModules (#78)
1 parent 634a7dd commit 4a631e4

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

src/core/parseSFC.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import { getIdentifierUsages } from './identifiers'
77
import { parse } from './babel'
88
import { pascalize } from './utils'
99

10+
const ELEMENT: NodeTypes.ELEMENT = 1
11+
const DIRECTIVE: NodeTypes.DIRECTIVE = 7
12+
const SIMPLE_EXPRESSION: NodeTypes.SIMPLE_EXPRESSION = 4
13+
1014
const multilineCommentsRE = /\/\*\s(.|[\r\n])*?\*\//gm
1115
const singlelineCommentsRE = /\/\/\s.*/g
1216

@@ -32,29 +36,25 @@ const BUILD_IN_DIRECTIVES = new Set([
3236
// 'ref',
3337
])
3438

35-
const parseDirective = (attr: string) => {
36-
try {
37-
const elementNode = baseCompile(`<a ${attr}></a>`).ast.children[0]
38-
if (elementNode?.type !== NodeTypes.ELEMENT) return undefined
39+
function parseDirective(comp: string, attr: string, body: string) {
40+
const elementNode = baseCompile(`<${comp} ${attr}="${body}" />`).ast.children[0]
41+
if (elementNode?.type !== ELEMENT) return undefined
3942

40-
const directiveNode = elementNode.props[0]
41-
if (directiveNode?.type !== NodeTypes.DIRECTIVE) return undefined
43+
const directiveNode = elementNode.props[0]
44+
if (directiveNode?.type !== DIRECTIVE) return undefined
4245

43-
const { arg, modifiers, name } = directiveNode
44-
const argExpression
45-
= arg?.type !== NodeTypes.SIMPLE_EXPRESSION
46+
const { arg, modifiers, name } = directiveNode
47+
const argExpression
48+
= arg?.type !== SIMPLE_EXPRESSION
4649
? undefined
4750
: arg.isStatic
4851
? JSON.stringify(arg.content)
4952
: arg.content
50-
return {
51-
argExpression,
52-
modifiers,
53-
name,
54-
}
55-
}
56-
catch (error) {
57-
return undefined
53+
54+
return {
55+
argExpression,
56+
modifiers,
57+
name,
5858
}
5959
}
6060

@@ -123,7 +123,7 @@ export function parseSFC(code: string, id?: string, options?: ScriptSetupTransfo
123123
}
124124

125125
if (key.startsWith('v-')) {
126-
const parsedDirective = parseDirective(key)
126+
const parsedDirective = parseDirective(name, key, value)
127127
if (parsedDirective && !BUILD_IN_DIRECTIVES.has(parsedDirective.name))
128128
directives.add(camelize(parsedDirective.name))
129129
if (parsedDirective?.argExpression)

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"esModuleInterop": true,
88
"strict": true,
99
"strictNullChecks": true,
10+
"isolatedModules": true,
1011
"resolveJsonModule": true
1112
},
1213
"vueCompilerOptions": {

0 commit comments

Comments
 (0)