generated from unplugin/unplugin-starter
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
transform.ts
200 lines (178 loc) · 5.49 KB
/
transform.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import type { SFCDescriptor, SFCScriptBlock } from 'vue/compiler-sfc'
import {
compileScript,
compileTemplate,
parse,
rewriteDefault,
} from 'vue/compiler-sfc'
import type { ElementNode } from '@vue/compiler-core'
import { format as prettierFormat } from 'prettier'
/**
* Transforms a vue single-file-component into Storybook's Component Story Format (CSF).
*/
export function transform(code: string) {
const { descriptor } = parse(code)
if (descriptor.template === null) throw new Error('No template found in SFC')
let result = ''
const resolvedScript = resolveScript(descriptor)
if (resolvedScript) {
result += rewriteDefault(resolvedScript.content, '_sfc_main')
result += '\n'
} else {
result += 'const _sfc_main = {}\n'
}
result += transformTemplate(descriptor.template.content, resolvedScript)
result = organizeImports(result)
return result
/*
return `
import MyButton from './Button.vue';
import { userEvent, within } from '@storybook/testing-library';
import { expect } from '@storybook/jest';
export default {
title: 'Example/ButtonTest',
component: MyButton,
argTypes: {
backgroundColor: { control: 'color' },
size: {
control: { type: 'select', options: ['small', 'medium', 'large'] },
},
onClick: {},
},
};
const Template = (args) => ({
components: { MyButton },
setup() {
return { args };
},
template: '<my-button v-bind="args" />',
});
export const Primary = Template.bind({});
Primary.args = {
primary: true,
label: 'Button',
};
Primary.play = async ({ args, canvasElement }) => {
const canvas = within(canvasElement);
const button = canvas.getByRole('button');
await userEvent.click(button);
await expect(args.onClick).toHaveBeenCalled();
};
export const Secondary = Template.bind({});
Secondary.args = {
label: 'Button',
};
export const Large = Template.bind({});
Large.args = {
size: 'large',
label: 'Button',
};
export const Small = Template.bind({});
Small.args = {
size: 'small',
label: 'Button',
};
`
*/
}
function transformTemplate(content: string, resolvedScript?: SFCScriptBlock) {
const template = compileTemplate({
source: content,
filename: 'test.vue',
id: 'test',
/* compilerOptions: {
nodeTransforms: [extractTitle, replaceStoryNode],
}, */
})
const roots =
template.ast?.children.filter(
(node) => node.type === 1 /* NodeTypes.ELEMENT */
) ?? []
if (roots.length !== 1) {
throw new Error('Expected exactly one <Stories> element as root.')
}
const root = roots[0]
if (root.type !== 1 || root.tag !== 'Stories')
throw new Error('Expected root to be a <Stories> element.')
let result = generateDefaultImport(root)
for (const story of root.children ?? []) {
if (story.type !== 1 || story.tag !== 'Story') continue
result += generateStoryImport(story, resolvedScript)
}
return result
}
function generateDefaultImport(root: ElementNode) {
const title = extractTitle(root)
const component = extractComponent(root)
return `export default {
${title ? `title: '${title}',` : ''}
${component ? `component: ${component},` : ''}
//decorators: [ ... ],
//parameters: { ... }
}
`
}
function extractProp(node: ElementNode, name: string) {
if (node.type === 1) {
return node.props.find(
(prop) =>
prop.name === name ||
(prop.name === 'bind' &&
prop.type === 7 &&
prop.arg?.type === 4 &&
prop.arg?.content === name)
)
}
}
function extractTitle(node: ElementNode) {
const prop = extractProp(node, 'title')
if (prop && prop.type === 6) return prop.value?.content
}
function extractComponent(node: ElementNode) {
const prop = extractProp(node, 'component')
if (prop && prop.type === 7)
return prop.exp?.type === 4
? prop.exp?.content.replace('_ctx.', '')
: undefined
}
function generateStoryImport(
story: ElementNode,
resolvedScript?: SFCScriptBlock
) {
const title = extractTitle(story)
if (!title) throw new Error('Story is missing a title')
const cleanTitle = title.replace(/[^a-zA-Z0-9]/g, '_')
const storyTemplate = parse(
story.loc.source
.replace(/<Story/, '<template')
.replace(/<\/Story>/, '</template>')
).descriptor.template?.content
if (storyTemplate === undefined) throw new Error('No template found in Story')
const { code } = compileTemplate({
source: storyTemplate.trim(),
filename: 'test.vue',
id: 'test',
compilerOptions: { bindingMetadata: resolvedScript?.bindings },
})
const renderFunction = code.replace(
'export function render',
`function render${cleanTitle}`
)
// Each named export is a story, has to return a Vue ComponentOptionsBase
return `
${renderFunction}
export const ${cleanTitle} = () => Object.assign({render: render${cleanTitle}}, _sfc_main)
${cleanTitle}.storyName = '${title}'`
}
// Minimal version of https://github.com/vitejs/vite/blob/57916a476924541dd7136065ceee37ae033ca78c/packages/plugin-vue/src/main.ts#L297
function resolveScript(descriptor: SFCDescriptor) {
if (descriptor.script || descriptor.scriptSetup)
return compileScript(descriptor, { id: 'test' })
}
function organizeImports(result: string): string {
// Use prettier to organize imports
return prettierFormat(result, {
parser: 'babel',
plugins: ['prettier-plugin-organize-imports'],
})
}