@observablehq/notebook-kit 2.6.3 → 2.6.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/package.json +1 -1
- package/dist/src/javascript/imports.d.ts +2 -2
- package/dist/src/javascript/imports.js +6 -0
- package/dist/src/javascript/observable.js +3 -2
- package/dist/src/javascript/transpile.d.ts +2 -0
- package/dist/src/javascript/transpile.js +4 -3
- package/dist/src/lib/unprefix.d.ts +1 -0
- package/dist/src/lib/unprefix.js +5 -0
- package/dist/src/runtime/define.js +1 -5
- package/dist/src/runtime/display.d.ts +1 -1
- package/dist/src/runtime/display.js +4 -1
- package/dist/src/styles/global.css +6 -2
- package/package.json +1 -1
package/dist/package.json
CHANGED
|
@@ -23,9 +23,9 @@ export type ImportOptions = {
|
|
|
23
23
|
/** If specified, allows renaming of imported variables such as viewof$foo for backwards compatibility. */
|
|
24
24
|
renameObservableImport?: (name: string) => string;
|
|
25
25
|
};
|
|
26
|
-
export declare function rewriteImportExpressions(output: Sourcemap, body: Node, { resolveImport, resolveLocalImports }?: ImportOptions):
|
|
26
|
+
export declare function rewriteImportExpressions(output: Sourcemap, body: Node, { resolveImport, resolveLocalImports }?: ImportOptions): Set<string>;
|
|
27
27
|
/** Note: mutates inputs! */
|
|
28
|
-
export declare function rewriteImportDeclarations(output: Sourcemap, body: Node, inputs: string[], { resolveImport, resolveLocalImports, renameObservableImport }?: ImportOptions):
|
|
28
|
+
export declare function rewriteImportDeclarations(output: Sourcemap, body: Node, inputs: string[], { resolveImport, resolveLocalImports, renameObservableImport }?: ImportOptions): Set<string>;
|
|
29
29
|
export declare function getLocalName(node: NamedImportSpecifier): string;
|
|
30
30
|
export declare function getImportedName(node: NamedImportSpecifier): string;
|
|
31
31
|
export declare function flatMapImportSpecifiers<T>(node: ImportDeclaration, f: (node: NamedImportSpecifier) => T): T[];
|
|
@@ -51,8 +51,10 @@ export function resolveImportDefault(specifier) {
|
|
|
51
51
|
return resolveObservableImport(resolveJsrImport(resolveNpmImport(specifier)));
|
|
52
52
|
}
|
|
53
53
|
export function rewriteImportExpressions(output, body, { resolveImport = resolveImportDefault, resolveLocalImports } = {}) {
|
|
54
|
+
const sources = new Set();
|
|
54
55
|
function rewriteImportSource(source, node = source) {
|
|
55
56
|
const value = getStringLiteralValue(source);
|
|
57
|
+
sources.add(value);
|
|
56
58
|
const resolution = resolveImport(value);
|
|
57
59
|
output.replaceLeft(node.start, node.end, resolveLocalImports && isLocalImport(resolution)
|
|
58
60
|
? `new URL(${JSON.stringify(resolution)}, document.baseURI).href`
|
|
@@ -77,9 +79,11 @@ export function rewriteImportExpressions(output, body, { resolveImport = resolve
|
|
|
77
79
|
}
|
|
78
80
|
}
|
|
79
81
|
});
|
|
82
|
+
return sources;
|
|
80
83
|
}
|
|
81
84
|
/** Note: mutates inputs! */
|
|
82
85
|
export function rewriteImportDeclarations(output, body, inputs, { resolveImport = resolveImportDefault, resolveLocalImports, renameObservableImport = dedollar } = {}) {
|
|
86
|
+
const sources = new Set();
|
|
83
87
|
const declarations = [];
|
|
84
88
|
simple(body, {
|
|
85
89
|
ImportDeclaration(node) {
|
|
@@ -97,6 +101,7 @@ export function rewriteImportDeclarations(output, body, inputs, { resolveImport
|
|
|
97
101
|
output.delete(node.start, node.end + +(output.input[node.end] === "\n"));
|
|
98
102
|
specifiers.push(rewriteImportSpecifiers(node));
|
|
99
103
|
const value = getStringLiteralValue(literal);
|
|
104
|
+
sources.add(value);
|
|
100
105
|
const resolution = resolveImport(value);
|
|
101
106
|
const source = resolveLocalImports && isLocalImport(resolution)
|
|
102
107
|
? `new URL(${JSON.stringify(resolution)}, document.baseURI)`
|
|
@@ -111,6 +116,7 @@ export function rewriteImportDeclarations(output, body, inputs, { resolveImport
|
|
|
111
116
|
else if (declarations.length === 1) {
|
|
112
117
|
output.insertLeft(0, `const ${specifiers[0]} = await ${imports[0]};\n`);
|
|
113
118
|
}
|
|
119
|
+
return sources;
|
|
114
120
|
}
|
|
115
121
|
function renderImport(source, node, input) {
|
|
116
122
|
const names = node.specifiers
|
|
@@ -53,7 +53,7 @@ function transpileObservableImport(input, cell, options) {
|
|
|
53
53
|
const declarations = flatMapImportSpecifiers(cell.body, (s) => s.local);
|
|
54
54
|
const outputs = Array.from(new Set(declarations.map(asDeclaration)));
|
|
55
55
|
transformObservableImport(cell.body);
|
|
56
|
-
rewriteImportDeclarations(output, cell.body, inputs, options);
|
|
56
|
+
const imports = rewriteImportDeclarations(output, cell.body, inputs, options);
|
|
57
57
|
output.insertLeft(0, `async (__variable) => {\n`);
|
|
58
58
|
if (outputs.length > 0)
|
|
59
59
|
output.insertRight(input.length, `\nreturn {${outputs}};`);
|
|
@@ -66,7 +66,8 @@ function transpileObservableImport(input, cell, options) {
|
|
|
66
66
|
autodisplay: false,
|
|
67
67
|
files: new Set(),
|
|
68
68
|
secrets: new Set(),
|
|
69
|
-
databases: new Set()
|
|
69
|
+
databases: new Set(),
|
|
70
|
+
imports
|
|
70
71
|
};
|
|
71
72
|
}
|
|
72
73
|
/** Mutates the given import declaration to be an Observable import. */
|
|
@@ -21,6 +21,8 @@ export type TranspiledJavaScript = {
|
|
|
21
21
|
databases?: Set<string>;
|
|
22
22
|
/** the names of any referenced secrets */
|
|
23
23
|
secrets?: Set<string>;
|
|
24
|
+
/** the module specifiers of any (statically analyzable) imports */
|
|
25
|
+
imports?: Set<string>;
|
|
24
26
|
};
|
|
25
27
|
export interface TranspileOptions extends ImportOptions {
|
|
26
28
|
/** If true, resolve file using import.meta.url (so Vite treats it as an asset). */
|
|
@@ -50,8 +50,8 @@ function transpileCell(cell, output, options) {
|
|
|
50
50
|
async = true;
|
|
51
51
|
const outputs = Array.from(new Set(cell.declarations?.map((r) => r.name)));
|
|
52
52
|
output.trim();
|
|
53
|
-
rewriteImportDeclarations(output, cell.body, inputs, options);
|
|
54
|
-
rewriteImportExpressions(output, cell.body, options);
|
|
53
|
+
const importDeclarations = rewriteImportDeclarations(output, cell.body, inputs, options);
|
|
54
|
+
const importExpressions = rewriteImportExpressions(output, cell.body, options);
|
|
55
55
|
if (options?.resolveFiles)
|
|
56
56
|
rewriteFileExpressions(output, cell.body);
|
|
57
57
|
if (cell.expression)
|
|
@@ -67,7 +67,8 @@ function transpileCell(cell, output, options) {
|
|
|
67
67
|
const files = new Set(cell.files.map((f) => f.argument));
|
|
68
68
|
const databases = new Set(cell.databases.map((f) => f.argument));
|
|
69
69
|
const secrets = new Set(cell.secrets.map((f) => f.argument));
|
|
70
|
-
|
|
70
|
+
const imports = new Set([...importDeclarations, ...importExpressions]);
|
|
71
|
+
return { body, inputs, outputs, autodisplay, files, databases, secrets, imports };
|
|
71
72
|
}
|
|
72
73
|
function deat(input) {
|
|
73
74
|
return input.replace(/^@/, "__"); // @variable to __variable
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function unprefix(name: string, prefix: string): string;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { unprefix } from "../lib/unprefix.js";
|
|
1
2
|
import { clear, display, observe } from "./display.js";
|
|
2
3
|
import { input } from "./stdlib/generators/index.js";
|
|
3
4
|
import { Mutator } from "./stdlib/mutable.js";
|
|
@@ -56,8 +57,3 @@ export function define(main, state, definition, observer = observe) {
|
|
|
56
57
|
}
|
|
57
58
|
}
|
|
58
59
|
}
|
|
59
|
-
function unprefix(name, prefix) {
|
|
60
|
-
if (!name.startsWith(prefix))
|
|
61
|
-
throw new Error(`expected ${prefix}: ${name}`);
|
|
62
|
-
return name.slice(prefix.length);
|
|
63
|
-
}
|
|
@@ -10,7 +10,7 @@ export type DisplayState = {
|
|
|
10
10
|
};
|
|
11
11
|
export declare function display(state: DisplayState, value: unknown, name?: string, mode?: DisplayMode): void;
|
|
12
12
|
export declare function clear(state: DisplayState): void;
|
|
13
|
-
export declare function observe(state: DisplayState, { autodisplay, assets, output: name, displayMode: mode }: Definition): {
|
|
13
|
+
export declare function observe(state: DisplayState, { autodisplay, assets, output: name, automutable, displayMode: mode }: Definition): {
|
|
14
14
|
_error: boolean;
|
|
15
15
|
_node: HTMLDivElement;
|
|
16
16
|
pending(): void;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { unprefix } from "../lib/unprefix.js";
|
|
1
2
|
import { inspect, inspectError, getExpanded } from "./inspect.js";
|
|
2
3
|
import { mapAssets } from "./stdlib/assets.js";
|
|
3
4
|
export function display(state, value, name, mode) {
|
|
@@ -45,7 +46,9 @@ export function clear(state) {
|
|
|
45
46
|
while (state.root.lastChild)
|
|
46
47
|
state.root.lastChild.remove();
|
|
47
48
|
}
|
|
48
|
-
export function observe(state, { autodisplay, assets, output: name, displayMode: mode }) {
|
|
49
|
+
export function observe(state, { autodisplay, assets, output: name, automutable, displayMode: mode }) {
|
|
50
|
+
if (automutable && name)
|
|
51
|
+
name = unprefix(name, "initial ");
|
|
49
52
|
return {
|
|
50
53
|
_error: false,
|
|
51
54
|
_node: state.root, // _node for visibility promise
|