Skip to content

Commit

Permalink
Switch to bun for most scripts.
Browse files Browse the repository at this point in the history
We're not quite ready to switch to `bun` for the entire project yet. In particular, we still rely on `npm` and `package-lock.json` for dependency installation.
For remaining blockers, see: #294
  • Loading branch information
lgarron committed Sep 17, 2023
1 parent 504211b commit 9233708
Show file tree
Hide file tree
Showing 29 changed files with 987 additions and 31 deletions.
19 changes: 14 additions & 5 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ jobs:
node-version: [19.x]

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v1
with:
bun-version: latest
- name: Use `node` v${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
Expand All @@ -33,7 +36,10 @@ jobs:
node-version: [19.x]

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v1
with:
bun-version: latest
- name: Use `node` v${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
Expand All @@ -54,7 +60,10 @@ jobs:
node-version: [19.x]

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v1
with:
bun-version: latest
- name: Use `node` v${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
Expand All @@ -73,11 +82,11 @@ jobs:
- run: make test-dist-sites-experiments
- run: make clean

test-using-bun:
test-using-bun-install:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v1
with:
bun-version: latest
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
/dist
/node_modules

# Stick with `package-lock.json` for now.
/bun.lockb

# Created by `npm pack` by default.
/*.tgz

Expand Down
39 changes: 21 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# TODO: see if we can make everything compatible with `bun`
NODE=node
BIOME=npx @biomejs/biome
NPX=npx
BUN_RUN=bun run
BUN_BUN_RUN=bun --bun run
BIOME=bun x @biomejs/biome
WEB_TEST_RUNNER=./node_modules/.bin/wtr

.PHONY: default
Expand Down Expand Up @@ -32,39 +35,39 @@ build: clean build-lib build-bin build-sites
build-lib: build-lib-js build-lib-types
.PHONY: build-lib-js
build-lib-js:
${NODE} ./script/build/lib/build-lib-js.js
${BUN_RUN} ./script/build/lib/build-lib-js.ts
.PHONY: build-lib-types
build-lib-types:
${NODE} ./script/build/lib/build-lib-types.js
${BUN_RUN} ./script/build/lib/build-lib-types.ts
.PHONY: build-bin
build-bin:
${NODE} ./script/build/bin/build-bin.js
${BUN_RUN} ./script/build/bin/build-bin.ts
chmod +x ./dist/bin/*.js
.PHONY: build-sites
build-sites: build-site-twizzle build-site-experiments
.PHONY: build-site-twizzle
build-site-twizzle:
${NODE} ./script/build/sites/build-site-twizzle.js
${BUN_RUN} ./script/build/sites/build-site-twizzle.ts
.PHONY: build-site-experiments
build-site-experiments:
${NODE} ./script/build/sites/build-site-experiments.js
${BUN_RUN} ./script/build/sites/build-site-experiments.ts
.PHONY: build-site-docs
build-site-docs:
rm -rf ./dist/sites/js.cubing.net/
npx typedoc src/cubing/*/index.ts
${NPX} typedoc src/cubing/*/index.ts
cp -R ./src/docs/js.cubing.net/* ./dist/sites/js.cubing.net/
@echo "\n\nNote: The js.cubing.net docs are deployed to GitHub Pages using GitHub Actions when a commit is pushed to the \`main\` branch:\nhttps://github.com/cubing/cubing.js/actions/workflows/pages.yml"
.PHONY: generate-js
generate-js: generate-js-parsers generate-js-svg
.PHONY: generate-js-parsers
generate-js-parsers:
npx peggy --format es src/cubing/kpuzzle/parser/parser-peggy.peggy
${NPX} peggy --format es src/cubing/kpuzzle/parser/parser-peggy.peggy
.PHONY: generate-js-svg
generate-js-svg:
@echo "TODO: Generating JS for SVGs is not implemented yet."
.PHONY: dev
dev: quick-setup
${NODE} ./script/build/sites/dev.js
${BUN_RUN} ./script/build/sites/dev.ts
.PHONY: link
link: build
npm link
Expand Down Expand Up @@ -111,10 +114,10 @@ test-spec-watch:
${WEB_TEST_RUNNER} --playwright --watch
.PHONY: test-src-import-restrictions
test-src-import-restrictions:
${NODE} ./script/test/src/import-restrictions/main.js
${BUN_RUN} ./script/test/src/import-restrictions/main.ts
.PHONY: test-src-tsc
test-src-tsc: build-lib-types
npx tsc --project ./tsconfig.json
${NPX} tsc --project ./tsconfig.json
.PHONY: test-src-scripts-consistency
test-src-scripts-consistency:
${NODE} ./script/test/src/scripts-consistency/main.js
Expand Down Expand Up @@ -150,13 +153,13 @@ test-dist-lib-perf: build-lib-js
${NODE} script/test/dist/lib/cubing/perf/*.js
.PHONY: test-dist-lib-plain-esbuild-compat
test-dist-lib-plain-esbuild-compat: build-lib-js
${NODE} script/test/dist/lib/cubing/plain-esbuild-compat/main.js
${BUN_RUN} script/test/dist/lib/cubing/plain-esbuild-compat/main.ts
.PHONY: test-dist-lib-vite
test-dist-lib-vite: build-lib-js
${NODE} ./script/test/dist/lib/cubing/vite/main.js
${BUN_RUN} ./script/test/dist/lib/cubing/vite/main.ts
.PHONY: test-dist-lib-build-size
test-dist-lib-build-size: build-lib-js
${NODE} ./script/test/dist/lib/cubing/build-size/main.js
${BUN_RUN} ./script/test/dist/lib/cubing/build-size/main.ts
.PHONY: test-dist-sites-experiments
test-dist-sites-experiments: build-sites
${NODE} ./script/test/dist/sites/experiments.cubing.net/main.js
Expand Down Expand Up @@ -190,17 +193,17 @@ postpublish: update-cdn update-create-cubing-app deploy
deploy: deploy-twizzle deploy-experiments
.PHONY: deploy-twizzle
deploy-twizzle: build-site-twizzle
${NODE} script/deploy/twizzle.js
${BUN_RUN} script/deploy/twizzle.ts
.PHONY: deploy-experiments
deploy-experiments: build-site-experiments
${NODE} script/deploy/experiments.js
${BUN_RUN} script/deploy/experiments.ts
.PHONY: roll-vendored-twsearch
roll-vendored-twsearch:
test -d ../twsearch/ || exit
cd ../twsearch/ && make clean build/esm
rm -rf src/cubing/vendor/mpl/twsearch/*
cp -R ../twsearch/build/esm/* src/cubing/vendor/mpl/twsearch/
node script/fix-vendored-twsearch.js
${BUN_RUN} script/fix-vendored-twsearch.ts
.PHONY: update-create-cubing-app
update-create-cubing-app:
cd ../create-cubing-app && make roll-cubing-commit && git push
Expand All @@ -217,7 +220,7 @@ update-cdn:

# Not .PHONY(!)
node_modules:
${NODE} ./script/quick-setup/main.js
${BUN_RUN} ./script/quick-setup/main.ts

.PHONY: publish
.PHONY: publish
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ If you would like to contribute to the development of `cubing.js`, please refer

## Developing `cubing.js` itself

If you have `git`, `node`, and `npm` installed, you can run the `cubing.js` source like this (see the [contribution guidelines](./CONTRIBUTING.md) for more details):
If you have `git`, `node`, `npm`, and [`bun`](https://bun.sh/) installed, you can run the `cubing.js` source like this (see the [contribution guidelines](./CONTRIBUTING.md) for more details):

```shell
git clone https://github.com/cubing/cubing.js && cd cubing.js
Expand Down
2 changes: 1 addition & 1 deletion script/bin/screenshot.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

import { build } from "esbuild";
import { execPromise } from "../lib/execPromise.js";
import { execPromise } from "../lib/execPromise";

const TS_FILE = new URL("./screenshot-src/main.ts", import.meta.url).pathname;
const JS_FILE = new URL("./screenshot-src/main.js", import.meta.url).pathname;
Expand Down
23 changes: 23 additions & 0 deletions script/build/bin/build-bin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { build } from "esbuild";

await build({
entryPoints: ["src/bin/*.ts"],
outdir: "dist/bin/",
chunkNames: "chunks/[name]-[hash]",
format: "esm",
target: "es2022",
bundle: true,
logLevel: "info",
sourcemap: true,
splitting: true,
packages: "external",
external: ["cubing/*"],
supported: {
"top-level-await": true,
},
banner: {
js: "#!/usr/bin/env node",
},
});

// Note: the output entry files are `chmod`ded by the `Makefile`.
34 changes: 34 additions & 0 deletions script/build/common/package-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { join } from "node:path";
import type { BuildOptions } from "esbuild";
import type { IterableElement } from "../../lib/vendor/type-fest";

export const packageNames = [
"alg",
"bluetooth",
"kpuzzle",
"notation",
"protocol",
"puzzle-geometry",
"puzzles",
"scramble",
"stream",
"search",
"twisty",
];

export const packageEntryPoints: string[] = packageNames.map((p) =>
join("src/cubing/", p, "/index.ts"),
);

export const searchWorkerEsbuildWorkaroundEntry: IterableElement<
BuildOptions["entryPoints"]
> = {
in: "src/cubing/search/worker-workarounds/search-worker-entry.js",
// esbuild automatically adds `.js`
// https://esbuild.github.io/api/#entry-points
out: "chunks/search-worker-entry",
};

export const packageEntryPointsWithSearchWorkerEntry: BuildOptions["entryPoints"] =
// @ts-ignore: TypeScript can't seem to reconcile the types, no matter how much we annotate.
packageEntryPoints.concat([searchWorkerEsbuildWorkaroundEntry]);
31 changes: 31 additions & 0 deletions script/build/lib/build-lib-js.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { build, type BuildOptions } from "esbuild";
import { mkdir, writeFile } from "node:fs/promises";
import { packageEntryPointsWithSearchWorkerEntry } from "../common/package-info";

// In theory we could set `packages: "external"` here and rely on `make
// test-src-import-restrictions`, but this is safer.
export const external = ["three", "comlink", "random-uint-below"];

export const esmOptions: BuildOptions = {
// TODO: construct entry points based on `exports` and add tests.
entryPoints: packageEntryPointsWithSearchWorkerEntry,
outdir: "dist/lib/cubing",
chunkNames: "chunks/[name]-[hash]",
format: "esm",
target: "es2022",
bundle: true,
splitting: true,
logLevel: "info",
sourcemap: true,
//
external: external,
metafile: true,
};

const result = await build(esmOptions);

await mkdir("./.temp", { recursive: true });
await writeFile(
"./.temp/esbuild-metafile.json",
JSON.stringify(result.metafile),
);
15 changes: 15 additions & 0 deletions script/build/lib/build-lib-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { spawnPromise } from "../../lib/execPromise";
import { packageEntryPoints } from "../common/package-info";

console.warn(`
⏳⏳⏳⏳⏳⏳⏳⏳⏳⏳⏳⏳⏳⏳⏳⏳
Note: The \`types\` target is slow. Expect several seconds.
⏳⏳⏳⏳⏳⏳⏳⏳⏳⏳⏳⏳⏳⏳⏳⏳
`);
await spawnPromise("npx", [
"tsup",
...packageEntryPoints,
"--dts-only",
"--out-dir",
"dist/lib/cubing",
]);
Loading

0 comments on commit 9233708

Please sign in to comment.