Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build: Write and store esbuild metafiles #29117

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
2806b81
move metafiles
JReinhold Sep 13, 2024
568d96d
use bench-dir for esbuild metafile, fix addon-test collision
JReinhold Sep 13, 2024
1439a0d
fix mixing nx project names
JReinhold Sep 13, 2024
bc838f2
store esbuild metafiles in circleci artifacts
JReinhold Sep 13, 2024
f5537b9
add analyzed metafiles to the output
JReinhold Sep 16, 2024
c5c640f
save metafiles from addons
JReinhold Sep 16, 2024
1f25aaf
add bench story that visualizes metafiles
JReinhold Sep 16, 2024
8af039d
Merge branch 'next' into 29105-save-esbuild-metafiles-and-make-them-a…
JReinhold Sep 16, 2024
1ef0911
cleanup
JReinhold Sep 16, 2024
5fd2058
Merge branch '29105-save-esbuild-metafiles-and-make-them-available-fr…
JReinhold Sep 16, 2024
2649625
persist bench dir between CI jobs
JReinhold Sep 17, 2024
4662d2f
prevent filename collision in core metafiles
JReinhold Sep 17, 2024
a130700
Merge branch 'next' of github.com:storybookjs/storybook into 29105-sa…
JReinhold Sep 17, 2024
6f75f30
Merge branch 'next' into 29105-save-esbuild-metafiles-and-make-them-a…
JReinhold Sep 19, 2024
c376578
fix create-storybook package name in bench stories
JReinhold Sep 21, 2024
5bf9baa
Merge branch '29105-save-esbuild-metafiles-and-make-them-available-fr…
JReinhold Sep 21, 2024
13a9574
merge all metafiles per package, name files after package names inste…
JReinhold Sep 24, 2024
12db3ed
strip @storybook from metafiles
JReinhold Sep 25, 2024
9bf3117
Update .circleci/config.yml
JReinhold Sep 25, 2024
f02e568
Merge branch '29105-save-esbuild-metafiles-and-make-them-available-fr…
JReinhold Sep 25, 2024
173de14
move metafiles to code, so nx can cache it properly
JReinhold Sep 25, 2024
24b85d4
rename nx projects to match package names
JReinhold Sep 25, 2024
ae0d306
don't eagerly import all metafiles in story
JReinhold Sep 25, 2024
18f8cc1
fix bench path in circleci
JReinhold Sep 25, 2024
0266e9e
Merge branch 'next' into 29105-save-esbuild-metafiles-and-make-them-a…
JReinhold Sep 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ jobs:
cd code
yarn local-registry --publish
- report-workflow-on-failure
- store_artifacts:
path: code/bench/esbuild-metafiles
- save_cache:
name: Save Yarn cache
key: build-yarn-2-cache-v4--{{ checksum "code/yarn.lock" }}--{{ checksum "scripts/yarn.lock" }}
Expand All @@ -164,6 +166,7 @@ jobs:
paths:
- code/node_modules
- scripts/node_modules
- code/bench
- code/examples
- code/node_modules
- code/addons
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ test-results
/repros
/sandbox
/bench
/code/bench
.verdaccio-cache
.next
/.npmrc
Expand Down
89 changes: 89 additions & 0 deletions code/.storybook/bench.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import React from 'react';

import type { Meta } from '@storybook/react';

// @ts-expect-error - TS doesn't know about import.meta.glob from Vite
JReinhold marked this conversation as resolved.
Show resolved Hide resolved
const allMetafiles = import.meta.glob([
'../bench/esbuild-metafiles/**/*.json',
// the core metafile is too big to be loaded automatically in the iframe
'!../bench/esbuild-metafiles/core/core.json',
]);

const METAFILES_DIR = '../bench/esbuild-metafiles/';
const PACKAGES_WITHOUT_ORG = ['storybook', 'sb', 'create-storybook'];

// allows the metafile path to be used in the URL hash
const safeMetafileArg = (path: string) =>
path
.replace(METAFILES_DIR, '')
.replaceAll('/', '__')
.replace(/(\w*).json/, '$1');

