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

Optimize: Production-mode react in manager #29197

Open
wants to merge 2 commits into
base: next
Choose a base branch
from
Open
Changes from 1 commit
Commits
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
53 changes: 37 additions & 16 deletions code/core/scripts/prep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@ async function run() {
'@storybook/core': join(cwd, 'src'),
react: dirname(require.resolve('react/package.json')),
'react-dom': dirname(require.resolve('react-dom/package.json')),
'react-dom/client': join(
dirname(require.resolve('react-dom/package.json')),
'client'
),
},
define: {
// This should set react in prod mode for the manager
'process.env.NODE_ENV': JSON.stringify('production'),
},
external: [],
})
Expand Down Expand Up @@ -306,27 +314,40 @@ async function run() {
console.log(`compiled ${chalk.cyan(filename)}`);
});
} else {
await Promise.all(
compile.map(async (context, index) => {
const outs = await Promise.all(
compile.map(async (context) => {
const out = 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}`;
return out;
})
);

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 })
);
const grouped = outs.reduce<Record<string, esbuild.Metafile>>((acc, out, index) => {
if (out.metafile) {
const { outputs, inputs } = out.metafile;
const keys = Object.keys(outputs);
const outName = keys.length === 1 ? dirname(keys[0]).replace('dist/', '') : `meta`;

if (acc[outName]) {
// merge results
acc[outName].inputs = { ...acc[outName].inputs, ...inputs };
acc[outName].outputs = { ...acc[outName].outputs, ...outputs };
} else {
acc[outName] = out.metafile;
}
}

return acc;
}, {});

await Promise.all(
Object.entries(grouped).map(async ([outName, metafile]) => {
await writeFile(`report/${outName}.json`, JSON.stringify(metafile, null, 2));
await writeFile(
`report/${outName}.txt`,
await esbuild.analyzeMetafile(metafile, { color: false, verbose: false })
);
})
);
}
Expand Down
Loading