Skip to content

Commit 2008ed4

Browse files
committed
package as "codeblocks", some refactoring
1 parent eabb093 commit 2008ed4

9 files changed

Lines changed: 245 additions & 128 deletions

File tree

.github/workflows/pages.yaml

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,42 +24,7 @@ jobs:
2424
env:
2525
GH_TOKEN: ${{ github.token }}
2626
RELEASE_REPOSITORY: ${{ github.repository }}
27-
run: |
28-
set -euo pipefail
29-
release_prefix="clangd-wasm/"
30-
release_tag=$(
31-
gh api --paginate "repos/${RELEASE_REPOSITORY}/releases?per_page=100" |
32-
jq -sr --arg prefix "$release_prefix" '
33-
add
34-
| map(select(
35-
(.draft | not)
36-
and (.tag_name | startswith($prefix))
37-
))
38-
| sort_by(.published_at)
39-
| last
40-
| .tag_name // empty
41-
'
42-
)
43-
if [[ -z "$release_tag" ]]; then
44-
echo "No published clangd wasm release found with prefix ${release_prefix}" >&2
45-
echo "Run scripts/release-clangd-wasm.sh locally first." >&2
46-
exit 1
47-
fi
48-
artifact_dir=$(mktemp -d)
49-
trap 'rm -rf "$artifact_dir"' EXIT
50-
echo "Using ${release_tag}"
51-
gh release download "$release_tag" \
52-
--repo "$RELEASE_REPOSITORY" \
53-
--pattern clangd.js \
54-
--pattern clangd.wasm \
55-
--pattern SHA256SUMS \
56-
--dir "$artifact_dir"
57-
(
58-
cd "$artifact_dir"
59-
sha256sum -c SHA256SUMS
60-
)
61-
install -m 0644 "$artifact_dir/clangd.js" public/wasm/clangd.js
62-
install -m 0644 "$artifact_dir/clangd.wasm" public/wasm/clangd.wasm
27+
run: scripts/install-latest-clangd-release
6328
- run: pnpm build
6429
- uses: actions/upload-pages-artifact@v3
6530
with:

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ lerna-debug.log*
99

1010
node_modules
1111
dist
12-
dist-ssr
12+
dist-*
13+
packages
1314
test-results
1415
playwright-report
1516
*.local

README.md

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ Copy the complete contents of `dist/` to one public directory, then include one
1111
<script src="./codeblocks.js"></script>
1212

1313
<codeblock compiler="gsnapshot" args="-std=c++26 -freflection">
14-
#include &lt;print&gt;
15-
int main() {
16-
std::println("foo {} bar", 42);
17-
}
14+
#include &lt;print&gt; int main() { std::println("foo {} bar", 42); }
1815
</codeblock>
1916
```
2017

@@ -26,12 +23,8 @@ A block can also contain multiple tabbed sources:
2623

2724
```html
2825
<codeblock compiler="clang2110" args="-std=c++23" height="360px">
29-
<codeblock-tab name="first.cpp">
30-
int main() { return 1; }
31-
</codeblock-tab>
32-
<codeblock-tab name="second.cpp">
33-
int main() { return 2; }
34-
</codeblock-tab>
26+
<codeblock-tab name="first.cpp"> int main() { return 1; } </codeblock-tab>
27+
<codeblock-tab name="second.cpp"> int main() { return 2; } </codeblock-tab>
3528
</codeblock>
3629
```
3730

@@ -66,7 +59,7 @@ Supported attributes are:
6659
},
6760
styles: {
6861
"editor-height": "360px",
69-
"accent": "#7c3aed",
62+
accent: "#7c3aed",
7063
},
7164
});
7265
@@ -176,6 +169,29 @@ pnpm exec playwright install chromium
176169
pnpm test:browser
177170
```
178171

172+
## npm packages
173+
174+
The release workflow publishes two packages from the same source and version:
175+
176+
- `codeblocks` is self-contained and includes the clangd JavaScript and
177+
WebAssembly files from `public/wasm/`.
178+
- `codeblocks-remote` omits those large files and builds the worker to load
179+
them from `https://clangd.cpp.social/wasm/`.
180+
181+
Both packages contain the classic loader as the `./loader` export, the stylesheet
182+
as `./styles.css`, the typed ES module API at the package root, and every emitted
183+
chunk and worker required by that build.
184+
185+
Local packaging never downloads artifacts. It uses the files already present in
186+
`public/wasm/`, so a manual package is built from exactly the current checkout:
187+
188+
```bash
189+
pnpm build
190+
pnpm package:npm
191+
npm pack ./packages/codeblocks
192+
```
193+
194+
179195
Create a deployable archive with:
180196

