Skip to content

Commit

Permalink
Merge pull request #28831 from storybookjs/norbert/final-bun-removal
Browse files Browse the repository at this point in the history
Build: Use `jiti` for building `core`
  • Loading branch information
ndelangen authored Aug 7, 2024
2 parents bbfdf58 + f438676 commit 8e51bf9
Show file tree
Hide file tree
Showing 22 changed files with 401 additions and 559 deletions.
7 changes: 0 additions & 7 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ orbs:
browser-tools: circleci/[email protected]
discord: antonioned/[email protected]
codecov: codecov/[email protected]
bun-orb: cmgriffing/[email protected]
node: circleci/[email protected]
nx: nrwl/[email protected]

Expand Down Expand Up @@ -139,8 +138,6 @@ jobs:
steps:
- git-shallow-clone/checkout_advanced:
clone_options: "--depth 1 --verbose"
- bun-orb/setup:
version: 1.1.1
- restore_cache:
name: Restore Yarn cache
keys:
Expand Down Expand Up @@ -202,8 +199,6 @@ jobs:
steps:
- git-shallow-clone/checkout_advanced:
clone_options: "--depth 1 --verbose"
- bun-orb/setup:
version: 1.1.1
- attach_workspace:
at: .
- nx/set-shas:
Expand Down Expand Up @@ -353,8 +348,6 @@ jobs:
steps:
- git-shallow-clone/checkout_advanced:
clone_options: "--depth 1 --verbose"
- bun-orb/setup:
version: 1.1.1
- attach_workspace:
at: .
- run:
Expand Down
4 changes: 0 additions & 4 deletions .github/workflows/canary-release-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ jobs:
with:
node-version-file: ".nvmrc"

- uses: oven-sh/setup-bun@v1
with:
bun-version: 1.1.1

- name: Cache dependencies
uses: actions/cache@v4
with:
Expand Down
8 changes: 0 additions & 8 deletions .github/workflows/generate-sandboxes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ jobs:
- uses: actions/checkout@v4
with:
ref: next

- uses: oven-sh/setup-bun@v1
with:
bun-version: 1.1.1

- uses: actions/setup-node@v4
with:
Expand Down Expand Up @@ -86,10 +82,6 @@ jobs:
with:
ref: main

- uses: oven-sh/setup-bun@v1
with:
bun-version: 1.1.1

- uses: actions/setup-node@v4
with:
node-version-file: ".nvmrc"
Expand Down
4 changes: 0 additions & 4 deletions .github/workflows/prepare-non-patch-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ jobs:
with:
node-version-file: ".nvmrc"

- uses: oven-sh/setup-bun@v1
with:
bun-version: 1.1.1

- name: Cache dependencies
uses: actions/cache@v4
with:
Expand Down
4 changes: 0 additions & 4 deletions .github/workflows/prepare-patch-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ jobs:
with:
node-version-file: ".nvmrc"

- uses: oven-sh/setup-bun@v1
with:
bun-version: 1.1.1

- name: Cache dependencies
uses: actions/cache@v4
with:
Expand Down
4 changes: 0 additions & 4 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ jobs:
with:
node-version-file: ".nvmrc"

- uses: oven-sh/setup-bun@v1
with:
bun-version: 1.1.1

