Skip to content

Commit be1b97e

Browse files
authored
Merge pull request #29117 from storybookjs/29105-save-esbuild-metafiles-and-make-them-available-from-ci
Build: Write and store esbuild metafiles
2 parents 71f2da5 + 0266e9e commit be1b97e

File tree

73 files changed

+577
-334
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+577
-334
lines changed

.circleci/config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ jobs:
154154
cd code
155155
yarn local-registry --publish
156156
- report-workflow-on-failure
157+
- store_artifacts:
158+
path: code/bench/esbuild-metafiles
157159
- save_cache:
158160
name: Save Yarn cache
159161
key: build-yarn-2-cache-v4--{{ checksum "code/yarn.lock" }}--{{ checksum "scripts/yarn.lock" }}
@@ -164,6 +166,7 @@ jobs:
164166
paths:
165167
- code/node_modules
166168
- scripts/node_modules
169+
- code/bench
167170
- code/examples
168171
- code/node_modules
169172
- code/addons

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ test-results
1212
/repros
1313
/sandbox
1414
/bench
15+
/code/bench
1516
.verdaccio-cache
1617
.next
1718
/.npmrc

code/.storybook/bench.stories.tsx

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
};

code/.storybook/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const managerApiPath = join(__dirname, '../core/src/manager-api');
77

88
const config: StorybookConfig = {
99
stories: [
10+
'./*.stories.@(js|jsx|ts|tsx)',
1011
{
1112
directory: '../core/template/stories',
1213
titlePrefix: 'core',

code/addons/a11y/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "a11y",
2+
"name": "addon-a11y",
33
"$schema": "../../node_modules/nx/schemas/project-schema.json",
44
"projectType": "library",
55
"targets": {

code/addons/actions/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "actions",
2+
"name": "addon-actions",
33
"$schema": "../../node_modules/nx/schemas/project-schema.json",
44
"projectType": "library",
55
"targets": {

code/addons/backgrounds/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "backgrounds",
2+
"name": "addon-backgrounds",
33
"$schema": "../../node_modules/nx/schemas/project-schema.json",
44
"projectType": "library",
55
"targets": {

code/addons/controls/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "controls",
2+
"name": "addon-controls",
33
"$schema": "../../node_modules/nx/schemas/project-schema.json",
44
"projectType": "library",
55
"targets": {

code/addons/docs/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "docs",
2+
"name": "addon-docs",
33
"$schema": "../../node_modules/nx/schemas/project-schema.json",
44
"projectType": "library",
55
"targets": {

code/addons/essentials/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "essentials",
2+
"name": "addon-essentials",
33
"$schema": "../../node_modules/nx/schemas/project-schema.json",
44
"projectType": "library",
55
"targets": {

0 commit comments

Comments
 (0)