generated from unplugin/unplugin-starter
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
transform.ts
151 lines (131 loc) · 4.47 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
import type { SFCDescriptor, SFCScriptBlock } from 'vue/compiler-sfc'
import { compileScript, compileTemplate, parse, rewriteDefault } from 'vue/compiler-sfc'
import type { ElementNode } from '@vue/compiler-core'
/**
* 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)
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 ?? []
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)
return `export default {
${title ? `title: '${title}',` : ''}
//component: MyComponent,
//decorators: [ ... ],
//parameters: { ... }
}
`
}
function extractTitle(node: ElementNode) {
if (node.type === 1) {
const titleProp = node.props.find(prop => prop.name === 'title')
if (titleProp && titleProp.type === 6)
return titleProp.value?.content
}
}
function generateStoryImport(story: ElementNode, resolvedScript?: SFCScriptBlock) {
const title = extractTitle(story)
if (!title)
throw new Error('Story is missing a title')
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${title}`)
// Each named export is a story, has to return a Vue ComponentOptionsBase
return `
${renderFunction}
export const ${title} = () => Object.assign({render: render${title}}, _sfc_main)`
}
// 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' })
}