Skip to content

Commit 69b3630

Browse files
committed
refactor(directive): code-spiltting
1 parent a4a9072 commit 69b3630

File tree

5 files changed

+70
-57
lines changed

5 files changed

+70
-57
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@
5050
"module": "dist/index.mjs",
5151
"types": "index.d.ts",
5252
"scripts": {
53-
"build": "rimraf dist && tsup src/*.ts --format cjs,esm --dts --splitting && esno scripts/postbuild.ts",
54-
"dev": "tsup src/*.ts --format cjs,esm --watch src",
55-
"example:build": "npm -C examples/vue3 run build",
56-
"example:dev": "npm -C examples/vue3 run dev",
53+
"build": "tsup src/*.ts && esno scripts/postbuild.ts",
54+
"dev": "tsup src/*.ts --watch src",
55+
"example:build": "npm -C examples/vite-vue3 run build",
56+
"example:dev": "npm -C examples/vite-vue3 run dev",
5757
"prepublishOnly": "npm run build",
5858
"release": "npx bumpp --commit --tag --push",
5959
"test": "jest",
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import Debug from 'debug'
2+
import type MagicString from 'magic-string'
3+
import { pascalCase, stringifyComponentImport } from '../../utils'
4+
import type { Context } from '../../context'
5+
import { SupportedTransformer } from '../../..'
6+
7+
const debug = Debug('unplugin-vue-components:transform:directive')
8+
9+
export default async(code: string, transformer: SupportedTransformer, s: MagicString, ctx: Context, sfcPath: string) => {
10+
let no = 0
11+
12+
const { resolve } = await (transformer === 'vue2' ? import('./vue2') : import('./vue3'))
13+
14+
const results = resolve(code, s)
15+
for (const { rawName, replace } of results) {
16+
debug(`| ${rawName}`)
17+
const name = pascalCase(rawName)
18+
ctx.updateUsageMap(sfcPath, [name])
19+
20+
const directive = await ctx.findComponent(name, 'directive', [sfcPath])
21+
if (!directive) continue
22+
23+
const varName = `__unplugin_directives_${no}`
24+
s.prepend(`${stringifyComponentImport({ ...directive, name: varName }, ctx)};\n`)
25+
no += 1
26+
replace(varName)
27+
}
28+
29+
debug(`^ (${no})`)
30+
}
Lines changed: 8 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
import Debug from 'debug'
21
import type {
32
CallExpression, ObjectProperty, File, VariableDeclaration, FunctionExpression, BlockStatement,
43
} from '@babel/types'
54
import type MagicString from 'magic-string'
65
import { parse, ParseResult } from '@babel/parser'
76
import traverse from '@babel/traverse'
8-
import { pascalCase, stringifyComponentImport } from '../utils'
9-
import type { Context } from '../context'
10-
import { ResolveResult } from '../transformer'
11-
import { SupportedTransformer } from '../..'
12-
13-
const debug = Debug('unplugin-vue-components:transform:directive')
7+
import { ResolveResult } from '../../transformer'
148

159
/**
1610
* get Vue 2 render function position
@@ -20,16 +14,16 @@ const debug = Debug('unplugin-vue-components:transform:directive')
2014
const getRenderFnStart = (ast: ParseResult<File>): number => {
2115
const renderFn = ast.program.body.find((node): node is VariableDeclaration =>
2216
node.type === 'VariableDeclaration'
23-
&& node.declarations[0].id.type === 'Identifier'
24-
&& node.declarations[0].id.name === 'render',
17+
&& node.declarations[0].id.type === 'Identifier'
18+
&& node.declarations[0].id.name === 'render',
2519
)
2620
const start = (((renderFn?.declarations[0].init as FunctionExpression).body) as BlockStatement).start
2721
if (start === null)
2822
throw new Error('[unplugin-vue-components:directive] Cannot find render function position.')
2923
return start + 1
3024
}
3125

32-
const resolveVue2 = (code: string, s: MagicString): ResolveResult[] => {
26+
export const resolve = (code: string, s: MagicString): ResolveResult[] => {
3327
const ast = parse(code, {
3428
sourceType: 'module',
3529
})
@@ -51,8 +45,8 @@ const resolveVue2 = (code: string, s: MagicString): ResolveResult[] => {
5145
const directives = args[1].properties.find(
5246
(property): property is ObjectProperty =>
5347
property.type === 'ObjectProperty'
54-
&& property.key.type === 'Identifier'
55-
&& property.key.name === 'directives',
48+
&& property.key.type === 'Identifier'
49+
&& property.key.name === 'directives',
5650
)?.value
5751
if (!directives || directives.type !== 'ArrayExpression')
5852
continue
@@ -65,8 +59,8 @@ const resolveVue2 = (code: string, s: MagicString): ResolveResult[] => {
6559
const nameNode = directive.properties.find(
6660
(p): p is ObjectProperty =>
6761
p.type === 'ObjectProperty'
68-
&& p.key.type === 'Identifier'
69-
&& p.key.name === 'name',
62+
&& p.key.type === 'Identifier'
63+
&& p.key.name === 'name',
7064
)?.value
7165
if (nameNode?.type !== 'StringLiteral') continue
7266
const name = nameNode.value
@@ -82,42 +76,3 @@ const resolveVue2 = (code: string, s: MagicString): ResolveResult[] => {
8276

8377
return results
8478
}
85-
86-
const resolveVue3 = (code: string, s: MagicString): ResolveResult[] => {
87-
const results: ResolveResult[] = []
88-
89-
for (const match of code.matchAll(/_resolveDirective\("(.+?)"\)/g)) {
90-
const matchedName = match[1]
91-
if (match.index != null && matchedName && !matchedName.startsWith('_')) {
92-
const start = match.index
93-
const end = start + match[0].length
94-
results.push({
95-
rawName: matchedName,
96-
replace: resolved => s.overwrite(start, end, resolved),
97-
})
98-
}
99-
}
100-
101-
return results
102-
}
103-
104-
export default async(code: string, transformer: SupportedTransformer, s: MagicString, ctx: Context, sfcPath: string) => {
105-
let no = 0
106-
107-
const results = transformer === 'vue2' ? resolveVue2(code, s) : resolveVue3(code, s)
108-
for (const { rawName, replace } of results) {
109-
debug(`| ${rawName}`)
110-
const name = pascalCase(rawName)
111-
ctx.updateUsageMap(sfcPath, [name])
112-
113-
const directive = await ctx.findComponent(name, 'directive', [sfcPath])
114-
if (!directive) continue
115-
116-
const varName = `__unplugin_directives_${no}`
117-
s.prepend(`${stringifyComponentImport({ ...directive, name: varName }, ctx)};\n`)
118-
no += 1
119-
replace(varName)
120-
}
121-
122-
debug(`^ (${no})`)
123-
}

src/core/transforms/directive/vue3.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type MagicString from 'magic-string'
2+
import { ResolveResult } from '../../transformer'
3+
4+
export const resolve = (code: string, s: MagicString): ResolveResult[] => {
5+
const results: ResolveResult[] = []
6+
7+
for (const match of code.matchAll(/_resolveDirective\("(.+?)"\)/g)) {
8+
const matchedName = match[1]
9+
if (match.index != null && matchedName && !matchedName.startsWith('_')) {
10+
const start = match.index
11+
const end = start + match[0].length
12+
results.push({
13+
rawName: matchedName,
14+
replace: resolved => s.overwrite(start, end, resolved),
15+
})
16+
}
17+
}
18+
19+
return results
20+
}

tsup.config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { Options } from 'tsup'
2+
3+
export const tsup: Options = {
4+
format: ['cjs', 'esm'],
5+
dts: true,
6+
splitting: true,
7+
clean: true,
8+
}

0 commit comments

Comments
 (0)