Skip to content

Commit 5170bbf

Browse files
authored
use observablehq:stdlib instead of npm:@observablehq/stdlib (#1670)
* observablehq:stdlib * observablehq:stdlib/{xlsx,zip} * document observablehq:
1 parent 825c9b7 commit 5170bbf

30 files changed

+100
-73
lines changed

docs/chart.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {FileAttachment} from "npm:@observablehq/stdlib";
21
import * as Plot from "npm:@observablehq/plot";
2+
import {FileAttachment} from "observablehq:stdlib";
33

44
export async function Chart() {
55
const gistemp = await FileAttachment("./lib/gistemp.csv").csv({typed: true});

docs/embeds.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ Embedded modules are vanilla JavaScript, and behave identically when embedded in
77
Embedded modules are often written as component functions that return DOM elements. These functions can take options (or “props”), and typically load their own data. For example, below is a simple `chart.js` module that exports a `Chart` function that renders a scatterplot of global surface temperature data.
88

99
```js run=false
10-
import {FileAttachment} from "npm:@observablehq/stdlib";
1110
import * as Plot from "npm:@observablehq/plot";
11+
import {FileAttachment} from "observablehq:stdlib";
1212

1313
export async function Chart() {
1414
const gistemp = await FileAttachment("./lib/gistemp.csv").csv({typed: true});

docs/files.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ keywords: file, fileattachment, attachment
77
Load files — whether static or generated dynamically by a [data loader](./data-loaders) — using the built-in `FileAttachment` function. This is available by default in Markdown, but you can import it explicitly like so:
88

99
```js echo
10-
import {FileAttachment} from "npm:@observablehq/stdlib";
10+
import {FileAttachment} from "observablehq:stdlib";
1111
```
1212

1313
The `FileAttachment` function takes a path and returns a file handle. This handle exposes:

docs/imports.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ Imported symbols can be referenced in any code block or inline expression — no
2525
Inputs.button("Throw confetti! 🎉", {reduce: () => confetti()})
2626
```
2727

28-
2928
<div class="tip">While imports can live in code blocks anywhere on the page, by convention imports are placed at the top of pages for readability.</div>
3029

3130
Framework provides a variety of ways to import. When you reference `d3`, `Inputs`, `Plot` or some other built-in, you’re [implicitly importing](#implicit-imports) from npm. In addition, you can import modules explicitly from:
@@ -130,7 +129,7 @@ import {foo} from "./foo.js";
130129
Within a local module, you can import other local modules, as well as `npm:`, Node, and remote imports. You can also reference local files within a local module by importing [`FileAttachment`](./files) from the Observable standard library like so:
131130

132131
```js run=false
133-
import {FileAttachment} from "npm:@observablehq/stdlib";
132+
import {FileAttachment} from "observablehq:stdlib";
134133

135134
export const sales = await FileAttachment("sales.csv").csv({typed: true});
136135
```
@@ -206,15 +205,23 @@ Module preloading does not apply to [dynamic imports](#dynamic-imports) and [`im
206205
<link rel="modulepreload" href="npm:d3-array">
207206
```
208207
208+
## Observable imports
209+
210+
Framework includes a few built-in libraries implemented by Framework itself that can be imported via the `observablehq:` protocol. This currently includes:
211+
212+
* `observablehq:runtime` - the [Observable Runtime](https://github.com/observablehq/runtime)
213+
* `observablehq:stdlib` - [Framework’s standard library](https://github.com/observablehq/framework/blob/main/src/client/stdlib.js)
214+
209215
## Implicit imports
210216
211217
For convenience, Framework provides recommended libraries by default in Markdown. These implicit imports are only evaluated if you reference the corresponding symbol and hence don’t add overhead if you don’t use them; for example, D3 won’t be loaded unless you reference `d3`.
212218
213219
Click on any of the imported symbols below to learn more.
214220
215-
<pre><code class="language-js">import {<a href="./files">FileAttachment</a>} from "npm:@observablehq/stdlib";</code></pre>
216-
<pre><code class="language-js">import {<a href="./reactivity#generators">Generators</a>} from "npm:@observablehq/stdlib";</code></pre>
217-
<pre><code class="language-js">import {<a href="./reactivity#mutables">Mutable</a>} from "npm:@observablehq/stdlib";</code></pre>
221+
<pre><code class="language-js">import {<a href="./files">FileAttachment</a>} from "observablehq:stdlib";</code></pre>
222+
<pre><code class="language-js">import {<a href="./reactivity#generators">Generators</a>} from "observablehq:stdlib";</code></pre>
223+
<pre><code class="language-js">import {<a href="./reactivity#mutables">Mutable</a>} from "observablehq:stdlib";</code></pre>
224+
<pre><code class="language-js">import {<a href="./javascript#resize-render">resize</a>} from "observablehq:stdlib";</code></pre>
218225
<pre><code class="language-js">import <a href="./lib/dot">dot</a> from "npm:@observablehq/dot";</code></pre>
219226
<pre><code class="language-js">import * as <a href="./lib/duckdb">duckdb</a> from "npm:@duckdb/duckdb-wasm";</code></pre>
220227
<pre><code class="language-js">import {<a href="./lib/duckdb">DuckDBClient</a>} from "npm:@observablehq/duckdb";</code></pre>
@@ -235,8 +242,12 @@ Click on any of the imported symbols below to learn more.
235242
<pre><code class="language-js">import {<a href="./lib/htl">svg</a>} from "npm:htl";</code></pre>
236243
<pre><code class="language-js">import * as <a href="./lib/leaflet">L</a> from "npm:leaflet";</code></pre>
237244
<pre><code class="language-js">import <a href="../lib/lodash">_</a> from "npm:lodash";</code></pre>
245+
<pre><code class="language-js">import * as <a href="./jsx">React</a> from "npm:react";</code></pre>
246+
<pre><code class="language-js">import * as <a href="./jsx">ReactDOM</a> from "npm:react-dom";</code></pre>
238247
<pre><code class="language-js">import * as <a href="../lib/topojson">topojson</a> from "npm:topojson-client";</code></pre>
239248
249+
In addition to the above, several implicit imports have slightly more involved definitions: [`now`](./lib/generators#now), [`width`](./lib/generators#width-element), [`dark`](./lib/generators#dark), [`vg`](./lib/mosaic), and [`vl`](./lib/vega-lite).
250+
240251
## Require
241252
242253
If you’re familiar with Observable notebooks, you may be familiar with `require`. We recommend that you avoid `require` as the underlying Asynchronous Module Definition (AMD) convention has been made obsolete by standard imports in JavaScript, and AMD tends to be implemented inconsistently.

docs/lib/generators.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ keywords: [dark, width]
77
The Observable standard library includes several generator utilities. These are available by default in Markdown as `Generators`, but you can import them explicitly:
88

99
```js echo
10-
import {Generators} from "npm:@observablehq/stdlib";
10+
import {Generators} from "observablehq:stdlib";
1111
```
1212

1313
## input(*element*)
@@ -85,6 +85,10 @@ width
8585

8686
[Source](https://github.com/observablehq/framework/blob/main/src/client/stdlib/generators/dark.ts) · Returns an async generator that yields a boolean indicating whether the page is currently displayed with a dark [color scheme](https://developer.mozilla.org/en-US/docs/Web/CSS/color-scheme).
8787

88+
```js run=false
89+
const dark = Generators.dark();
90+
```
91+
8892
If the page supports both light and dark mode (as with the [default theme](../themes)), the value reflects the user’s [preferred color scheme](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme). The generator will yield a new value if the preferred color changes — as when the user changes their system settings, or if the user’s system adapts automatically to the diurnal cycle — allowing you to update the display as needed without requiring a page reload.
8993

9094
If the page only supports light mode, the value is always false; likewise it is always true if the page only has a dark theme.

docs/lib/mosaic.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ ${histogram}
1515

1616
The views above are coordinated: brushing a time window in the histogram, or a region in either map, will filter both maps. _What spatial patterns can you find?_
1717

18+
Mosaic vgplot is available by default in Markdown as `vg` and is backed by the default DuckDB client that is configured using [SQL front matter](../sql). If you would prefer to initialize Mosaic yourself, you can do something like:
19+
20+
```js run=false
21+
import {DuckDBClient} from "npm:@observablehq/duckdb";
22+
import * as vgplot from "npm:@uwdata/vgplot";
23+
24+
const db = await DuckDBClient.of({trips: FileAttachment("nyc-taxi.parquet")});
25+
const coordinator = new vgplot.Coordinator();
26+
coordinator.databaseConnector(vgplot.wasmConnector({duckdb: db._db}));
27+
const vg = vgplot.createAPIContext({coordinator});
28+
```
29+
1830
The code below creates three views, coordinated by Mosaic’s [crossfilter](https://uwdata.github.io/mosaic/api/core/selection.html#selection-crossfilter) helper.
1931

2032
```js echo

docs/reactivity.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ Normally, only the code block that declares a top-level variable can define it o
326326
`Mutable` is available by default in Markdown but you can import it explicitly like so:
327327
328328
```js echo
329-
import {Mutable} from "npm:@observablehq/stdlib";
329+
import {Mutable} from "observablehq:stdlib";
330330
```
331331
332332
Then to use it:

src/client/preview.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {FileAttachment, registerFile} from "npm:@observablehq/stdlib";
1+
import {FileAttachment, registerFile} from "observablehq:stdlib";
22
import {main, runtime, undefine} from "./main.js";
33
import {findLoading, findRoots, registerRoot} from "./main.js";
44
import {enableCopyButtons} from "./pre.js";

src/client/stdlib/inputs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {file as _file} from "@observablehq/inputs";
2-
import {AbstractFile} from "npm:@observablehq/stdlib";
2+
import {AbstractFile} from "observablehq:stdlib";
33

44
export {
55
button,

src/client/stdlib/zip.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {AbstractFile} from "npm:@observablehq/stdlib";
21
import JSZip from "npm:jszip";
2+
import {AbstractFile} from "observablehq:stdlib";
33

44
export class ZipArchive {
55
constructor(archive) {

0 commit comments

Comments
 (0)