/
plugin.ts
270 lines (249 loc) · 8.27 KB
/
plugin.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import { createRequire } from 'module'
import { relative, dirname } from 'pathe'
import { resolveConfig as resolveViteConfig, Plugin as VitePlugin } from 'vite'
import { lookup as lookupMime } from 'mrmime'
import { APP_PATH, DIST_CLIENT_PATH } from './alias.js'
import { Context } from './context.js'
import { notifyStoryChange } from './stories.js'
import { makeTree } from './tree.js'
import { parseColor } from './colors.js'
import { createMarkdownRenderer } from './markdown.js'
const require = createRequire(import.meta.url)
export const STORIES_ID = '$histoire-stories'
export const RESOLVED_STORIES_ID = `/${STORIES_ID}-resolved`
export const SETUP_ID = '$histoire-setup'
export const NOOP_ID = '/$histoire-noop'
export const CONFIG_ID = '$histoire-config'
export const RESOLVED_CONFIG_ID = `/${CONFIG_ID}-resolved`
export const THEME_ID = '$histoire-theme'
export const RESOLVED_THEME_ID = `/${THEME_ID}-resolved.css`
export async function createVitePlugins (ctx: Context): Promise<VitePlugin[]> {
const viteConfig = await resolveViteConfig({}, ctx.mode === 'dev' ? 'serve' : 'build')
const hasVuePlugin = viteConfig.plugins.find(p => p.name === 'vite:vue')
const plugins: VitePlugin[] = []
if (!hasVuePlugin) {
plugins.push((await import('@vitejs/plugin-vue')).default())
}
if (ctx.config.vite) {
plugins.push({
name: 'histoire-vite-config-override',
config (config, env) {
return typeof ctx.config.vite === 'function' ? ctx.config.vite(config, env) : ctx.config.vite
},
})
}
plugins.push({
name: 'histoire-vite-plugin',
config () {
return {
optimizeDeps: {
// force include vue to avoid duplicated copies when linked + optimized
include: [
'vue',
dirname(require.resolve('@vue/runtime-core/package.json')),
dirname(require.resolve('shiki/package.json')),
dirname(require.resolve('vscode-textmate/package.json')),
dirname(require.resolve('vscode-oniguruma/package.json')),
dirname(require.resolve('case/package.json')),
],
},
server: {
fs: {
allow: [DIST_CLIENT_PATH, viteConfig.root, process.cwd()],
},
},
}
},
async resolveId (id, importer) {
if (id.startsWith(STORIES_ID)) {
return RESOLVED_STORIES_ID
}
if (id.startsWith(SETUP_ID)) {
if (ctx.config.setupFile) {
return this.resolve(ctx.config.setupFile, importer, {
skipSelf: true,
})
} else {
return NOOP_ID
}
}
if (id.startsWith(CONFIG_ID)) {
return RESOLVED_CONFIG_ID
}
if (id.startsWith(THEME_ID)) {
return RESOLVED_THEME_ID
}
},
load (id) {
if (id === RESOLVED_STORIES_ID) {
const resolvedStories = ctx.storyFiles.filter(s => !!s.story)
const files = resolvedStories.map((file, index) => {
return {
id: file.id,
path: file.treePath,
story: file.story,
framework: 'vue3',
index,
}
})
return `import { defineAsyncComponent } from 'vue'
${resolvedStories.map((file, index) => `const Comp${index} = defineAsyncComponent(() => import('${file.path}'))`).join('\n')}
export let files = [${files.map((file) => `{${JSON.stringify(file).slice(1, -1)}, component: Comp${file.index}}`).join(',\n')}]
export let tree = ${JSON.stringify(makeTree(ctx.config, resolvedStories))}
const handlers = []
export function onUpdate (cb) {
handlers.push(cb)
}
if (import.meta.hot) {
import.meta.hot.accept(newModule => {
files = newModule.files
tree = newModule.tree
handlers.forEach(h => {
h(newModule.files, newModule.tree)
newModule.onUpdate(h)
})
})
}`
}
if (id === NOOP_ID) {
return `export default () => {}`
}
if (id === RESOLVED_CONFIG_ID) {
let js = `export const config = ${JSON.stringify(ctx.config)}\n`
if (ctx.config.theme?.logo) {
for (const key in ctx.config.theme.logo) {
js += `import Logo_${key} from '${ctx.config.theme.logo[key]}'\n`
}
}
js += `export const logos = {${Object.keys(ctx.config.theme?.logo ?? {}).map(key => `${key}: Logo_${key}`).join(', ')}}\n`
return js
}
if (id === RESOLVED_THEME_ID) {
let css = '*, ::before, ::after {'
// Colors
for (const color in ctx.config.theme?.colors ?? {}) {
for (const key in ctx.config.theme.colors[color]) {
css += `--_histoire-color-${color}-${key}: ${parseColor(ctx.config.theme.colors[color][key]).color.join(' ')};`
}
}
css += '}'
return css
}
},
handleHotUpdate (updateContext) {
const story = ctx.storyFiles.find(file => file.path === updateContext.file)
if (story) {
notifyStoryChange(story)
}
},
configureServer (server) {
server.middlewares.use(async (req, res, next) => {
if (req.url!.startsWith('/__sandbox')) {
res.statusCode = 200
let html = `
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="description" content="">
</head>
<body>
<div id="app"></div>
<script>
// Hide spammy vite messages
const origConsoleLog = console.log
console.log = (...args) => {
if (typeof args[0] !== 'string' || !args[0].startsWith('[vite] connect')) {
origConsoleLog(...args)
}
}
</script>
<script type="module" src="/@fs/${APP_PATH}/sandbox.js"></script>
</body>
</html>`
// Apply Vite HTML transforms. This injects the Vite HMR client, and
// also applies HTML transforms from Vite plugins
html = await server.transformIndexHtml(req.url, html)
res.end(html)
return
}
next()
})
// serve our index.html after vite history fallback
return () => {
server.middlewares.use(async (req, res, next) => {
if (req.url!.endsWith('.html')) {
res.statusCode = 200
let html = `
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="icon" href=""/>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="description" content="">
${ctx.config.theme?.favicon ? `<link rel="icon" type="${lookupMime(ctx.config.theme.favicon)}" href="${ctx.config.theme.favicon}"/>` : ''}
</head>
<body>
<div id="app"></div>
<script type="module" src="/@fs/${APP_PATH}/index.js"></script>
</body>
</html>`
// Apply Vite HTML transforms. This injects the Vite HMR client, and
// also applies HTML transforms from Vite plugins
html = await server.transformIndexHtml(req.url, html)
res.end(html)
return
}
next()
})
}
},
})
// Custom blocks
plugins.push(...await createCustomBlocksPlugins(ctx))
if (ctx.mode === 'build') {
// Add file name in build mode to have components names instead of <Anonymous>
const include = [/\.vue$/]
const exclude = [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/, /[\\/]\.nuxt[\\/]/]
plugins.push({
name: 'histoire-file-name-plugin',
enforce: 'post',
transform (code, id) {
if (exclude.some(r => r.test(id))) return
if (include.some(r => r.test(id))) {
const file = relative(viteConfig.root, id)
const index = code.indexOf('export default')
const result = `${code.substring(0, index)}_sfc_main.__file = '${file}'\n${code.substring(index)}`
return result
}
},
})
}
return plugins
}
async function createCustomBlocksPlugins (ctx) {
const plugins: VitePlugin[] = []
let md = await createMarkdownRenderer()
if (ctx.config.markdown) {
const result = await ctx.config.markdown(md)
if (result) {
md = result
}
}
plugins.push({
name: 'histoire-vue-docs-block',
transform (code, id) {
if (!id.includes('?vue&type=docs')) return
if (!id.includes('lang.md')) return
const html = md.render(code)
return `export default Comp => {
Comp.doc = ${JSON.stringify(html)}
}`
},
})
return plugins
}