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

Commit e0b0a78

Browse files
committed
feat: suppport ast transform hooks
1 parent 0437983 commit e0b0a78

File tree

6 files changed

+24
-17
lines changed

6 files changed

+24
-17
lines changed

src/core/macros.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
} from '@babel/types'
1414
import { types as t } from '@babel/core'
1515
import { parseExpression } from '@babel/parser'
16-
import { PropTypeData } from './types'
16+
import { PropTypeData } from '../types'
1717

1818
// Special compiler macros
1919
const DEFINE_PROPS = 'defineProps'

src/core/parseSFC.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Parser as HTMLParser } from 'htmlparser2'
22
import { parse, ParserOptions } from '@babel/parser'
33
import { camelize, capitalize, isHTMLTag, isSVGTag, isVoidTag } from '@vue/shared'
4-
import { ParsedSFC, ScriptTagMeta } from './types'
4+
import { ParsedSFC, ScriptSetupTransformOptions, ScriptTagMeta } from '../types'
55
import { getIdentifierUsages } from './identifiers'
66

7-
export function parseSFC(code: string, id?: string): ParsedSFC {
7+
export function parseSFC(code: string, id?: string, options?: ScriptSetupTransformOptions): ParsedSFC {
88
const components = new Set<string>()
99
const expressions = new Set<string>()
1010
const identifiers = new Set<string>()
@@ -138,6 +138,9 @@ export function parseSFC(code: string, id?: string): ParsedSFC {
138138
scriptSetup.ast = parse(scriptSetup.content, parserOptions).program
139139
script.ast = parse(script.content || '', parserOptions).program
140140

141+
scriptSetup.ast = options?.astTransforms?.scriptSetup?.(scriptSetup.ast) || scriptSetup.ast
142+
script.ast = options?.astTransforms?.script?.(script.ast) || script.ast
143+
141144
return {
142145
id,
143146
template: {

src/core/transform.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import MagicString from 'magic-string'
2+
import { ScriptSetupTransformOptions } from '../types'
23
import { parseSFC } from './parseSFC'
34
import { transformScriptSetup } from './transformScriptSetup'
45

5-
export function transform(input: string, id?: string) {
6+
export function transform(input: string, id?: string, options?: ScriptSetupTransformOptions) {
67
const s = new MagicString(input)
78
const sfc = parseSFC(input, id)
8-
const { code } = transformScriptSetup(sfc)
9+
const { code } = transformScriptSetup(sfc, options)
910

1011
const attributes = {
1112
...sfc.script.attrs,

src/core/transformScriptSetup.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { types as t } from '@babel/core'
22
import { camelize, capitalize } from '@vue/shared'
33
import traverse from '@babel/traverse'
44
import generate from '@babel/generator'
5-
import { ParsedSFC } from './types'
5+
import { ParsedSFC, ScriptSetupTransformOptions } from '../types'
66
import { applyMacros } from './macros'
77
import { getIdentifierDeclarations } from './identifiers'
88

9-
export function transformScriptSetup(sfc: ParsedSFC) {
9+
export function transformScriptSetup(sfc: ParsedSFC, options?: ScriptSetupTransformOptions) {
1010
const { scriptSetup, script, template } = sfc
1111

1212
const imports = scriptSetup.ast.body.filter(n => n.type === 'ImportDeclaration')
@@ -31,7 +31,7 @@ export function transformScriptSetup(sfc: ParsedSFC) {
3131
)
3232

3333
// append `<script setup>` imports to `<script>`
34-
const ast = t.program([
34+
let ast = t.program([
3535
...imports,
3636
...script.ast.body,
3737
])
@@ -139,7 +139,7 @@ export function transformScriptSetup(sfc: ParsedSFC) {
139139
)
140140
}
141141

142-
if (!hasBody) {
142+
if (!hasBody && !options?.astTransforms) {
143143
return {
144144
ast: null,
145145
code: '',
@@ -152,6 +152,8 @@ export function transformScriptSetup(sfc: ParsedSFC) {
152152
t.exportDefaultDeclaration(__sfc) as any,
153153
)
154154

155+
ast = options?.astTransforms?.post?.(ast, sfc) || ast
156+
155157
return {
156158
ast,
157159
code: generate(ast).code,

src/core/types.ts renamed to src/types.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ export interface PropTypeData {
2828
}
2929

3030
export interface ScriptSetupTransformOptions {
31-
32-
}
33-
34-
export interface ScriptSetupTransformContext {
35-
31+
astTransforms?: {
32+
script?: (ast: Program) => Program
33+
scriptSetup?: (ast: Program) => Program
34+
post?: (ast: Program, sfc: ParsedSFC) => Program
35+
}
3636
}

src/unplugin.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { createUnplugin } from 'unplugin'
2+
import { ScriptSetupTransformOptions } from './types'
23
import { transform } from '.'
34

45
const scriptSetupRE = /<script\s+setup/
56

6-
export default createUnplugin(() => ({
7+
export default createUnplugin<ScriptSetupTransformOptions>(options => ({
78
name: 'vue2-script-setup-transform',
89
enforce: 'pre',
910
transformInclude(id) {
1011
return id.endsWith('.vue')
1112
},
12-
transform(code) {
13+
transform(code, id) {
1314
if (scriptSetupRE.test(code))
14-
return transform(code)
15+
return transform(code, id, options)
1516
},
1617
}))

0 commit comments

Comments
 (0)