From 45e597f9b3450d5ffa7a9e25a0a77b869590b250 Mon Sep 17 00:00:00 2001 From: Norbert de Langen Date: Tue, 24 Sep 2024 15:40:33 +0200 Subject: [PATCH 1/3] set react in prod mode & bundle metafiles --- code/core/scripts/prep.ts | 53 +++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/code/core/scripts/prep.ts b/code/core/scripts/prep.ts index 20c979ebd9aa..16578658536b 100644 --- a/code/core/scripts/prep.ts +++ b/code/core/scripts/prep.ts @@ -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: [], }) @@ -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>((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 }) + ); }) ); } From 23134a2d7e8f127eb5f441aee29cb0b963328e01 Mon Sep 17 00:00:00 2001 From: Norbert de Langen Date: Wed, 25 Sep 2024 11:14:05 +0200 Subject: [PATCH 2/3] fix build --- code/core/scripts/prep.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/code/core/scripts/prep.ts b/code/core/scripts/prep.ts index 16578658536b..b844f1013c9c 100644 --- a/code/core/scripts/prep.ts +++ b/code/core/scripts/prep.ts @@ -1,5 +1,5 @@ /* eslint-disable local-rules/no-uncategorized-errors */ -import { existsSync, mkdirSync, watch } from 'node:fs'; +import { existsSync, watch } from 'node:fs'; import { mkdir, rm, writeFile } from 'node:fs/promises'; import { dirname, join } from 'node:path'; @@ -341,6 +341,10 @@ async function run() { return acc; }, {}); + if (!existsSync(join(cwd, 'report'))) { + await mkdir('report'); + } + await Promise.all( Object.entries(grouped).map(async ([outName, metafile]) => { await writeFile(`report/${outName}.json`, JSON.stringify(metafile, null, 2)); From 56a312e2886043ae0b3e4645b23f803cd98e045b Mon Sep 17 00:00:00 2001 From: Jeppe Reinhold Date: Wed, 25 Sep 2024 22:11:55 +0200 Subject: [PATCH 3/3] revert metafile changes --- code/core/scripts/prep.ts | 51 +++++++++++++-------------------------- 1 file changed, 17 insertions(+), 34 deletions(-) diff --git a/code/core/scripts/prep.ts b/code/core/scripts/prep.ts index b844f1013c9c..f1edbe307d1e 100644 --- a/code/core/scripts/prep.ts +++ b/code/core/scripts/prep.ts @@ -1,5 +1,5 @@ /* eslint-disable local-rules/no-uncategorized-errors */ -import { existsSync, watch } from 'node:fs'; +import { existsSync, mkdirSync, watch } from 'node:fs'; import { mkdir, rm, writeFile } from 'node:fs/promises'; import { dirname, join } from 'node:path'; @@ -314,44 +314,27 @@ async function run() { console.log(`compiled ${chalk.cyan(filename)}`); }); } else { - const outs = await Promise.all( - compile.map(async (context) => { + await Promise.all( + compile.map(async (context, index) => { const out = await context.rebuild(); await context.dispose(); - return out; - }) - ); - - const grouped = outs.reduce>((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 (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 (acc[outName]) { - // merge results - acc[outName].inputs = { ...acc[outName].inputs, ...inputs }; - acc[outName].outputs = { ...acc[outName].outputs, ...outputs }; - } else { - acc[outName] = out.metafile; + 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 acc; - }, {}); - - if (!existsSync(join(cwd, 'report'))) { - await mkdir('report'); - } - - 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 }) - ); }) ); }