@observablehq/notebook-kit 2.6.0 → 2.6.2

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.0",
8
+ "version": "2.6.2",
9
9
  "type": "module",
10
10
  "scripts": {
11
11
  "test": "TZ=America/Los_Angeles vitest",
@@ -6,10 +6,10 @@ 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;
13
13
  export declare function isMutableImport(node: NamedImportSpecifier): node is ImportSpecifier;
14
- /** Turns e.g. "viewof$foo" into "viewof foo", and "$$" into "$". */
14
+ /** Turns e.g. "viewof$foo" into "viewof foo", and "viewof$$foo" into "viewof$foo". */
15
15
  export declare function dedollar(input: string): string;
@@ -1,5 +1,4 @@
1
1
  import { flatMapImportSpecifiers, getImportedName, getLocalName } from "../imports.js";
2
- const CODE_DOLLAR = 36;
3
2
  /** If specifier is an observable: protocol import, resolves it. */
4
3
  export function resolveObservableImport(specifier) {
5
4
  if (!specifier.startsWith("observable:"))
@@ -17,13 +16,13 @@ function getImportAttributeKey(node) {
17
16
  return node.key.type === "Identifier" ? node.key.name : String(node.key.value);
18
17
  }
19
18
  /** Note: mutates inputs! */
20
- export function renderObservableImport(source, node, inputs) {
19
+ export function renderObservableImport(source, node, inputs, renameImport = String) {
21
20
  if (!inputs.includes("@variable"))
22
21
  inputs.push("@variable");
23
22
  return `(import(${source}).then((_) => {
24
23
  const module = __variable._module._runtime.module(_.default)${"injections" in node ? `.derive([${renderObservableInjections(node)}], __variable._module)` : ""};
25
24
  const outputs = new Map(Array.from(__variable._outputs, (v) => [v._name, v]));${flatMapImportSpecifiers(node, (specifier) => {
26
- const i = dedollar(getImportedName(specifier));
25
+ const i = renameImport(getImportedName(specifier));
27
26
  const l = getLocalName(specifier);
28
27
  return `
29
28
  outputs.get(${JSON.stringify(l)})?.import(${JSON.stringify(i)}${i === l ? "" : `, ${JSON.stringify(l)}`}, module);`;
@@ -75,29 +74,8 @@ export function isViewImport(node) {
75
74
  export function isMutableImport(node) {
76
75
  return "mutable" in node && !!node.mutable;
77
76
  }
78
- /** Turns e.g. "viewof$foo" into "viewof foo", and "$$" into "$". */
77
+ /** Turns e.g. "viewof$foo" into "viewof foo", and "viewof$$foo" into "viewof$foo". */
79
78
  export function dedollar(input) {
80
- const start = 0;
81
- const end = input.length;
82
- let dollars = 0;
83
- for (let i = start; i < end; ++i) {
84
- switch (input.charCodeAt(i)) {
85
- case CODE_DOLLAR: {
86
- ++dollars;
87
- break;
88
- }
89
- default: {
90
- if (dollars > 0) {
91
- input = `${input.slice(0, i - 1)}${dollars === 1 ? " " : ""}${input.slice(i)}`;
92
- dollars = 0;
93
- }
94
- break;
95
- }
96
- }
97
- }
98
- if (dollars > 0) {
99
- input = `${input.slice(0, end - 1)}${dollars === 1 ? " " : ""}`;
100
- dollars = 0;
101
- }
102
- return input;
79
+ const match = /^(viewof|mutable)([$]+)([^$].*)$/.exec(input);
80
+ return match ? `${match[1]}${match[2].length > 1 ? match[2].slice(1) : " "}${match[3]}` : input;
103
81
  }
@@ -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) {
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.0",
8
+ "version": "2.6.2",
9
9
  "type": "module",
10
10
  "scripts": {
11
11
  "test": "TZ=America/Los_Angeles vitest",