export default {
title: 'Bench',
parameters: {
layout: 'fullscreen',
chromatic: { disableSnapshot: true },
},
args: {
metafile: safeMetafileArg(Object.keys(allMetafiles)[0]),
},
argTypes: {
metafile: {
options: Object.keys(allMetafiles).concat('core - core').map(safeMetafileArg).sort(),
mapping: Object.fromEntries(
Object.keys(allMetafiles).map((path) => [safeMetafileArg(path), path])
),
control: {
type: 'select',
labels: Object.fromEntries(
Object.keys(allMetafiles)
.map((path) => {
const [, dirName, subEntry] = /esbuild-metafiles\/(.+)\/(.+).json/.exec(path)!;
const pkgName = PACKAGES_WITHOUT_ORG.includes(dirName)
? dirName
: `@storybook/${dirName}`;

return [
safeMetafileArg(path),
subEntry !== 'metafile' ? `${pkgName} - ${subEntry}` : pkgName,
];
})
.concat([['core - core', '@storybook/core - core - TOO BIG PLEASE UPLOAD MANUALLY']])
),
},
},
},
loaders: [
async ({ args }) => {
if (!args.metafile) {
return;
}
let metafile;
try {
metafile = await allMetafiles[args.metafile]();
} catch (e) {
return;
}
const encodedMetafile = btoa(JSON.stringify(metafile));
return { encodedMetafile };
},
],
render: (args, { loaded }) => {
const { encodedMetafile = '' } = loaded ?? {};

return (
<iframe
// esbuild analyzer has a hidden feature to load a base64-encoded metafile from the the URL hash
// see https://github.com/esbuild/esbuild.github.io/blob/ccf70086543a034495834b4135e15e91a3ffceb8/src/analyze/index.ts#L113-L116
src={`https://esbuild.github.io/analyze/#${encodedMetafile}`}
style={{ border: 'none', width: '100%', height: '100vh' }}
key={args.metafile} // force re-render on args change
/>
);
},
} satisfies Meta;

export const ESBuildAnalyzer = {
name: 'ESBuild Metafiles',
};
1 change: 1 addition & 0 deletions code/.storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const managerApiPath = join(__dirname, '../core/src/manager-api');

