-
-
Notifications
You must be signed in to change notification settings - Fork 9.9k
Expand file tree
/
Copy pathindex.ts
More file actions
67 lines (54 loc) · 1.81 KB
/
index.ts
File metadata and controls
67 lines (54 loc) · 1.81 KB
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
// noinspection JSUnusedGlobalSymbols
import { readFile } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import { NoStatsForViteDevError } from 'storybook/internal/server-errors';
import type { Middleware, Options } from 'storybook/internal/types';
import type { ViteDevServer } from 'vite';
import { build as viteBuild } from './build';
import type { ViteBuilder } from './types';
import { createViteServer } from './vite-server';
export { withoutVitePlugins } from './utils/without-vite-plugins';
export { hasVitePlugins } from './utils/has-vite-plugins';
export * from './types';
function iframeHandler(options: Options, server: ViteDevServer): Middleware {
return async (req, res) => {
const indexHtml = await readFile(
fileURLToPath(import.meta.resolve('@storybook/builder-vite/input/iframe.html')),
{
encoding: 'utf8',
}
);
const transformed = await server.transformIndexHtml('/iframe.html', indexHtml);
res.setHeader('Content-Type', 'text/html');
res.statusCode = 200;
res.write(transformed);
res.end();
};
}
let server: ViteDevServer;
export async function bail(): Promise<void> {
return server?.close();
}
export const start: ViteBuilder['start'] = async ({
startTime,
options,
router,
server: devServer,
}) => {
server = await createViteServer(options as Options, devServer);
router.get('/iframe.html', iframeHandler(options as Options, server));
router.use(server.middlewares);
return {
bail,
stats: {
toJson: () => {
throw new NoStatsForViteDevError();
},
},
totalTime: process.hrtime(startTime),
};
};
export const build: ViteBuilder['build'] = async ({ options }) => {
return viteBuild(options as Options);
};
export const corePresets = [import.meta.resolve('@storybook/builder-vite/preset')];