Skip to content

Commit 4293948

Browse files
committed
feat(compiler): support string source
1 parent 73c3534 commit 4293948

File tree

7 files changed

+200
-112
lines changed

7 files changed

+200
-112
lines changed

packages/babel/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"@babel/core": "catalog:",
5959
"@babel/parser": "catalog:",
6060
"@babel/plugin-syntax-jsx": "^7.25.9",
61-
"@babel/traverse": "^7.26.7",
61+
"@babel/traverse": "^7.26.8",
6262
"@babel/types": "catalog:",
6363
"@vue-jsx-vapor/compiler": "workspace:*",
6464
"source-map-js": "^1.2.1"

packages/compiler/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,10 @@
5555
"test": "vitest"
5656
},
5757
"dependencies": {
58+
"@babel/parser": "catalog:",
5859
"@babel/types": "catalog:",
5960
"@vue/compiler-dom": "catalog:",
6061
"@vue/compiler-vapor": "catalog:",
6162
"@vue/shared": "catalog:"
62-
},
63-
"devDependencies": {
64-
"@babel/parser": "catalog:"
6563
}
6664
}

packages/compiler/src/compile.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { extend } from '@vue/shared'
1+
import { extend, isString } from '@vue/shared'
22
import {
33
type VaporCodegenResult as BaseVaporCodegenResult,
44
generate,
55
} from '@vue/compiler-vapor'
6+
import { parse } from '@babel/parser'
67
import {
78
type DirectiveTransform,
89
type NodeTransform,
@@ -27,7 +28,7 @@ import {
2728
} from './ir'
2829
import { transformVFor } from './transforms/vFor'
2930
import type { CompilerOptions as BaseCompilerOptions } from '@vue/compiler-dom'
30-
import type { JSXElement, JSXFragment } from '@babel/types'
31+
import type { ExpressionStatement, JSXElement, JSXFragment } from '@babel/types'
3132

3233
export { generate }
3334

@@ -38,15 +39,18 @@ export interface VaporCodegenResult
3839

3940
// code/AST -> IR (transform) -> JS (generate)
4041
export function compile(
41-
root: JSXElement | JSXFragment,
42+
source: JSXElement | JSXFragment | string,
4243
options: CompilerOptions = {},
4344
): VaporCodegenResult {
4445
const resolvedOptions = extend({}, options, {
4546
inline: true,
4647
prefixIdentifiers: false,
4748
expressionPlugins: options.expressionPlugins || ['jsx'],
4849
})
49-
if (options.isTS) {
50+
if (!resolvedOptions.source && isString(source)) {
51+
resolvedOptions.source = source
52+
}
53+
if (resolvedOptions.isTS) {
5054
const { expressionPlugins } = resolvedOptions
5155
if (!expressionPlugins.includes('typescript')) {
5256
resolvedOptions.expressionPlugins = [
@@ -55,7 +59,14 @@ export function compile(
5559
]
5660
}
5761
}
58-
62+
const root = isString(source)
63+
? (
64+
parse(source, {
65+
sourceType: 'module',
66+
plugins: resolvedOptions.expressionPlugins,
67+
}).program.body[0] as ExpressionStatement
68+
).expression
69+
: source
5970
const children =
6071
root.type === 'JSXFragment'
6172
? root.children
@@ -65,7 +76,7 @@ export function compile(
6576
const ast: RootNode = {
6677
type: IRNodeTypes.ROOT,
6778
children,
68-
source: options.source || '',
79+
source: resolvedOptions.source || '',
6980
}
7081
const [nodeTransforms, directiveTransforms] = getBaseTransformPreset()
7182

@@ -74,12 +85,12 @@ export function compile(
7485
extend({}, resolvedOptions, {
7586
nodeTransforms: [
7687
...nodeTransforms,
77-
...(options.nodeTransforms || []), // user transforms
88+
...(resolvedOptions.nodeTransforms || []), // user transforms
7889
],
7990
directiveTransforms: extend(
8091
{},
8192
directiveTransforms,
82-
options.directiveTransforms || {}, // user transforms
93+
resolvedOptions.directiveTransforms || {}, // user transforms
8394
),
8495
}),
8596
)

packages/compiler/test/transforms/__snapshots__/vFor.spec.ts.snap

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -52,28 +52,6 @@ exports[`compiler: v-for > nested v-for 1`] = `
5252
"
5353
`;
5454

55-
exports[`compiler: v-for > object de-structured index 1`] = `
56-
"
57-
const n0 = _createFor(() => (items), (_for_item0, _for_key0, _for_index0) => {
58-
const n2 = t0()
59-
_setText(n2, () => (_for_item0.value.id), () => (_for_item0.value.value), () => (_for_index0.value))
60-
return n2
61-
}, ({ id, value }, key, index) => (id))
62-
return n0
63-
"
64-
`;
65-
66-
exports[`compiler: v-for > object de-structured value (with key and index) 1`] = `
67-
"
68-
const n0 = _createFor(() => (items), (_for_item0, _for_key0, _for_index0) => {
69-
const n2 = t0()
70-
_setText(n2, () => (_for_item0.value.id), () => (_for_item0.value.value), () => (_for_index0.value))
71-
return n2
72-
}, ({ id, value }, key, index) => (id))
73-
return n0
74-
"
75-
`;
76-
7755
exports[`compiler: v-for > object de-structured value (with rest) 1`] = `
7856
"
7957
const n0 = _createFor(() => (list), (_for_item0, _for_key0) => {
@@ -107,17 +85,6 @@ exports[`compiler: v-for > object value, key and index 1`] = `
10785
"
10886
`;
10987

110-
exports[`compiler: v-for > object value, key, index) 1`] = `
111-
"
112-
const n0 = _createFor(() => (items), (_for_item0, _for_key0, _for_index0) => {
113-
const n2 = t0()
114-
_setText(n2, () => (id), () => (_for_item0.value), () => (_for_index0.value))
115-
return n2
116-
}, (value, key, index) => (id))
117-
return n0
118-
"
119-
`;
120-
12188
exports[`compiler: v-for > v-for aliases w/ complex expressions 1`] = `
12289
"
12390
const n0 = _createFor(() => (list), (_for_item0) => {

packages/unplugin/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@
169169
},
170170
"dependencies": {
171171
"@babel/core": "catalog:",
172-
"@babel/plugin-transform-typescript": "^7.26.7",
172+
"@babel/plugin-transform-typescript": "^7.26.8",
173173
"@vue-jsx-vapor/babel": "workspace:*",
174174
"@vue-jsx-vapor/compiler": "workspace:*",
175175
"@vue-macros/common": "^1.16.1",

0 commit comments

Comments
 (0)