@observablehq/notebook-kit 2.1.0 → 2.1.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.1.0",
8
+ "version": "2.1.2",
9
9
  "type": "module",
10
10
  "scripts": {
11
11
  "test": "vitest",
@@ -1,4 +1,4 @@
1
- import { getImportedName } from "../imports.js";
1
+ import { getImportedName, getLocalName } from "../imports.js";
2
2
  const CODE_DOLLAR = 36;
3
3
  /** If specifier is an observable: protocol import, resolves it. */
4
4
  export function resolveObservableImport(specifier) {
@@ -18,25 +18,21 @@ function getImportAttributeKey(node) {
18
18
  }
19
19
  /** Note: mutates inputs! */
20
20
  export function renderObservableImport(source, node, inputs) {
21
- if (!inputs.includes("__ojs_runtime"))
22
- inputs.push("__ojs_runtime");
23
- if (!inputs.includes("__ojs_observer"))
24
- inputs.push("__ojs_observer");
21
+ if (!inputs.includes("@variable"))
22
+ inputs.push("@variable");
25
23
  return `(import(${JSON.stringify(source)}).then((_) => {
26
- const observers = {};
27
- const module = __ojs_runtime.module(_.default);
28
- const main = __ojs_runtime.module();${node.specifiers
24
+ const module = __variable._module._runtime.module(_.default);
25
+ const outputs = new Map(Array.from(__variable._outputs, (v) => [v._name, v]));${node.specifiers
29
26
  .map((specifier) => {
30
27
  if (specifier.type === "ImportNamespaceSpecifier")
31
28
  throw new SyntaxError("observable namespace imports are not supported");
32
- const iname = getImportedName(specifier);
33
- const vname = dedollar(iname);
29
+ const iname = dedollar(getImportedName(specifier));
30
+ const lname = getLocalName(specifier);
34
31
  return `
35
- if (!module.defines("${vname}")) throw new SyntaxError(\`export '${vname}' not found\`);
36
- main.variable(observers.${iname} = __ojs_observer()).import("${vname}", module);`;
32
+ outputs.get(${JSON.stringify(lname)})?.import(${JSON.stringify(iname)}${iname === lname ? "" : `, ${JSON.stringify(lname)}`}, module);`;
37
33
  })
38
34
  .join("")}
39
- return observers;
35
+ return {};
40
36
  }))`;
41
37
  }
42
38
  /** Turns e.g. "viewof$foo" into "viewof foo", and "$$" into "$". */
@@ -19,5 +19,6 @@ export type RewriteImportOptions = {
19
19
  export declare function rewriteImportExpressions(output: Sourcemap, body: Node, { resolveLocalImports }?: RewriteImportOptions): void;
20
20
  /** Note: mutates inputs! */
21
21
  export declare function rewriteImportDeclarations(output: Sourcemap, body: Node, inputs: string[], { resolveLocalImports }?: RewriteImportOptions): void;
22
+ export declare function getLocalName(node: NamedImportSpecifier): string;
22
23
  export declare function getImportedName(node: NamedImportSpecifier): string;
23
24
  export {};
@@ -98,7 +98,7 @@ export function rewriteImportDeclarations(output, body, inputs, { resolveLocalIm
98
98
  : JSON.stringify(resolution), node, output.input));
99
99
  }
100
100
  if (declarations.length > 1) {
101
- output.insertLeft(0, `const [${specifiers.join(", ")}] = await Promise.all([${imports.join(", ")}]);\n`);
101
+ output.insertLeft(0, `const [${specifiers.join(", ")}] = await Promise.all([${imports.join(", ")}]);\n`); // prettier-ignore
102
102
  }
103
103
  else if (declarations.length === 1) {
104
104
  output.insertLeft(0, `const ${specifiers[0]} = await ${imports[0]};\n`);
@@ -117,7 +117,7 @@ function renderImport(source, node, input) {
117
117
  })`
118
118
  : ""}`;
119
119
  }
120
- function getLocalName(node) {
120
+ export function getLocalName(node) {
121
121
  return node.local.name;
122
122
  }
123
123
  export function getImportedName(node) {
@@ -114,7 +114,9 @@ function getPrefix(cell) {
114
114
  ? getSqlPrefix(cell)
115
115
  : isInterpreter(cell.mode)
116
116
  ? getInterpreterPrefix(cell)
117
- : cell.mode;
117
+ : cell.mode === "html"
118
+ ? "htl.html"
119
+ : cell.mode;
118
120
  }
119
121
  function getSqlPrefix(cell) {
120
122
  const { id, database = "var:db", since } = cell;
@@ -52,7 +52,7 @@ export function transpileJavaScript(input, options) {
52
52
  rewriteFileExpressions(output, cell.body);
53
53
  if (cell.expression)
54
54
  output.insertLeft(0, `return (\n`);
55
- output.insertLeft(0, `${async ? "async " : ""}(${inputs}) => {\n`);
55
+ output.insertLeft(0, `${async ? "async " : ""}(${inputs.map(deat)}) => {\n`);
56
56
  if (outputs.length > 0)
57
57
  output.insertRight(input.length, `\nreturn {${outputs}};`);
58
58
  if (cell.expression)
@@ -65,3 +65,6 @@ export function transpileJavaScript(input, options) {
65
65
  const secrets = new Set(cell.secrets.map((f) => f.argument));
66
66
  return { body, inputs, outputs, autodisplay, files, databases, secrets };
67
67
  }
68
+ function deat(input) {
69
+ return input.replace(/^@/, "__"); // @variable to __variable
70
+ }
@@ -22,7 +22,7 @@ export class NotebookRuntime {
22
22
  writable: true,
23
23
  value: void 0
24
24
  });
25
- const runtime = new Runtime({ ...builtins, __ojs_runtime: () => runtime });
25
+ const runtime = new Runtime(builtins);
26
26
  this.runtime = Object.assign(runtime, { fileAttachments });
27
27
  this.main = runtime.module();
28
28
  }
@@ -35,7 +35,7 @@ export const runtime = defaultNotebook.runtime;
35
35
  export const main = defaultNotebook.main;
36
36
  export const define = defaultNotebook.define.bind(defaultNotebook);
37
37
  main.constructor.prototype.defines = function (name) {
38
- return (this._scope.has(name) ||
38
+ return (this._scope.has(name) || // prettier
39
39
  this._builtins.has(name) ||
40
40
  this._runtime._builtin._scope.has(name));
41
41
  };
@@ -5,7 +5,6 @@ import { FileAttachment } from "./fileAttachment.js";
5
5
  import * as Generators from "./generators/index.js";
6
6
  import { Interpreter } from "./interpreter.js";
7
7
  import { Mutable } from "./mutable.js";
8
- import { Observer } from "./observer.js";
9
8
  import * as Promises from "./promises/index.js";
10
9
  export declare const root: HTMLElement;
11
10
  export declare const library: {
@@ -104,5 +103,4 @@ export declare const library: {
104
103
  Promises: () => typeof Promises;
105
104
  Files: () => typeof Files;
106
105
  DOM: () => typeof DOM;
107
- __ojs_observer: () => () => Observer;
108
106
  };
@@ -5,7 +5,6 @@ import { FileAttachment } from "./fileAttachment.js";
5
5
  import * as Generators from "./generators/index.js";
6
6
  import { Interpreter } from "./interpreter.js";
7
7
  import { Mutable } from "./mutable.js";
8
- import { Observer } from "./observer.js";
9
8
  import * as Promises from "./promises/index.js";
10
9
  import * as recommendedLibraries from "./recommendedLibraries.js";
11
10
  import * as sampleDatasets from "./sampleDatasets.js";
@@ -22,7 +21,6 @@ export const library = {
22
21
  Promises: () => Promises, // deprecated!
23
22
  Files: () => Files, // deprecated!
24
23
  DOM: () => DOM, // deprecated!
25
- __ojs_observer: () => () => new Observer(),
26
24
  ...recommendedLibraries,
27
25
  ...sampleDatasets
28
26
  };
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.1.0",
8
+ "version": "2.1.2",
9
9
  "type": "module",
10
10
  "scripts": {
11
11
  "test": "vitest",
@@ -1,16 +0,0 @@
1
- export declare class Observer {
2
- #private;
3
- fulfilled?: (value: unknown) => void;
4
- rejected?: (error: unknown) => void;
5
- constructor();
6
- next(): Promise<{
7
- done: boolean;
8
- value: unknown;
9
- }>;
10
- throw(): {
11
- done: boolean;
12
- };
13
- return(): {
14
- done: boolean;
15
- };
16
- }
@@ -1,42 +0,0 @@
1
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
- };
6
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
7
- if (kind === "m") throw new TypeError("Private method is not writable");
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
9
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
10
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
11
- };
12
- var _Observer_promise;
13
- export class Observer {
14
- constructor() {
15
- _Observer_promise.set(this, void 0);
16
- Object.defineProperty(this, "fulfilled", {
17
- enumerable: true,
18
- configurable: true,
19
- writable: true,
20
- value: void 0
21
- });
22
- Object.defineProperty(this, "rejected", {
23
- enumerable: true,
24
- configurable: true,
25
- writable: true,
26
- value: void 0
27
- });
28
- this.next();
29
- }
30
- async next() {
31
- const value = await __classPrivateFieldGet(this, _Observer_promise, "f");
32
- __classPrivateFieldSet(this, _Observer_promise, new Promise((res, rej) => ((this.fulfilled = res), (this.rejected = rej))), "f");
33
- return { done: false, value };
34
- }
35
- throw() {
36
- return { done: true };
37
- }
38
- return() {
39
- return { done: true };
40
- }
41
- }
42
- _Observer_promise = new WeakMap();