@observablehq/notebook-kit 1.7.3 → 1.7.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 CHANGED
@@ -5,7 +5,7 @@
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/observablehq/notebook-kit.git"
7
7
  },
8
- "version": "1.7.3",
8
+ "version": "1.7.5",
9
9
  "type": "module",
10
10
  "scripts": {
11
11
  "test": "vitest",
@@ -113,7 +113,15 @@ export declare class NotebookRuntime {
113
113
  Mutable: () => typeof import("./stdlib/mutable.js").Mutable;
114
114
  Promises: () => typeof import("./stdlib/promises/index.js");
115
115
  DOM: () => typeof import("./stdlib/dom/index.js");
116
- require: () => typeof import("./stdlib/require.js").require;
116
+ require: () => {
117
+ (...specifiers: unknown[]): Promise<unknown>;
118
+ resolve: (specifier: unknown) => string;
119
+ } & {
120
+ alias: (aliases: Record<string, string>) => {
121
+ (...specifiers: unknown[]): Promise<unknown>;
122
+ resolve: (specifier: unknown) => string;
123
+ };
124
+ };
117
125
  __ojs_observer: () => () => import("./stdlib/observer.js").Observer;
118
126
  });
119
127
  define(state: DefineState, definition: Definition, observer?: typeof observe): void;
@@ -6,7 +6,6 @@ import { Interpreter } from "./interpreter.js";
6
6
  import { Mutable } from "./mutable.js";
7
7
  import { Observer } from "./observer.js";
8
8
  import * as Promises from "./promises/index.js";
9
- import { require } from "./require.js";
10
9
  export declare const root: HTMLElement;
11
10
  export declare const library: {
12
11
  aapl: () => Promise<any>;
@@ -103,6 +102,14 @@ export declare const library: {
103
102
  Mutable: () => typeof Mutable;
104
103
  Promises: () => typeof Promises;
105
104
  DOM: () => typeof DOM;
106
- require: () => typeof require;
105
+ require: () => {
106
+ (...specifiers: unknown[]): Promise<unknown>;
107
+ resolve: (specifier: unknown) => string;
108
+ } & {
109
+ alias: (aliases: Record<string, string>) => {
110
+ (...specifiers: unknown[]): Promise<unknown>;
111
+ resolve: (specifier: unknown) => string;
112
+ };
113
+ };
107
114
  __ojs_observer: () => () => Observer;
108
115
  };
@@ -1,4 +1,11 @@
1
- export declare function require(...specifiers: unknown[]): unknown;
2
- export declare namespace require {
3
- var resolve: (_specifier: unknown) => string;
4
- }
1
+ export declare const require: {
2
+ (...specifiers: unknown[]): Promise<unknown>;
3
+ resolve: (specifier: unknown) => string;
4
+ } & {
5
+ alias: typeof alias;
6
+ };
7
+ declare function alias(aliases: Record<string, string>): {
8
+ (...specifiers: unknown[]): Promise<unknown>;
9
+ resolve: (specifier: unknown) => string;
10
+ };
11
+ export {};
@@ -1,9 +1,13 @@
1
- require.resolve = resolve;
2
1
  const cache = new WeakMap();
3
- export function require(...specifiers) {
4
- return specifiers.length === 1
5
- ? import(/* @vite-ignore */ resolve(specifiers[0])).then(objectify)
6
- : Promise.all(specifiers.map((s) => require(s))).then((modules) => Object.assign({}, ...modules)); // prettier-ignore
2
+ export const require = Object.assign(Requirer(resolve), { alias });
3
+ function Requirer(resolve) {
4
+ async function require(...specifiers) {
5
+ return specifiers.length === 1
6
+ ? import(/* @vite-ignore */ resolve(specifiers[0])).then(objectify)
7
+ : Promise.all(specifiers.map((s) => require(s))).then(merge);
8
+ }
9
+ require.resolve = resolve;
10
+ return require;
7
11
  }
8
12
  function parseNpmSpecifier(specifier) {
9
13
  const parts = specifier.split("/");
@@ -16,12 +20,19 @@ function parseNpmSpecifier(specifier) {
16
20
  const path = parts.length > 0 ? `/${parts.join("/")}` : "";
17
21
  return { name, range, path };
18
22
  }
23
+ function alias(aliases) {
24
+ return Requirer((specifier) => {
25
+ if (specifier in aliases)
26
+ specifier = aliases[specifier];
27
+ return resolve(specifier);
28
+ });
29
+ }
19
30
  function resolve(_specifier) {
20
31
  const specifier = String(_specifier);
21
32
  if (isProtocol(specifier) || isLocal(specifier))
22
33
  return specifier;
23
34
  const { name, range, path } = parseNpmSpecifier(specifier);
24
- return `https://cdn.jsdelivr.net/npm/${name}${range}${path + (isFile(path) || isDirectory(path) ? "" : "/+esm")}`;
35
+ return `https://cdn.jsdelivr.net/npm/${name}${range}${path + ((isFile(path) && !isJavaScript(path)) || isDirectory(path) ? "" : "/+esm")}`;
25
36
  }
26
37
  /** Promote exclusive default export to module. */
27
38
  function defaultify(module) {
@@ -37,6 +48,9 @@ function objectify(module) {
37
48
  cache.set(module, (object = defaultify(module)));
38
49
  return object;
39
50
  }
51
+ function merge(modules) {
52
+ return Object.assign({}, ...modules);
53
+ }
40
54
  /** Returns true for e.g. https://example.com/ */
41
55
  function isProtocol(specifier) {
42
56
  return /^\w+:/.test(specifier);
@@ -46,10 +60,14 @@ function isLocal(specifier) {
46
60
  return /^(\.\/|\.\.\/|\/)/.test(specifier);
47
61
  }
48
62
  /** Returns true for e.g. foo/bar.js */
63
+ function isJavaScript(specifier) {
64
+ return /\.js$/i.test(specifier);
65
+ }
66
+ /** Returns true for e.g. foo/bar.txt */
49
67
  function isFile(specifier) {
50
- return /(\.\w*)$/.test(specifier);
68
+ return /\.\w*$/.test(specifier);
51
69
  }
52
70
  /** Returns true for e.g. foo/bar/ */
53
71
  function isDirectory(specifier) {
54
- return /(\/)$/.test(specifier);
72
+ return /\/$/.test(specifier);
55
73
  }
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": "1.7.3",
8
+ "version": "1.7.5",
9
9
  "type": "module",
10
10
  "scripts": {
11
11
  "test": "vitest",