@observablehq/notebook-kit 2.6.1 → 2.6.3

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 CHANGED
@@ -5,7 +5,7 @@
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/observablehq/notebook-kit.git"
7
7
  },
8
- "version": "2.6.1",
8
+ "version": "2.6.3",
9
9
  "type": "module",
10
10
  "scripts": {
11
11
  "test": "TZ=America/Los_Angeles vitest",
@@ -6,7 +6,7 @@ import type { NamedImportSpecifier } from "../imports.js";
6
6
  export declare function resolveObservableImport(specifier: string): string;
7
7
  export declare function isObservableImport(node: ImportDeclaration, specifier: string): boolean;
8
8
  /** Note: mutates inputs! */
9
- export declare function renderObservableImport(source: string, node: ImportDeclaration | ImportWithDeclaration, inputs: string[]): string;
9
+ export declare function renderObservableImport(source: string, node: ImportDeclaration | ImportWithDeclaration, inputs: string[], renameImport?: (name: string) => string): string;
10
10
  export declare function toSpecialIdentifier(identifier: Identifier, type: "viewof" | "mutable"): Identifier;
11
11
  export declare function toSpecialImportSpecifier(specifier: ImportSpecifier, type: "viewof" | "mutable"): ImportSpecifier;
12
12
  export declare function isViewImport(node: NamedImportSpecifier): node is ImportSpecifier;
@@ -16,13 +16,13 @@ function getImportAttributeKey(node) {
16
16
  return node.key.type === "Identifier" ? node.key.name : String(node.key.value);
17
17
  }
18
18
  /** Note: mutates inputs! */
19
- export function renderObservableImport(source, node, inputs) {
19
+ export function renderObservableImport(source, node, inputs, renameImport = String) {
20
20
  if (!inputs.includes("@variable"))
21
21
  inputs.push("@variable");
22
22
  return `(import(${source}).then((_) => {
23
23
  const module = __variable._module._runtime.module(_.default)${"injections" in node ? `.derive([${renderObservableInjections(node)}], __variable._module)` : ""};
24
24
  const outputs = new Map(Array.from(__variable._outputs, (v) => [v._name, v]));${flatMapImportSpecifiers(node, (specifier) => {
25
- const i = dedollar(getImportedName(specifier));
25
+ const i = renameImport(getImportedName(specifier));
26
26
  const l = getLocalName(specifier);
27
27
  return `
28
28
  outputs.get(${JSON.stringify(l)})?.import(${JSON.stringify(i)}${i === l ? "" : `, ${JSON.stringify(l)}`}, module);`;
@@ -20,10 +20,12 @@ export type ImportOptions = {
20
20
  resolveImport?: (specifier: string) => string;
21
21
  /** If true, resolve local imports relative to document.baseURI. */
22
22
  resolveLocalImports?: boolean;
23
+ /** If specified, allows renaming of imported variables such as viewof$foo for backwards compatibility. */
24
+ renameObservableImport?: (name: string) => string;
23
25
  };
24
26
  export declare function rewriteImportExpressions(output: Sourcemap, body: Node, { resolveImport, resolveLocalImports }?: ImportOptions): void;
25
27
  /** Note: mutates inputs! */
26
- export declare function rewriteImportDeclarations(output: Sourcemap, body: Node, inputs: string[], { resolveImport, resolveLocalImports }?: ImportOptions): void;
28
+ export declare function rewriteImportDeclarations(output: Sourcemap, body: Node, inputs: string[], { resolveImport, resolveLocalImports, renameObservableImport }?: ImportOptions): void;
27
29
  export declare function getLocalName(node: NamedImportSpecifier): string;
28
30
  export declare function getImportedName(node: NamedImportSpecifier): string;
29
31
  export declare function flatMapImportSpecifiers<T>(node: ImportDeclaration, f: (node: NamedImportSpecifier) => T): T[];
@@ -1,6 +1,6 @@
1
1
  import { resolveJsrImport } from "./imports/jsr.js";
2
2
  import { resolveNpmImport } from "./imports/npm.js";
3
- import { isObservableImport, isMutableImport, isViewImport } from "./imports/observable.js";
3
+ import { isObservableImport, isMutableImport, isViewImport, dedollar } from "./imports/observable.js";
4
4
  import { resolveObservableImport, renderObservableImport } from "./imports/observable.js";
5
5
  import { toSpecialImportSpecifier } from "./imports/observable.js";
6
6
  import { getStringLiteralValue, isStringLiteral } from "./strings.js";
@@ -79,7 +79,7 @@ export function rewriteImportExpressions(output, body, { resolveImport = resolve
79
79
  });
80
80
  }
81
81
  /** Note: mutates inputs! */
82
- export function rewriteImportDeclarations(output, body, inputs, { resolveImport = resolveImportDefault, resolveLocalImports } = {}) {
82
+ export function rewriteImportDeclarations(output, body, inputs, { resolveImport = resolveImportDefault, resolveLocalImports, renameObservableImport = dedollar } = {}) {
83
83
  const declarations = [];
84
84
  simple(body, {
85
85
  ImportDeclaration(node) {
@@ -102,7 +102,7 @@ export function rewriteImportDeclarations(output, body, inputs, { resolveImport
102
102
  ? `new URL(${JSON.stringify(resolution)}, document.baseURI)`
103
103
  : JSON.stringify(resolution);
104
104
  imports.push(isObservableImport(node, value)
105
- ? renderObservableImport(source, node, inputs)
105
+ ? renderObservableImport(source, node, inputs, renameObservableImport)
106
106
  : renderImport(source, node, output.input));
107
107
  }
108
108
  if (declarations.length > 1) {
@@ -127,6 +127,6 @@ function asDeclaration(ref) {
127
127
  return ref.type === "ViewExpression"
128
128
  ? `viewof$${ref.id.name}`
129
129
  : ref.type === "MutableExpression"
130
- ? `mutable ${ref.id.name}` // "initial x", really
130
+ ? `initial ${ref.id.name}`
131
131
  : ref.name;
132
132
  }
@@ -43,7 +43,7 @@ export function define(main, state, definition, observer = observe) {
43
43
  variables.push(main.define(o, [output], input));
44
44
  }
45
45
  else if (automutable) {
46
- const o = unprefix(output, "mutable ");
46
+ const o = unprefix(output, "initial ");
47
47
  const x = `cell ${id}`;
48
48
  v.define(o, [x], ([mutable]) => mutable); // observe live value
49
49
  variables.push(main.define(output, inputs, body), // initial value
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/observablehq/notebook-kit.git"
7
7
  },
8
- "version": "2.6.1",
8
+ "version": "2.6.3",
9
9
  "type": "module",
10
10
  "scripts": {
11
11
  "test": "TZ=America/Los_Angeles vitest",