- name: Cache dependencies
uses: actions/cache@v4
with:
Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/tests-unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v1
with:
bun-version: 1.1.1
- name: Set node version
uses: actions/setup-node@v4
with:
Expand Down
1 change: 0 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ You will need to have the following installed:
- git
- node
- yarn
- [bun](https://bun.sh/)

## Using fnm as a Node version manager

Expand Down
4 changes: 2 additions & 2 deletions code/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@
"!src/**/*"
],
"scripts": {
"check": "bun ./scripts/check.ts",
"prep": "bun ./scripts/prep.ts"
"check": "jiti ./scripts/check.ts",
"prep": "jiti ./scripts/prep.ts"
},
"dependencies": {
"@storybook/csf": "^0.1.11",
Expand Down
93 changes: 49 additions & 44 deletions code/core/scripts/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,52 @@ import { process, dts, nodeInternals } from '../../../scripts/prepare/tools';
import { getEntries } from './entries';
import pkg from '../package.json';

const cwd = process.cwd();

const flags = process.argv.slice(2);

const selection = flags[0] || 'all';

const entries = getEntries(cwd);
const external = [
...Object.keys((pkg as any).dependencies || {}),
...Object.keys((pkg as any).peerDependencies || {}),
...nodeInternals,
'typescript',
'@storybook/core',

'@storybook/core/builder-manager',
'@storybook/core/channels',
'@storybook/core/client-logger',
'@storybook/core/common',
'@storybook/core/components',
'@storybook/core/core-events',
'@storybook/core/core-server',
'@storybook/core/csf-tools',
'@storybook/core/docs-tools',
'@storybook/core/manager-api',
'@storybook/core/node-logger',
'@storybook/core/preview-api',
'@storybook/core/router',
'@storybook/core/telemetry',
'@storybook/core/theming',
'@storybook/core/types',
];

const all = entries.filter((e) => e.dts);
const list = selection === 'all' ? all : [all[Number(selection)]];

await Promise.all(
list.map(async (i) => {
await dts(
i.file,
[...external, ...i.externals],
join(import.meta.dirname, '..', 'tsconfig.json')
);
})
);
async function run() {
const cwd = process.cwd();

const flags = process.argv.slice(2);

const selection = flags[0] || 'all';

const entries = getEntries(cwd);
const external = [
...Object.keys((pkg as any).dependencies || {}),
...Object.keys((pkg as any).peerDependencies || {}),
...nodeInternals,
'typescript',
'@storybook/core',

'@storybook/core/builder-manager',
'@storybook/core/channels',
'@storybook/core/client-logger',
'@storybook/core/common',
'@storybook/core/components',
'@storybook/core/core-events',
'@storybook/core/core-server',
'@storybook/core/csf-tools',
'@storybook/core/docs-tools',
'@storybook/core/manager-api',
'@storybook/core/node-logger',
'@storybook/core/preview-api',
'@storybook/core/router',
'@storybook/core/telemetry',
'@storybook/core/theming',
'@storybook/core/types',
];

const all = entries.filter((e) => e.dts);
const list = selection === 'all' ? all : [all[Number(selection)]];

console.log('Generating d.ts files for', list.map((i) => i.file).join(', '));

await Promise.all(
list.map(async (i) => {
await dts(i.file, [...external, ...i.externals], join(__dirname, '..', 'tsconfig.json'));
})
);
}

run().catch((e) => {
process.stderr.write(e.toString());
process.exit(1);
});
3 changes: 2 additions & 1 deletion code/core/scripts/helpers/dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { readJson } from 'fs-extra';
import { join } from 'node:path';

export async function flattenDependencies(
Expand All @@ -16,7 +17,7 @@ export async function flattenDependencies(
console.log(dep + ' not found');
return;
}
const { dependencies = {}, peerDependencies = {} } = await Bun.file(path).json();
const { dependencies = {}, peerDependencies = {} } = await readJson(path);
const all: string[] = [
...new Set([...Object.keys(dependencies), ...Object.keys(peerDependencies)]),
]
Expand Down
8 changes: 5 additions & 3 deletions code/core/scripts/helpers/generatePackageJsonFile.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { join, relative } from 'node:path';
import slash from 'slash';
import { sortPackageJson, Bun } from '../../../../scripts/prepare/tools';
import { sortPackageJson } from '../../../../scripts/prepare/tools';
import type { getEntries } from '../entries';
import { readJSON } from 'fs-extra';
import { writeFile } from 'node:fs/promises';

const cwd = process.cwd();

export async function generatePackageJsonFile(entries: ReturnType<typeof getEntries>) {
const location = join(cwd, 'package.json');
const pkgJson = await Bun.file(location).json();
const pkgJson = await readJSON(location);

/** Re-create the `exports` field in `code/core/package.json`
* This way we only need to update the `./scripts/entries.ts` file to ensure all things we create actually exist and are mapped to the correct path.
Expand Down Expand Up @@ -67,5 +69,5 @@ export async function generatePackageJsonFile(entries: ReturnType<typeof getEntr
},
};

await Bun.write(location, `${sortPackageJson(JSON.stringify(pkgJson, null, 2))}\n`, {});
await writeFile(location, `${sortPackageJson(JSON.stringify(pkgJson, null, 2))}\n`, {});
}
46 changes: 31 additions & 15 deletions code/core/scripts/helpers/generateTypesFiles.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { relative } from 'node:path';
import { process, chalk, limit, Bun } from '../../../../scripts/prepare/tools';
import { join, relative } from 'node:path';
import { spawn } from '../../../../scripts/prepare/tools';
import { process, chalk, limit } from '../../../../scripts/prepare/tools';
import type { getEntries } from '../entries';

export async function generateTypesFiles(
Expand All @@ -14,38 +15,53 @@ export async function generateTypesFiles(
// ...this way we do not bog down the main process/esbuild and can run them in parallel
// we limit the number of concurrent processes to 3, because we don't want to overload the host machine
// by trial and error, 3 seems to be the sweet spot between perf and consistency
const limited = limit(3);
let processes: ReturnType<(typeof Bun)['spawn']>[] = [];
const limited = limit(10);
let processes: ReturnType<typeof spawn>[] = [];

await Promise.all(
dtsEntries.map(async (fileName, index) => {
return limited(async () => {
const getDtsProcess = () =>
Bun.spawn(['bun', './scripts/dts.ts', index.toString()], {
cwd,
stdio: ['ignore', 'pipe', 'inherit'],
});
spawn(
join(__dirname, '../../../../scripts/node_modules/.bin/jiti'),
['./scripts/dts.ts', index.toString()],
{
cwd,
stdio: ['ignore', 'pipe', 'inherit'],
}
);
let timer: ReturnType<typeof setTimeout> | undefined;
let dtsProcess = getDtsProcess();
const dtsProcess = getDtsProcess();
processes.push(dtsProcess);

await Promise.race([
dtsProcess.exited.catch(async () => {
await dtsProcess.kill();
dtsProcess = getDtsProcess();
return dtsProcess.exited;
new Promise((resolve) => {
dtsProcess.on('exit', () => {
resolve(void 0);
});
dtsProcess.on('error', () => {
resolve(void 0);
});
dtsProcess.on('close', () => {
resolve(void 0);
});
}),
new Promise((_, reject) => {
new Promise((resolve) => {
timer = setTimeout(() => {
console.log(index, fileName);

reject(new Error('timed out'));
dtsProcess.kill(408); // timed out
resolve(void 0);
}, 60000);
}),
]);

if (timer) {
clearTimeout(timer);
}

if (dtsProcess.exitCode !== 0) {
console.log(dtsProcess.exitCode);
// If any fail, kill all the other processes and exit (bail)
processes.forEach((p) => p.kill());
processes = [];
Expand Down
16 changes: 8 additions & 8 deletions code/core/scripts/helpers/generateTypesMapperFiles.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Bun } from '../../../../scripts/prepare/tools';
import type { getEntries } from '../entries';
import { join, relative } from 'node:path';
import { dedent } from '../../../../scripts/prepare/tools';
import { writeFile } from 'node:fs/promises';
import { ensureFile } from 'fs-extra';

const cwd = process.cwd();

Expand All @@ -10,7 +11,7 @@ async function generateTypesMapperContent(filePath: string) {
const downwards = relative(cwd, filePath);

return dedent`
// auto generated file from ${import.meta.filename}, do not edit
// auto generated file from ${__filename}, do not edit
export * from '${join(upwards, downwards)}';
export type * from '${join(upwards, downwards)}';
`;
Expand All @@ -27,11 +28,10 @@ export async function generateTypesMapperFiles(entries: ReturnType<typeof getEnt
const all = entries.filter((e) => e.dts).map((e) => e.file);

await Promise.all(
all.map(async (filePath) =>
Bun.write(
filePath.replace('src', 'dist').replace(/\.tsx?/, '.d.ts'),
await generateTypesMapperContent(filePath)
)
)
all.map(async (filePath) => {
const location = filePath.replace('src', 'dist').replace(/\.tsx?/, '.d.ts');
await ensureFile(location);
await writeFile(location, await generateTypesMapperContent(filePath));
})
);
}
Loading

0 comments on commit 8e51bf9

Please sign in to comment.