Skip to content

Commit ea94ffd

Browse files
committed
Fix build process to include copydecks
1 parent 2112d69 commit ea94ffd

File tree

4 files changed

+25
-26
lines changed

4 files changed

+25
-26
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ npm run dev:test
8787
Create a clean build for distribution:
8888

8989
```
90-
npm run build
90+
npm run build:all && npm run build:browser
9191
```
9292

9393
Output files will be in dist/:

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
"exports": { ".": "./dist/index.js" },
1212
"scripts": {
1313
"build": "tsc -p tsconfig.json",
14+
"build:browser": "esbuild src/index.ts --bundle --format=esm --outfile=dist/index.browser.js",
15+
"build:assets": "mkdir -p dist/copydecks && cp -R copydecks/* dist/copydecks/",
16+
"build:all": "npm run build && npm run build:browser && npm run build:assets",
1417
"test": "vitest run",
1518
"dev:test": "vitest"
1619
},

src/index.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
export type {
2-
Trace,
3-
ExplainOptions,
4-
ExplainResult,
5-
CopyDeck,
6-
AdapterFn
7-
} from "./types";
8-
9-
export { loadCopydeck, registerAdapter, explain } from "./engine";
10-
export { skulptAdapter } from "./adapters/skulpt";
11-
export { pyodideAdapter } from "./adapters/pyodide";
1+
export type { Trace, ExplainOptions, ExplainResult, CopyDeck, AdapterFn } from "./types.js";
2+
export { loadCopydeck, registerAdapter, explain } from "./engine.js";
3+
export { skulptAdapter } from "./adapters/skulpt.js";
4+
export { pyodideAdapter } from "./adapters/pyodide.js";
5+
export { loadCopydeckFor } from "./loaders.js";

src/loaders.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
1-
import { loadCopydeck } from "./engine";
2-
import type { CopyDeck } from "./types";
1+
import { loadCopydeck } from "./engine.js";
2+
import type { CopyDeck } from "./types.js";
33

4-
/**
5-
* Loads a copydeck from /copydecks/{locale}/copydeck.json.
6-
* Falls back to a base language (e.g. "en" from "en-GB") or "en".
7-
*/
8-
export async function loadCopydeckFor(locale: string = "en"): Promise<CopyDeck> {
4+
type Options = { base?: string };
5+
6+
export async function loadCopydeckFor(locale = "en", opts: Options = {}): Promise<CopyDeck> {
97
const tryLocales = [locale];
10-
const base = locale.split("-")[0];
11-
if (base !== locale) tryLocales.push(base);
8+
const baseLang = locale.split("-")[0];
9+
if (baseLang !== locale) tryLocales.push(baseLang);
1210
if (!tryLocales.includes("en")) tryLocales.push("en");
1311

12+
const baseUrl = opts.base
13+
? new URL(opts.base, window.location.origin)
14+
: new URL("./copydecks/", import.meta.url);
15+
1416
for (const lang of tryLocales) {
1517
try {
16-
const res = await fetch(`/copydecks/${lang}/copydeck.json`);
18+
const url = new URL(`${lang}/copydeck.json`, baseUrl).toString();
19+
const res = await fetch(url);
1720
if (res.ok) {
18-
const deck = await res.json();
21+
const deck = (await res.json()) as CopyDeck;
1922
loadCopydeck(deck);
2023
return deck;
2124
}
22-
} catch {
23-
/* try next */
24-
}
25+
} catch { /* try next */ }
2526
}
2627
throw new Error(`No copydeck found for ${tryLocales.join(", ")}`);
2728
}
29+

0 commit comments

Comments
 (0)