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

custom builtins #1085

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions docs/_builtins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
confetti: () => import("npm:canvas-confetti").then((confetti) => confetti.default)
};
4 changes: 4 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Configuration

```js
confetti();
```

A `observablehq.config.js` (or `observablehq.config.ts`) file located in the project root allows configuration of your project. For example, a site might use a config file to set the project’s title and the sidebar contents:

```js run=false
Expand Down
4 changes: 3 additions & 1 deletion src/client/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import builtins from "observablehq:builtins";
import {Runtime} from "observablehq:runtime";
import {FileAttachment, Generators, Mutable, resize} from "observablehq:stdlib";
import {inspect, inspectError} from "./inspect.js";
Expand All @@ -13,7 +14,8 @@ const library = {
Generators: () => Generators,
Mutable: () => Mutable,
...recommendedLibraries,
...sampleDatasets
...sampleDatasets,
...builtins
};

export const runtime = new Runtime(library);
Expand Down
4 changes: 3 additions & 1 deletion src/rollup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {extname} from "node:path/posix";
import {extname, join} from "node:path/posix";
import {nodeResolve} from "@rollup/plugin-node-resolve";
import {type CallExpression} from "acorn";
import {simple} from "acorn-walk";
Expand Down Expand Up @@ -109,6 +109,8 @@ function importResolve(input: string, root: string, path: string): Plugin {
async function resolve(specifier: string | AstNode): Promise<ResolveIdResult> {
return typeof specifier !== "string" || specifier === input
? null
: specifier === "observablehq:builtins"
? {id: join(root, "_builtins.js")}
: specifier.startsWith("observablehq:")
? {id: relativePath(path, `/_observablehq/${specifier.slice("observablehq:".length)}${extname(specifier) ? "" : ".js"}`), external: true} // prettier-ignore
: specifier === "npm:@observablehq/runtime"
Expand Down
Loading