|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import type { Meta } from '@storybook/react'; |
| 4 | + |
| 5 | +// @ts-expect-error - TS doesn't know about import.meta.glob from Vite |
| 6 | +const allMetafiles = import.meta.glob([ |
| 7 | + '../bench/esbuild-metafiles/**/*.json', |
| 8 | + // the core metafile is too big to be loaded automatically in the iframe |
| 9 | + '!../bench/esbuild-metafiles/core/core.json', |
| 10 | +]); |
| 11 | + |
| 12 | +const METAFILES_DIR = '../bench/esbuild-metafiles/'; |
| 13 | +const PACKAGES_WITHOUT_ORG = ['storybook', 'sb', 'create-storybook']; |
| 14 | + |
| 15 | +// allows the metafile path to be used in the URL hash |
| 16 | +const safeMetafileArg = (path: string) => |
| 17 | + path |
| 18 | + .replace(METAFILES_DIR, '') |
| 19 | + .replaceAll('/', '__') |
| 20 | + .replace(/(\w*).json/, '$1'); |
| 21 | + |
| 22 | +export default { |
| 23 | + title: 'Bench', |
| 24 | + parameters: { |
| 25 | + layout: 'fullscreen', |
| 26 | + chromatic: { disableSnapshot: true }, |
| 27 | + }, |
| 28 | + args: { |
| 29 | + metafile: safeMetafileArg(Object.keys(allMetafiles)[0]), |
| 30 | + }, |
| 31 | + argTypes: { |
| 32 | + metafile: { |
| 33 | + options: Object.keys(allMetafiles).concat('core - core').map(safeMetafileArg).sort(), |
| 34 | + mapping: Object.fromEntries( |
| 35 | + Object.keys(allMetafiles).map((path) => [safeMetafileArg(path), path]) |
| 36 | + ), |
| 37 | + control: { |
| 38 | + type: 'select', |
| 39 | + labels: Object.fromEntries( |
| 40 | + Object.keys(allMetafiles) |
| 41 | + .map((path) => { |
| 42 | + const [, dirName, subEntry] = /esbuild-metafiles\/(.+)\/(.+).json/.exec(path)!; |
| 43 | + const pkgName = PACKAGES_WITHOUT_ORG.includes(dirName) |
| 44 | + ? dirName |
| 45 | + : `@storybook/${dirName}`; |
| 46 | + |
| 47 | + return [ |
| 48 | + safeMetafileArg(path), |
| 49 | + subEntry !== 'metafile' ? `${pkgName} - ${subEntry}` : pkgName, |
| 50 | + ]; |
| 51 | + }) |
| 52 | + .concat([['core - core', '@storybook/core - core - TOO BIG PLEASE UPLOAD MANUALLY']]) |
| 53 | + ), |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + loaders: [ |
| 58 | + async ({ args }) => { |
| 59 | + if (!args.metafile) { |
| 60 | + return; |
| 61 | + } |
| 62 | + let metafile; |
| 63 | + try { |
| 64 | + metafile = await allMetafiles[args.metafile](); |
| 65 | + } catch (e) { |
| 66 | + return; |
| 67 | + } |
| 68 | + const encodedMetafile = btoa(JSON.stringify(metafile)); |
| 69 | + return { encodedMetafile }; |
| 70 | + }, |
| 71 | + ], |
| 72 | + render: (args, { loaded }) => { |
| 73 | + const { encodedMetafile = '' } = loaded ?? {}; |
| 74 | + |
| 75 | + return ( |
| 76 | + <iframe |
| 77 | + // esbuild analyzer has a hidden feature to load a base64-encoded metafile from the the URL hash |
| 78 | + // see https://github.com/esbuild/esbuild.github.io/blob/ccf70086543a034495834b4135e15e91a3ffceb8/src/analyze/index.ts#L113-L116 |
| 79 | + src={`https://esbuild.github.io/analyze/#${encodedMetafile}`} |
| 80 | + style={{ border: 'none', width: '100%', height: '100vh' }} |
| 81 | + key={args.metafile} // force re-render on args change |
| 82 | + /> |
| 83 | + ); |
| 84 | + }, |
| 85 | +} satisfies Meta; |
| 86 | + |
| 87 | +export const ESBuildAnalyzer = { |
| 88 | + name: 'ESBuild Metafiles', |
| 89 | +}; |
0 commit comments