181197
```bash

package.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
{
2-
"name": "clangd-in-browser",
2+
"name": "codeblocks",
33
"private": true,
4-
"version": "0.0.0",
4+
"version": "0.1.0",
5+
"description": "Editable C++ code blocks backed by Monaco and clangd WebAssembly",
6+
"license": "MIT",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/cppsocial/clangd-web.git"
10+
},
511
"type": "module",
612
"scripts": {
713
"clean": "vite-node --config ./scripts/vite.node.ts ./scripts/clean.ts",
814
"dev": "pnpm build",
915
"watch": "tsc -w",
10-
"build": "pnpm run clean && tsc && vite build && node scripts/copy-runtime-assets.mjs",
16+
"build": "pnpm run clean && tsc && vite build && tsc -p tsconfig.types.json && node scripts/copy-runtime-assets.mjs",
17+
"package:npm": "node scripts/prepare-npm-package.mjs dist packages/codeblocks codeblocks",
1118
"pack:runtime": "pnpm build && bash scripts/package-runtime.sh",
1219
"release:wasm": "bash scripts/release-clangd-wasm.sh",
1320
"demo:origins": "node scripts/serve-test.mjs",

scripts/copy-runtime-assets.mjs

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,40 @@ import { existsSync } from "node:fs";
33

44
const root = new URL("../", import.meta.url);
55
const source = new URL("public/wasm/", root);
6-
const destination = new URL("dist/wasm/", root);
6+
const arguments_ = new Set(process.argv.slice(2));
7+
const outdirArgument = process.argv
8+
.slice(2)
9+
.find((value) => value.startsWith("--outdir="));
10+
const outdir = outdirArgument?.slice("--outdir=".length) ?? "dist";
11+
const includeWasm = !arguments_.has("--without-wasm");
12+
const destination = new URL(`${outdir}/wasm/`, root);
713

8-
for (const name of ["clangd.js", "clangd.wasm"]) {
9-
if (!existsSync(new URL(name, source))) {
10-
throw new Error(
11-
`Missing public/wasm/${name}. Run scripts/install-clangd-artifacts first.`,
12-
);
14+
if (includeWasm) {
15+
for (const name of ["clangd.js", "clangd.wasm"]) {
16+
if (!existsSync(new URL(name, source))) {
17+
throw new Error(
18+
`Missing public/wasm/${name}. Run scripts/install-clangd-artifacts first.`,
19+
);
20+
}
1321
}
22+
await mkdir(destination, { recursive: true });
23+
await cp(source, destination, {
24+
recursive: true,
25+
filter: (path) => !path.endsWith(".gitignore"),
26+
});
1427
}
1528

16-
await mkdir(destination, { recursive: true });
17-
await cp(source, destination, { recursive: true, filter: (path) => !path.endsWith(".gitignore") });
18-
await cp(new URL("public/_headers", root), new URL("dist/_headers", root));
29+
await cp(new URL("public/_headers", root), new URL(`${outdir}/_headers`, root));
1930

20-
await cp(new URL("public/codeblocks.js", root), new URL("dist/codeblocks.js", root));
21-
await cp(new URL("public/index.html", root), new URL("dist/index.html", root));
22-
await cp(new URL("public/coi-serviceworker.js", root), new URL("dist/coi-serviceworker.js", root));
31+
await cp(
32+
new URL("public/codeblocks.js", root),
33+
new URL(`${outdir}/codeblocks.js`, root),
34+
);
35+
await cp(
36+
new URL("public/coi-serviceworker.js", root),
37+
new URL(`${outdir}/coi-serviceworker.js`, root),
38+
);
39+
await cp(
40+
new URL("src/loader-api.d.ts", root),
41+
new URL(`${outdir}/loader.d.ts`, root),
42+
);

0 commit comments

Comments
 (0)