const config: StorybookConfig = {
stories: [
'./*.stories.@(js|jsx|ts|tsx)',
{
directory: '../core/template/stories',
titlePrefix: 'core',
Expand Down
2 changes: 1 addition & 1 deletion code/addons/a11y/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "a11y",
"name": "addon-a11y",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"projectType": "library",
"targets": {
Expand Down
2 changes: 1 addition & 1 deletion code/addons/actions/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "actions",
"name": "addon-actions",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"projectType": "library",
"targets": {
Expand Down
2 changes: 1 addition & 1 deletion code/addons/backgrounds/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "backgrounds",
"name": "addon-backgrounds",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"projectType": "library",
"targets": {
Expand Down
2 changes: 1 addition & 1 deletion code/addons/controls/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "controls",
"name": "addon-controls",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"projectType": "library",
"targets": {
Expand Down
2 changes: 1 addition & 1 deletion code/addons/docs/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "docs",
"name": "addon-docs",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"projectType": "library",
"targets": {
Expand Down
2 changes: 1 addition & 1 deletion code/addons/essentials/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "essentials",
"name": "addon-essentials",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"projectType": "library",
"targets": {
Expand Down
2 changes: 1 addition & 1 deletion code/addons/gfm/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "gfm",
"name": "addon-gfm",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"projectType": "library",
"targets": {
Expand Down
2 changes: 1 addition & 1 deletion code/addons/highlight/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "highlight",
"name": "addon-highlight",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"projectType": "library",
"targets": {
Expand Down
2 changes: 1 addition & 1 deletion code/addons/interactions/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "interactions",
"name": "addon-interactions",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"projectType": "library",
"targets": {
Expand Down
2 changes: 1 addition & 1 deletion code/addons/jest/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "jest",
"name": "addon-jest",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"projectType": "library",
"targets": {
Expand Down
2 changes: 1 addition & 1 deletion code/addons/links/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "links",
"name": "addon-links",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"projectType": "library",
"targets": {
Expand Down
2 changes: 1 addition & 1 deletion code/addons/measure/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "measure",
"name": "addon-measure",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"projectType": "library",
"targets": {
Expand Down
2 changes: 1 addition & 1 deletion code/addons/onboarding/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "onboarding",
"name": "addon-onboarding",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"projectType": "library",
"targets": {
Expand Down
2 changes: 1 addition & 1 deletion code/addons/outline/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "outline",
"name": "addon-outline",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"projectType": "library",
"targets": {
Expand Down
2 changes: 1 addition & 1 deletion code/addons/storysource/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "storysource",
"name": "addon-storysource",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"projectType": "library",
"targets": {
Expand Down
2 changes: 1 addition & 1 deletion code/addons/test/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "addon-test",
"name": "experimental-addon-test",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"projectType": "library",
"targets": {
Expand Down
2 changes: 1 addition & 1 deletion code/addons/themes/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "themes",
"name": "addon-themes",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"targets": {
"build": {}
Expand Down
2 changes: 1 addition & 1 deletion code/addons/toolbars/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "toolbars",
"name": "addon-toolbars",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"projectType": "library",
"targets": {
Expand Down
2 changes: 1 addition & 1 deletion code/addons/viewport/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "viewport",
"name": "addon-viewport",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"projectType": "library",
"targets": {
Expand Down
71 changes: 52 additions & 19 deletions code/core/scripts/prep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { existsSync, mkdirSync, watch } from 'node:fs';
import { mkdir, rm, writeFile } from 'node:fs/promises';
import { dirname, join } from 'node:path';

import type { Metafile } from 'esbuild';

import {
chalk,
dedent,
Expand Down Expand Up @@ -306,27 +308,58 @@ async function run() {
console.log(`compiled ${chalk.cyan(filename)}`);
});
} else {
await Promise.all(
compile.map(async (context, index) => {
const out = await context.rebuild();
// repo root/bench/esbuild-metafiles/core
const metafilesDir = join(__dirname, '..', '..', 'bench', 'esbuild-metafiles', 'core');
if (existsSync(metafilesDir)) {
await rm(metafilesDir, { recursive: true });
}
ndelangen marked this conversation as resolved.
Show resolved Hide resolved
await mkdir(metafilesDir, { recursive: true });

const outputs = await Promise.all(
compile.map(async (context) => {
const output = await context.rebuild();
await context.dispose();

if (out.metafile) {
const { outputs } = out.metafile;
const keys = Object.keys(outputs);
const format = keys.every((key) => key.endsWith('.js')) ? 'esm' : 'cjs';
const outName =
keys.length === 1 ? dirname(keys[0]).replace('dist/', '') : `meta-${format}-${index}`;

if (!existsSync('report')) {
mkdirSync('report');
}
await writeFile(`report/${outName}.json`, JSON.stringify(out.metafile, null, 2));
await writeFile(
`report/${outName}.txt`,
await esbuild.analyzeMetafile(out.metafile, { color: false, verbose: false })
);
}
return output;
})
);

const metafileByModule: Record<string, Metafile> = {};

for (const currentOutput of outputs) {
if (!currentOutput.metafile) {
continue;
}

const keys = Object.keys(currentOutput.metafile.outputs);
const moduleName = keys.length === 1 ? dirname(keys[0]).replace('dist/', '') : 'core';

const existingMetafile = metafileByModule[moduleName];

if (existingMetafile) {
existingMetafile.inputs = {
...existingMetafile.inputs,
...currentOutput.metafile.inputs,
};
existingMetafile.outputs = {
...existingMetafile.outputs,
...currentOutput.metafile.outputs,
};
} else {
metafileByModule[moduleName] = currentOutput.metafile;
}
}

await Promise.all(
Object.entries(metafileByModule).map(async ([moduleName, metafile]) => {
await writeFile(
join(metafilesDir, `${moduleName}.json`),
JSON.stringify(metafile, null, 2)
);
await writeFile(
join(metafilesDir, `${moduleName}.txt`),
await esbuild.analyzeMetafile(metafile, { color: false, verbose: false })
);
})
);
}
Expand Down
2 changes: 1 addition & 1 deletion code/lib/cli-sb/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "cli-sb",
"name": "sb",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"projectType": "library",
"targets": {}
Expand Down
2 changes: 1 addition & 1 deletion code/lib/cli-storybook/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "cli-storybook",
"name": "cli",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"projectType": "library",
"targets": {
Expand Down
2 changes: 1 addition & 1 deletion code/lib/cli/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "cli",
"name": "storybook",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"projectType": "library",
"targets": {
Expand Down
1 change: 1 addition & 0 deletions code/lib/instrumenter/project.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "instrumenter",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"projectType": "library",
"targets": {
Expand Down
1 change: 1 addition & 0 deletions code/lib/test/project.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "test",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"projectType": "library",
"targets": {
Expand Down
2 changes: 1 addition & 1 deletion code/nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"{workspaceRoot}/../scripts/prepare/{bundle,addon-bundle,esm-bundle}.ts"
],
"dependsOn": ["^build"],
"outputs": ["{projectRoot}/dist"],
"outputs": ["{projectRoot}/dist", "{workspaceRoot}/bench/esbuild-metafiles/{projectName}"],
"cache": true
},
"test": {
Expand Down
Loading