Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

page loaders #1216

Merged
merged 5 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
/coverage/
/dist/
/docs/.observablehq/dist/
/docs/theme/*.md
/docs/themes.md
/test/build/
/test/output/**/*-changed.*
/test/output/build/**/*-changed/
Expand Down
11 changes: 11 additions & 0 deletions docs/[dir]/dynamic.md.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {parseArgs} from "node:util";

const {values} = parseArgs({
options: {
dir: {
type: "string"
}
}
});

console.log(JSON.stringify(values));
1 change: 1 addition & 0 deletions docs/dynamic.md.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(Date.now());
25 changes: 0 additions & 25 deletions docs/theme/generate-themes.ts

This file was deleted.

13 changes: 12 additions & 1 deletion observablehq.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {existsSync} from "node:fs";
import {readFile, readdir, stat} from "node:fs/promises";
import {join} from "node:path/posix";
import {formatPrefix} from "d3-format";
import {themes} from "./docs/themes.md.ts";

let stargazers_count: number;
try {
Expand Down Expand Up @@ -86,7 +87,17 @@ export default {
{name: "Converting notebooks", path: "/convert"},
{name: "Contributing", path: "/contributing", pager: false}
],
paths: ["/foo/index", "/bar/index"],
paths: [
"/theme/dark",
"/theme/dark-alt",
"/theme/dashboard",
"/theme/light",
"/theme/light-alt",
"/theme/wide",
"/themes",
...themes.dark.map((theme) => `/theme/${theme}`),
...themes.light.map((theme) => `/theme/${theme}`)
],
base: "/framework",
globalStylesheets: [
"https://fonts.googleapis.com/css2?family=Inter:ital,opsz,wght@0,14..32,100..900;1,14..32,100..900&family=Spline+Sans+Mono:ital,wght@0,300..700;1,300..700&display=swap"
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
"observable": "dist/bin/observable.js"
},
"scripts": {
"dev": "rimraf --glob docs/themes.md docs/theme/*.md && (tsx watch docs/theme/generate-themes.ts & tsx watch --ignore docs --no-warnings=ExperimentalWarning ./src/bin/observable.ts preview --no-open)",
"docs:themes": "rimraf --glob docs/themes.md docs/theme/*.md && tsx docs/theme/generate-themes.ts",
"docs:build": "yarn docs:themes && tsx --no-warnings=ExperimentalWarning ./src/bin/observable.ts build",
"docs:deploy": "yarn docs:themes && tsx --no-warnings=ExperimentalWarning ./src/bin/observable.ts deploy",
"dev": "tsx watch --ignore docs --no-warnings=ExperimentalWarning ./src/bin/observable.ts preview --no-open",
"docs:build": "tsx --no-warnings=ExperimentalWarning ./src/bin/observable.ts build",
"docs:deploy": "tsx --no-warnings=ExperimentalWarning ./src/bin/observable.ts deploy",
"build": "rimraf dist && node build.js --outdir=dist --outbase=src \"src/**/*.{ts,js,css}\" --ignore \"**/*.d.ts\"",
"test": "concurrently npm:test:mocha npm:test:tsc npm:test:lint npm:test:prettier",
"test:coverage": "c8 --check-coverage --lines 80 --per-file yarn test:mocha",
Expand Down
28 changes: 12 additions & 16 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {getModuleResolver, getResolvers} from "./resolvers.js";
import {resolveImportPath, resolveStylesheetPath} from "./resolvers.js";
import {bundleStyles, rollupClient} from "./rollup.js";
import type {Params} from "./route.js";
import {find} from "./route.js";
import {searchIndex} from "./search.js";
import {Telemetry} from "./telemetry.js";
import {tree} from "./tree.js";
Expand Down Expand Up @@ -83,9 +82,10 @@ export async function build(
const globalImports = new Set<string>(); // e.g., "/_observablehq/search.js"
const stylesheets = new Set<string>(); // e.g., "/style.css"
for (const path of paths.map(normalizePagePath)) {
const found = find(root, `${path}.md`);
if (!found) throw new Error(`page not found: ${path}`);
const {path: sourceFile, params} = found;
const loader = loaders.find(`${path}.md`);
if (!loader) throw new Error(`page not found: ${path}`);
const {params} = loader;
const sourceFile = await loader.load(effects);
const sourcePath = join(root, sourceFile);
const options = {...config, params, path};
effects.output.write(`${faint("parse")} ${sourcePath} `);
Expand All @@ -96,7 +96,7 @@ export async function build(
effects.logger.log(faint("(skipped)"));
continue;
}
const resolvers = await getResolvers(page, {path: sourceFile, ...config});
const resolvers = await getResolvers(page, {path, ...config});
const elapsed = Math.floor(performance.now() - start);
for (const f of resolvers.assets) files.add(resolvePath(path, f));
for (const f of resolvers.files) files.add(resolvePath(path, f));
Expand Down Expand Up @@ -173,23 +173,19 @@ export async function build(

// Copy over referenced files, accumulating hashed aliases.
for (const file of files) {
let sourcePath: string;
effects.output.write(`${faint("copy")} ${join(root, file)} ${faint("→")} `);
const loader = loaders.find(join("/", file), {useStale: true});
if (!loader) {
effects.logger.error(red("error: missing referenced file"));
continue;
}
if ("load" in loader) {
try {
sourcePath = join(root, await loader.load(effects));
} catch (error) {
if (!isEnoent(error)) throw error;
effects.logger.error(red("error: missing referenced file"));
continue;
}
} else {
sourcePath = loader.path;
let sourcePath: string;
try {
sourcePath = join(root, await loader.load(effects));
} catch (error) {
if (!isEnoent(error)) throw error;
effects.logger.error(red("error: missing referenced file"));
continue;
}
const contents = await readFile(sourcePath);
const hash = createHash("sha256").update(contents).digest("hex").slice(0, 8);
Expand Down
Loading