@pairdraw/emit 0.0.0-stage → 0.16.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +40 -2
- package/dist/chunks/estree.js +217 -0
- package/dist/chunks/estree.js.map +1 -0
- package/dist/chunks/index.js +258 -0
- package/dist/chunks/index.js.map +1 -0
- package/dist/index.d.ts +149 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/rolldown-runtime.js +13 -0
- package/package.json +48 -4
- package/src/estree.ts +195 -0
- package/src/index.ts +4 -0
- package/src/jsx.ts +138 -0
- package/src/vue.ts +164 -0
package/src/vue.ts
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { escapeAttribute, escapeText } from 'entities'
|
|
2
|
+
|
|
3
|
+
import { printExpression, printModule, type SyntaxNode } from './estree'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Text and attribute values are escaped as HTML by `entities`. Vue also reads `{{` as an
|
|
7
|
+
* interpolation before it decodes entities, so text writes `{` as one too and a design's text
|
|
8
|
+
* can never become template syntax.
|
|
9
|
+
*/
|
|
10
|
+
const templateText = (value: string) => escapeText(value).replaceAll('{', '{')
|
|
11
|
+
|
|
12
|
+
export type VueAttribute =
|
|
13
|
+
| { type: 'static'; name: string; value: string }
|
|
14
|
+
/** `:name="expression"` */
|
|
15
|
+
| { type: 'bound'; name: string; expression: SyntaxNode }
|
|
16
|
+
/** `v-model="expression"`, or `v-model:argument` */
|
|
17
|
+
| { type: 'model'; argument?: string; expression: SyntaxNode }
|
|
18
|
+
/** `v-if="expression"`: the element renders only while the expression holds. */
|
|
19
|
+
| { type: 'if'; expression: SyntaxNode }
|
|
20
|
+
|
|
21
|
+
export interface VueElement {
|
|
22
|
+
type: 'element'
|
|
23
|
+
tag: string
|
|
24
|
+
attributes: VueAttribute[]
|
|
25
|
+
children: VueNode[]
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface VueText {
|
|
29
|
+
type: 'text'
|
|
30
|
+
value: string
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** `{{ expression }}`, printed from a syntax tree so it holds only what the tree says. */
|
|
34
|
+
export interface VueInterpolation {
|
|
35
|
+
type: 'interpolation'
|
|
36
|
+
expression: SyntaxNode
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export type VueNode = VueElement | VueText | VueInterpolation
|
|
40
|
+
|
|
41
|
+
export const attribute = (name: string, value: string): VueAttribute => ({
|
|
42
|
+
type: 'static',
|
|
43
|
+
name,
|
|
44
|
+
value
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
export const bound = (name: string, expression: SyntaxNode): VueAttribute => ({
|
|
48
|
+
type: 'bound',
|
|
49
|
+
name,
|
|
50
|
+
expression
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
export const model = (expression: SyntaxNode, argument?: string): VueAttribute => ({
|
|
54
|
+
type: 'model',
|
|
55
|
+
argument,
|
|
56
|
+
expression
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
export const renderIf = (expression: SyntaxNode): VueAttribute => ({ type: 'if', expression })
|
|
60
|
+
|
|
61
|
+
export const element = (
|
|
62
|
+
tag: string,
|
|
63
|
+
attributes: VueAttribute[] = [],
|
|
64
|
+
children: VueNode[] = []
|
|
65
|
+
): VueElement => ({ type: 'element', tag, attributes, children })
|
|
66
|
+
|
|
67
|
+
export const text = (value: string): VueText => ({ type: 'text', value })
|
|
68
|
+
|
|
69
|
+
export const interpolation = (expression: SyntaxNode): VueInterpolation => ({
|
|
70
|
+
type: 'interpolation',
|
|
71
|
+
expression
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
function printAttribute(item: VueAttribute): string {
|
|
75
|
+
if (item.type === 'static') return `${item.name}="${escapeAttribute(item.value)}"`
|
|
76
|
+
const value = escapeAttribute(printExpression(item.expression))
|
|
77
|
+
if (item.type === 'bound') return `:${item.name}="${value}"`
|
|
78
|
+
if (item.type === 'if') return `v-if="${value}"`
|
|
79
|
+
return `${item.argument ? `v-model:${item.argument}` : 'v-model'}="${value}"`
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Markup with nothing added around it, as text content needs. */
|
|
83
|
+
function printInline(node: VueNode): string {
|
|
84
|
+
if (node.type === 'text') return templateText(node.value)
|
|
85
|
+
if (node.type === 'interpolation') {
|
|
86
|
+
const expression = printExpression(node.expression)
|
|
87
|
+
// The template ends an interpolation at the first `}}`, wherever it falls.
|
|
88
|
+
if (expression.includes('}}'))
|
|
89
|
+
throw new Error(`An interpolated expression cannot contain "}}": ${expression}`)
|
|
90
|
+
return `{{ ${expression} }}`
|
|
91
|
+
}
|
|
92
|
+
const open = [node.tag, ...node.attributes.map(printAttribute)].join(' ')
|
|
93
|
+
if (node.children.length === 0) return `<${open} />`
|
|
94
|
+
return `<${open}>${node.children.map(printInline).join('')}</${node.tag}>`
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function printNode(node: VueNode, depth: number): string {
|
|
98
|
+
const indent = ' '.repeat(depth)
|
|
99
|
+
// Vue renders whitespace between text and elements as a space, so an element with text or
|
|
100
|
+
// an interpolation in it keeps its children on its own line, exactly as given.
|
|
101
|
+
if (node.type !== 'element' || node.children.some((child) => child.type !== 'element'))
|
|
102
|
+
return `${indent}${printInline(node)}`
|
|
103
|
+
const open = [node.tag, ...node.attributes.map(printAttribute)].join(' ')
|
|
104
|
+
if (node.children.length === 0) return `${indent}<${open} />`
|
|
105
|
+
const children = node.children.map((child) => printNode(child, depth + 1))
|
|
106
|
+
return [`${indent}<${open}>`, ...children, `${indent}</${node.tag}>`].join('\n')
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** A template's markup, one element per line. */
|
|
110
|
+
export function printTemplate(root: VueNode): string {
|
|
111
|
+
return printNode(root, 0)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export interface VueComponent {
|
|
115
|
+
/** The `<script setup lang="ts">` body, a TypeScript program from `es`. */
|
|
116
|
+
script: SyntaxNode
|
|
117
|
+
template: VueNode
|
|
118
|
+
/** Stylesheet text for a `<style scoped>` block. */
|
|
119
|
+
style?: string
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* A raw block's content, which ends at the first closing tag for its element. Strings in the
|
|
124
|
+
* script and values in the stylesheet can come from an imported design, so a closing tag in
|
|
125
|
+
* them is written `<\\/`, which JavaScript strings and CSS both read back as `</`.
|
|
126
|
+
*/
|
|
127
|
+
const rawBlock = (tag: string, content: string) =>
|
|
128
|
+
`<${tag}>\n${content.trim().replace(/<\/(?=script|style)/gi, '<\\/')}\n</${tag.split(' ')[0]}>`
|
|
129
|
+
|
|
130
|
+
/** Whether a component's script has anything to write, so it gets a script block. */
|
|
131
|
+
const hasScript = (script: SyntaxNode) => printModule(script).trim() !== ''
|
|
132
|
+
|
|
133
|
+
function templateElements(node: VueNode): VueElement[] {
|
|
134
|
+
return node.type === 'element' ? [node, ...node.children.flatMap(templateElements)] : []
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* The elements `printComponent` writes, in the order they open: the template's own, between
|
|
139
|
+
* `null`s for the script, template, and style blocks that hold them.
|
|
140
|
+
*/
|
|
141
|
+
export function componentElements({
|
|
142
|
+
script,
|
|
143
|
+
template,
|
|
144
|
+
style
|
|
145
|
+
}: VueComponent): (VueElement | null)[] {
|
|
146
|
+
return [
|
|
147
|
+
...(hasScript(script) ? [null] : []),
|
|
148
|
+
null,
|
|
149
|
+
...templateElements(template),
|
|
150
|
+
...(style ? [null] : [])
|
|
151
|
+
]
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** A single-file component: script setup, template, and scoped style. */
|
|
155
|
+
export function printComponent({ script, template, style }: VueComponent): string {
|
|
156
|
+
const code = printModule(script)
|
|
157
|
+
// A component with nothing to import or declare has no script block.
|
|
158
|
+
const blocks = [
|
|
159
|
+
...(code.trim() ? [rawBlock('script setup lang="ts"', code)] : []),
|
|
160
|
+
`<template>\n${printNode(template, 1)}\n</template>`,
|
|
161
|
+
...(style ? [rawBlock('style scoped', style)] : [])
|
|
162
|
+
]
|
|
163
|
+
return `${blocks.join('\n\n')}\n`
|
|
164
|
+
}
|